DevExpress之ChartControl用法实例总结
程序员文章站
2023-12-17 22:15:10
devexpress中的chartcontrol顾名思义就是数据基于图表展示,其关键在于series上的处理。本文实例展示了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; }
上述代码运行效果如下图所示: