DevExpress之ChartControl用法實例總結。本站提示廣大學習愛好者:(DevExpress之ChartControl用法實例總結)文章只能為提供參考,不一定能成為您想要的結果。以下是DevExpress之ChartControl用法實例總結正文
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; }
上述代碼運轉後果以下圖所示: