A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈君 金牌黑马   /  2014-8-26 22:47  /  7762 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这篇文章主要介绍了DevExpress之ChartControl用法实例总结,需要的朋友可以参考下
DevExpress中的ChartControl顾名思义就是数据基于图表展示,其关键在于Series上的处理。本文实例展示了ChartControl的用法,具体内容如下:
主要功能代码部分如下:
  1. using System;
  2. using System.Drawing;
  3. using DevExpress.XtraCharts;

  4. namespace DevExpressUtilHelpV3
  5. {
  6. public static class ChartToolV3
  7. {
  8. /// <summary>
  9. /// 创建Series
  10. /// </summary>
  11. /// <param name="chat">ChartControl</param>
  12. /// <param name="seriesName">Series名字『诸如:理论电量』</param>
  13. /// <param name="seriesType">seriesType『枚举』</param>
  14. /// <param name="dataSource">数据源</param>
  15. /// <param name="xBindName">ChartControl的X轴绑定</param>
  16. /// <param name="yBindName">ChartControl的Y轴绑定</param>
  17. public static void CreateSeries(this ChartControl chat, string seriesName, ViewType seriesType, object dataSource, string xBindName, string yBindName)
  18. {
  19. CreateSeries(chat, seriesName, seriesType, dataSource, xBindName, yBindName, null);
  20. }
  21. /// <summary>
  22. /// 创建Series
  23. /// </summary>
  24. /// <param name="chat">ChartControl</param>
  25. /// <param name="seriesName">Series名字『诸如:理论电量』</param>
  26. /// <param name="seriesType">seriesType『枚举』</param>
  27. /// <param name="dataSource">数据源</param>
  28. /// <param name="xBindName">ChartControl的X轴绑定</param>
  29. /// <param name="yBindName">ChartControl的Y轴绑定</param>
  30. /// <param name="createSeriesRule">Series自定义『委托』</param>
  31. public static void CreateSeries(this ChartControl chat, string seriesName, ViewType seriesType, object dataSource, string xBindName, string yBindName, Action<Series> createSeriesRule)
  32. {
  33. if (chat == null)
  34. throw new ArgumentNullException("chat");
  35. if (string.IsNullOrEmpty(seriesName))
  36. throw new ArgumentNullException("seriesType");
  37. if (string.IsNullOrEmpty(xBindName))
  38. throw new ArgumentNullException("xBindName");
  39. if (string.IsNullOrEmpty(yBindName))
  40. throw new ArgumentNullException("yBindName");

  41. Series _series = new Series(seriesName, seriesType);
  42. _series.ArgumentScaleType = ScaleType.Qualitative;
  43. _series.ArgumentDataMember = xBindName;
  44. _series.ValueDataMembers[0] = yBindName;

  45. _series.DataSource = dataSource;
  46. if (createSeriesRule != null)
  47. createSeriesRule(_series);
  48. chat.Series.Add(_series);
  49. }

  50. }
  51. }
复制代码

代码使用示例如下:
  1. public Form1()
  2. {
  3. InitializeComponent();
  4. }
  5. private void Form1_Load(object sender, EventArgs e)
  6. {
  7. DataTable _dt = CreateTestDB();
  8. chartControl1.CreateSeries("理论功率", ViewType.Spline, _dt, "time", "Power");
  9. chartControl1.CreateSeries("实际功率", ViewType.Spline, _dt, "time", "ActulPower");
  10. }
  11. /// <summary>
  12. /// 准备数据源
  13. /// </summary>
  14. /// <returns>DataTable</returns>
  15. private DataTable CreateTestDB()
  16. {
  17. DataTable _testData = new DataTable();
  18. _testData.Columns.Add(new DataColumn("time", typeof(string)));
  19. _testData.Columns.Add(new DataColumn("Power", typeof(decimal)));
  20. _testData.Columns.Add(new DataColumn("ActulPower", typeof(decimal)));
  21. Random _rm = new Random();
  22. for (int i = 0; i < 24; i++)
  23. {
  24. DataRow _drNew = _testData.NewRow();
  25. _drNew["time"] = string.Format("{0}点", i);
  26. _drNew["Power"] = 250;
  27. _drNew["ActulPower"] = _rm.Next(220, 245);
  28. _testData.Rows.Add(_drNew);
  29. }
  30. return _testData;
  31. }
复制代码

上述代码运行效果如下图所示:

点评

写的很好  发表于 2014-8-27 09:30

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马