架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 21163|回复: 0

[WPF] WPF、Silverlight项目中使用柱状图、饼状图、折线图

[复制链接]
发表于 2015-12-9 15:46:45 | 显示全部楼层 |阅读模式
在开发的过程中,可能会遇到柱状图、饼状图、折线图来更好的显示数据,最近整理了一下,遂放出来望需要的朋友可以参考。本文仅仅是简单显示,如需复杂显示效果请参考官网代码示例。----本文代码使用WPF,Silverlight类似代码,使用第三方wpf_visifire_v5.1.2-0_trial控件。

先放上一组截图吧:
06164254-0d56204fed634e14a80cca63cd30e62a.png
06164258-a02e55f0156e41f2939b52d94128325d.png
06164307-760a07b8953f4765b3be837acc9603e9.png


公共数据:

  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" };
复制代码

柱状图:

  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.         }
复制代码

饼状图:

  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.         }
复制代码
折线图:

  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.         }
复制代码

代码下载 VisifireShow.rar (475.66 KB, 下载次数: 2, 售价: 1 粒MB)





上一篇:一个break能跳出几个循环?怎么才能跳出所有循环?
下一篇:给visual studio 2013或者vs2010设置背景图片,炫到酷!
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-12-19 01:35

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表