This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21090|Reply: 0

[WPF] WPF and Silverlight projects use bar charts, pie charts, and line charts

[Copy link]
Posted on 12/9/2015 3:46:45 PM | | | |
In the process of development, you may encounter bar charts, pie charts, and line charts to better display data. This article is just a simple display, if you want complex display effects, please refer to the official website code example. ---- the code in this article uses WPF, Silverlight similar code, and uses third-party wpf_visifire_v5.1.2-0_trial controls.

Let's put a set of screenshots first:


Public data:

  1. private List<string> strListx = new List<string>() { "苹果", "樱桃", "菠萝", "香蕉", "榴莲", "葡萄", "桃子", "猕猴桃" };
  2.         private List<string> strListy = new List<string>() { "13", "75", "60", "38", "97", "22", "39", "80" };

  3.         private List<DateTime> LsTime = new List<DateTime>()
  4.             {
  5.                new DateTime(2012,1,1),
  6.                new DateTime(2012,2,1),
  7.                new DateTime(2012,3,1),
  8.                new DateTime(2012,4,1),
  9.                new DateTime(2012,5,1),
  10.                new DateTime(2012,6,1),
  11.                new DateTime(2012,7,1),
  12.                new DateTime(2012,8,1),
  13.                new DateTime(2012,9,1),
  14.                new DateTime(2012,10,1),
  15.                new DateTime(2012,11,1),
  16.                new DateTime(2012,12,1),
  17.             };
  18.         private List<string> cherry = new List<string>() { "33", "75", "60", "98", "67", "88", "39", "45", "13", "22", "45", "80" };
  19.         private List<string> pineapple = new List<string>() { "13", "34", "38", "12", "45", "76", "36", "80", "97", "22", "76", "39" };
Copy code

Histogram:

  1. public void CreateChartColumn(string name, List<string> valuex, List<string> valuey)
  2.         {
  3.             //创建一个图标
  4.             Chart chart = new Chart();

  5.             //设置图标的宽度和高度
  6.             chart.Width = 580;
  7.             chart.Height = 380;
  8.             chart.Margin = new Thickness(100, 5, 10, 5);
  9.             //是否启用打印和保持图片
  10.             chart.ToolBarEnabled = false;

  11.             //设置图标的属性
  12.             chart.ScrollingEnabled = false;//是否启用或禁用滚动
  13.             chart.View3D = true;//3D效果显示

  14.             //创建一个标题的对象
  15.             Title title = new Title();

  16.             //设置标题的名称
  17.             title.Text = Name;
  18.             title.Padding = new Thickness(0, 10, 5, 0);

  19.             //向图标添加标题
  20.             chart.Titles.Add(title);

  21.             Axis yAxis = new Axis();
  22.             //设置图标中Y轴的最小值永远为0           
  23.             yAxis.AxisMinimum = 0;
  24.             //设置图表中Y轴的后缀         
  25.             yAxis.Suffix = "斤";
  26.             chart.AxesY.Add(yAxis);

  27.             // 创建一个新的数据线。               
  28.             DataSeries dataSeries = new DataSeries();

  29.             // 设置数据线的格式
  30.             dataSeries.RenderAs = RenderAs.StackedColumn;//柱状Stacked


  31.             // 设置数据点              
  32.             DataPoint dataPoint;
  33.             for (int i = 0; i < valuex.Count; i++)
  34.             {
  35.                 // 创建一个数据点的实例。                  
  36.                 dataPoint = new DataPoint();
  37.                 // 设置X轴点                    
  38.                 dataPoint.AxisXLabel = valuex[i];
  39.                 //设置Y轴点                  
  40.                 dataPoint.YValue = double.Parse(valuey[i]);
  41.                 //添加一个点击事件        
  42.                 dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  43.                 //添加数据点                  
  44.                 dataSeries.DataPoints.Add(dataPoint);
  45.             }

  46.             // 添加数据线到数据序列。               
  47.             chart.Series.Add(dataSeries);

  48.             //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.           
  49.             Grid gr = new Grid();
  50.             gr.Children.Add(chart);
  51.             Simon.Children.Add(gr);
  52.         }
Copy code

Pie chart:

  1. public void CreateChartPie(string name, List<string> valuex, List<string> valuey)
  2.         {
  3.             //创建一个图标
  4.             Chart chart = new Chart();

  5.             //设置图标的宽度和高度
  6.             chart.Width = 580;
  7.             chart.Height = 380;
  8.             chart.Margin = new Thickness(100, 5, 10, 5);
  9.             //是否启用打印和保持图片
  10.             chart.ToolBarEnabled = false;

  11.             //设置图标的属性
  12.             chart.ScrollingEnabled = false;//是否启用或禁用滚动
  13.             chart.View3D = true;//3D效果显示

  14.             //创建一个标题的对象
  15.             Title title = new Title();

  16.             //设置标题的名称
  17.             title.Text = name;
  18.             title.Padding = new Thickness(0, 10, 5, 0);

  19.             //向图标添加标题
  20.             chart.Titles.Add(title);

  21.             //Axis yAxis = new Axis();
  22.             ////设置图标中Y轴的最小值永远为0           
  23.             //yAxis.AxisMinimum = 0;
  24.             ////设置图表中Y轴的后缀         
  25.             //yAxis.Suffix = "斤";
  26.             //chart.AxesY.Add(yAxis);

  27.             // 创建一个新的数据线。               
  28.             DataSeries dataSeries = new DataSeries();

  29.             // 设置数据线的格式
  30.             dataSeries.RenderAs = RenderAs.Pie;//柱状Stacked


  31.             // 设置数据点              
  32.             DataPoint dataPoint;
  33.             for (int i = 0; i < valuex.Count; i++)
  34.             {
  35.                 // 创建一个数据点的实例。                  
  36.                 dataPoint = new DataPoint();
  37.                 // 设置X轴点                    
  38.                 dataPoint.AxisXLabel = valuex[i];

  39.                 dataPoint.LegendText = "##" + valuex[i];
  40.                 //设置Y轴点                  
  41.                 dataPoint.YValue = double.Parse(valuey[i]);
  42.                 //添加一个点击事件        
  43.                 dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  44.                 //添加数据点                  
  45.                 dataSeries.DataPoints.Add(dataPoint);
  46.             }

  47.             // 添加数据线到数据序列。               
  48.             chart.Series.Add(dataSeries);

  49.             //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.           
  50.             Grid gr = new Grid();
  51.             gr.Children.Add(chart);
  52.             Simon.Children.Add(gr);
  53.         }
Copy code
Line chart:

  1. public void CreateChartSpline(string name, List<DateTime> lsTime, List<string> cherry, List<string> pineapple)
  2.         {
  3.             //创建一个图标
  4.             Chart chart = new Chart();

  5.             //设置图标的宽度和高度
  6.             chart.Width = 580;
  7.             chart.Height = 380;
  8.             chart.Margin = new Thickness(100, 5, 10, 5);
  9.             //是否启用打印和保持图片
  10.             chart.ToolBarEnabled = false;

  11.             //设置图标的属性
  12.             chart.ScrollingEnabled = false;//是否启用或禁用滚动
  13.             chart.View3D = true;//3D效果显示

  14.             //创建一个标题的对象
  15.             Title title = new Title();

  16.             //设置标题的名称
  17.             title.Text = name;
  18.             title.Padding = new Thickness(0, 10, 5, 0);

  19.             //向图标添加标题
  20.             chart.Titles.Add(title);

  21.             //初始化一个新的Axis
  22.             Axis xaxis = new Axis();
  23.             //设置Axis的属性
  24.             //图表的X轴坐标按什么来分类,如时分秒
  25.             xaxis.IntervalType = IntervalTypes.Months;
  26.             //图表的X轴坐标间隔如2,3,20等,单位为xAxis.IntervalType设置的时分秒。
  27.             xaxis.Interval = 1;
  28.             //设置X轴的时间显示格式为7-10 11:20           
  29.             xaxis.ValueFormatString = "MM月";
  30.             //给图标添加Axis            
  31.             chart.AxesX.Add(xaxis);

  32.             Axis yAxis = new Axis();
  33.             //设置图标中Y轴的最小值永远为0           
  34.             yAxis.AxisMinimum = 0;
  35.             //设置图表中Y轴的后缀         
  36.             yAxis.Suffix = "斤";
  37.             chart.AxesY.Add(yAxis);


  38.             // 创建一个新的数据线。               
  39.             DataSeries dataSeries = new DataSeries();
  40.             // 设置数据线的格式。               
  41.             dataSeries.LegendText = "樱桃";

  42.             dataSeries.RenderAs = RenderAs.Spline;//折线图

  43.             dataSeries.XValueType = ChartValueTypes.DateTime;
  44.             // 设置数据点              
  45.             DataPoint dataPoint;
  46.             for (int i = 0; i < lsTime.Count; i++)
  47.             {
  48.                 // 创建一个数据点的实例。                  
  49.                 dataPoint = new DataPoint();
  50.                 // 设置X轴点                    
  51.                 dataPoint.XValue = lsTime[i];
  52.                 //设置Y轴点                  
  53.                 dataPoint.YValue = double.Parse(cherry[i]);
  54.                 dataPoint.MarkerSize = 8;
  55.                 //dataPoint.Tag = tableName.Split('(')[0];
  56.                 //设置数据点颜色                  
  57.                 // dataPoint.Color = new SolidColorBrush(Colors.LightGray);                  
  58.                 dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  59.                 //添加数据点                  
  60.                 dataSeries.DataPoints.Add(dataPoint);
  61.             }

  62.             // 添加数据线到数据序列。               
  63.             chart.Series.Add(dataSeries);


  64.             // 创建一个新的数据线。               
  65.             DataSeries dataSeriesPineapple = new DataSeries();
  66.             // 设置数据线的格式。         

  67.             dataSeriesPineapple.LegendText = "菠萝";

  68.             dataSeriesPineapple.RenderAs = RenderAs.Spline;//折线图

  69.             dataSeriesPineapple.XValueType = ChartValueTypes.DateTime;
  70.             // 设置数据点              

  71.             DataPoint dataPoint2;
  72.             for (int i = 0; i < lsTime.Count; i++)
  73.             {
  74.                 // 创建一个数据点的实例。                  
  75.                 dataPoint2 = new DataPoint();
  76.                 // 设置X轴点                    
  77.                 dataPoint2.XValue = lsTime[i];
  78.                 //设置Y轴点                  
  79.                 dataPoint2.YValue = double.Parse(pineapple[i]);
  80.                 dataPoint2.MarkerSize = 8;
  81.                 //dataPoint2.Tag = tableName.Split('(')[0];
  82.                 //设置数据点颜色                  
  83.                 // dataPoint.Color = new SolidColorBrush(Colors.LightGray);                  
  84.                 dataPoint2.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  85.                 //添加数据点                  
  86.                 dataSeriesPineapple.DataPoints.Add(dataPoint2);
  87.             }
  88.             // 添加数据线到数据序列。               
  89.             chart.Series.Add(dataSeriesPineapple);

  90.             //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.           
  91.             Grid gr = new Grid();
  92.             gr.Children.Add(chart);
  93.             
  94.             Simon.Children.Add(gr);
  95.         }
Copy code

Code download VisifireShow.rar (475.66 KB, Number of downloads: 2, 售价: 1 粒MB)





Previous:How many loops can a break jump out? How can I jump out of all loops?
Next:Set a background image for Visual Studio 2013 or VS2010, it's cool!
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com