欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

DevExpress之ChartControl用法实例总结

时间:2020-10-26 23:17:18|栏目:.NET代码|点击:

DevExpress中的ChartControl顾名思义就是数据基于图表展示,其关键在于Series上的处理。本文实例展示了ChartControl的用法,具体内容如下:

主要功能代码部分如下:

using System;
using System.Drawing;
using DevExpress.XtraCharts;

namespace DevExpressUtilHelpV3
{
  public static class ChartToolV3
  {
    /// <summary>
    /// 创建Series
    /// </summary>
    /// <param name="chat">ChartControl</param>
    /// <param name="seriesName">Series名字『诸如:理论电量』</param>
    /// <param name="seriesType">seriesType『枚举』</param>
    /// <param name="dataSource">数据源</param>
    /// <param name="xBindName">ChartControl的X轴绑定</param>
    /// <param name="yBindName">ChartControl的Y轴绑定</param>
    public static void CreateSeries(this ChartControl chat, string seriesName, ViewType seriesType, object dataSource, string xBindName, string yBindName)
    {
      CreateSeries(chat, seriesName, seriesType, dataSource, xBindName, yBindName, null);
    }
    /// <summary>
    /// 创建Series
    /// </summary>
    /// <param name="chat">ChartControl</param>
    /// <param name="seriesName">Series名字『诸如:理论电量』</param>
    /// <param name="seriesType">seriesType『枚举』</param>
    /// <param name="dataSource">数据源</param>
    /// <param name="xBindName">ChartControl的X轴绑定</param>
    /// <param name="yBindName">ChartControl的Y轴绑定</param>
    /// <param name="createSeriesRule">Series自定义『委托』</param>
    public static void CreateSeries(this ChartControl chat, string seriesName, ViewType seriesType, object dataSource, string xBindName, string yBindName, Action<Series> createSeriesRule)
    {
      if (chat == null)
        throw new ArgumentNullException("chat");
      if (string.IsNullOrEmpty(seriesName))
        throw new ArgumentNullException("seriesType");
      if (string.IsNullOrEmpty(xBindName))
        throw new ArgumentNullException("xBindName");
      if (string.IsNullOrEmpty(yBindName))
        throw new ArgumentNullException("yBindName");

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

      _series.DataSource = dataSource;
      if (createSeriesRule != null)
        createSeriesRule(_series);
      chat.Series.Add(_series);
    }

  }
}

代码使用示例如下:

public Form1()
{
  InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
  DataTable _dt = CreateTestDB();
  chartControl1.CreateSeries("理论功率", ViewType.Spline, _dt, "time", "Power");
  chartControl1.CreateSeries("实际功率", ViewType.Spline, _dt, "time", "ActulPower");
}
/// <summary>
/// 准备数据源
/// </summary>
/// <returns>DataTable</returns>
private DataTable CreateTestDB()
{
  DataTable _testData = new DataTable();
  _testData.Columns.Add(new DataColumn("time", typeof(string)));
  _testData.Columns.Add(new DataColumn("Power", typeof(decimal)));
  _testData.Columns.Add(new DataColumn("ActulPower", typeof(decimal)));
  Random _rm = new Random();
  for (int i = 0; i < 24; i++)
  {
 DataRow _drNew = _testData.NewRow();
 _drNew["time"] = string.Format("{0}点", i);
 _drNew["Power"] = 250;
 _drNew["ActulPower"] = _rm.Next(220, 245);
 _testData.Rows.Add(_drNew);
  }
  return _testData;
}

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

上一篇:ASP.NET中为GridView添加删除提示框的方法

栏    目:.NET代码

下一篇:c#基础学习之封装

本文标题:DevExpress之ChartControl用法实例总结

本文地址:http://www.codeinn.net/misctech/16104.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有