using System; using System.Drawing; using DevExpress.XtraCharts;dom
namespace DevExpressUtilHelpV3this
{spa
public static class ChartToolV3 orm
{ ci
/// <summary> string
/// 建立Series it
/// </summary> io
/// <param name="chat">ChartControl</param> class
/// <param name="seriesName">Series名字『諸如:理論電量』</param> test
/// <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;
}