ChartServices Dev图形封装
1.工具类ChartServices using System; using System.Data; using System.Drawing; using DevExpress.XtraCharts; namespace HraWeb.Common { public static class ChartServices { /// <summary> /// 绘制图形 /// </summary> /// <param name="control">图表控件</param> /// <param name="seriesName">系列名</param> /// <param name="type">类型</param> /// <param name="dt">数据源</param> /// <param name="column1"></param> /// <param name="column2"></param> public static void DrawChart(DevExpress.XtraCharts.Web.WebChartControl control, string seriesName, ViewType type, DataTable dt, string column1, string column2) { Series series = new Series(seriesName, type); DataTable table = dt; SeriesPoint point = null; for (int i = 0; i < table.Rows.Count; i++) { point = new SeriesPoint(table.Rows[i][column1].ToString(), Convert.ToDouble(table.Rows[i][column2].ToString())); series.Points.Add(point); } control.Series.Add(series); //针对饼图的特殊处理 if (type == ViewType.Pie) { //设置显示方式(Argument:显示图例说明,ArgumentAndValues:显示图例内容和数据) series.Label.PointOptions.PointView = PointView.ArgumentAndValues; //设置数据显示形式(Percent:百分比,Currency:货币类型,数据前添加¥,Scientific:科学计数法) series.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent; //数据是否保留小数(0:不保留小数位,1保留一位小数,2保留两位小数) series.Label.PointOptions.ValueNumericOptions.Precision = 0; //数据以百分比显示时只能是Default和None ((PieSeriesLabel)series.Label).ResolveOverlappingMode = ResolveOverlappingMode.Default; } } /// <summary> /// 设置图表标题 /// </summary> /// <param name="control">图表控件</param> /// /// <param name="isVisible">标题是否可见</param> /// <param name="text">标题文本</param> /// <param name="isWordWrop">是否换行</param> /// <param name="maxLineCount">最大允许行数</param> /// <param name="alignment">对齐方式</param> /// <param name="dock">位置</param> /// <param name="isAntialiasing">是否允许设置外观</param> /// <param name="font">字体</param> /// <param name="textColor">文本颜色</param> /// <param name="indent">字体缩进值</param> public static void SetChartTitle(DevExpress.XtraCharts.Web.WebChartControl control, bool isVisible, String text, bool isWordWrop, int maxLineCount, StringAlignment alignment, ChartTitleDockStyle dock, bool isAntialiasing, Font font, Color textColor, int indent) { //设置标题 ChartTitle title = new ChartTitle(); title.Visible = isVisible; //显示文本 title.Text = text; //是否允许换行 title.WordWrap = isWordWrop; //最大允许行数 title.MaxLineCount = maxLineCount; //对齐方式 title.Alignment = alignment; //位置 title.Dock = dock; //是否允许设置外观 title.Antialiasing = isAntialiasing; //字体 title.Font = font; //字体颜色 title.TextColor = textColor; //缩进值 title.Indent = indent; control.Titles.Add(title); } /// <summary> /// 为X轴添加标题 /// </summary> /// <param name="control">图形控件</param> /// <param name="isVisible">标题是否可见</param> /// <param name="aligment">对齐方式</param> /// <param name="text">标题显示文本</param> /// <param name="color">标题字体颜色</param> /// <param name="isAntialiasing">是否允许设置外观</param> /// <param name="font">字体</param> public static void SetAxisX(DevExpress.XtraCharts.Web.WebChartControl control, bool isVisible, StringAlignment aligment, string text, Color color, bool isAntialiasing, Font font) { XYDiagram xydiagram = (XYDiagram)control.Diagram; xydiagram.AxisX.Title.Visible = isVisible; xydiagram.AxisX.Title.Alignment = aligment; xydiagram.AxisX.Title.Text = text; xydiagram.AxisX.Title.TextColor = color; xydiagram.AxisX.Title.Antialiasing = isAntialiasing; xydiagram.AxisX.Title.Font = font; } /// <summary> /// 为X轴添加标题 /// </summary> /// <param name="control">图形控件</param> /// <param name="isVisible">标题是否可见</param> /// <param name="aligment">对齐方式</param> /// <param name="text">标题显示文本</param> /// <param name="color">标题字体颜色</param> /// <param name="isAntialiasing">是否允许设置外观</param> /// <param name="font">字体</param> public static void SetAxisY(DevExpress.XtraCharts.Web.WebChartControl control, bool isVisible, StringAlignment aligment, string text, Color color, bool isAntialiasing, Font font) { XYDiagram xydiagram = (XYDiagram)control.Diagram; xydiagram.AxisY.Title.Visible = isVisible; xydiagram.AxisY.Title.Alignment = aligment; xydiagram.AxisY.Title.Text = text; xydiagram.AxisY.Title.TextColor = color; xydiagram.AxisY.Title.Antialiasing = isAntialiasing; xydiagram.AxisY.Title.Font = font; } } } 2.webform.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="HraWeb.WebForm2" %> <%@ Register assembly="DevExpress.XtraCharts.v15.1.Web, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.XtraCharts.Web" tagprefix="dxchartsui" %> <%@ Register TagPrefix="dx" Namespace="DevExpress.Web" Assembly="DevExpress.Web.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" %> <%@ Register assembly="DevExpress.XtraCharts.v15.1, Version=15.1.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.XtraCharts" tagprefix="cc1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <dxchartsui:WebChartControl ID="WebChartControl5" runat="server" CrosshairEnabled="True" Height="200px" PaletteName="Flow" Width="229px"> <seriesserializable> <cc1:Series Name="Series 1"> <points> <cc1:SeriesPoint ArgumentSerializable="0" Values="1000"> </cc1:SeriesPoint> <cc1:SeriesPoint ArgumentSerializable="1" Values="200"> </cc1:SeriesPoint> <cc1:SeriesPoint ArgumentSerializable="2" Values="30"> </cc1:SeriesPoint> <cc1:SeriesPoint ArgumentSerializable="3" Values="4"> </cc1:SeriesPoint> <cc1:SeriesPoint ArgumentSerializable="4" Values="500"> </cc1:SeriesPoint> </points> <viewserializable> <cc1:FunnelSeriesView> </cc1:FunnelSeriesView> </viewserializable> </cc1:Series> </seriesserializable> </dxchartsui:WebChartControl> <dx:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="类型" Caption="公司基本面信息" Width="600px" > <SettingsPager Visible="False"> </SettingsPager> <Styles> <AlternatingRow Enabled="true" BackColor="#D5FEFF" /> </Styles> </dx:ASPxGridView> <dxchartsui:WebChartControl ID="WebChartControl2" runat="server" Width="600px" Height="300px" AppearanceNameSerializable="Gray" CrosshairEnabled="True" PaletteName="Green"> </dxchartsui:WebChartControl> <table> <td> <dxchartsui:WebChartControl ID="WebChartControl4" runat="server" Width="300px" Height="250px" AppearanceNameSerializable="Gray" CrosshairEnabled="True" PaletteName="Green"> </dxchartsui:WebChartControl> </td> <td> <dxchartsui:WebChartControl ID="WebChartControl6" runat="server" Width="300px" Height="250px" AppearanceNameSerializable="Gray" CrosshairEnabled="True" PaletteName="Green"> </dxchartsui:WebChartControl> </td> </table> <dxchartsui:WebChartControl ID="WebChartControl3" runat="server" Width="600px" Height="300px" AppearanceNameSerializable="Gray" CrosshairEnabled="True" PaletteName="Green"> </dxchartsui:WebChartControl> <dxchartsui:WebChartControl ID="WebChartControl1" runat="server" Width="600px" Height="300px" AppearanceNameSerializable="Gray" CrosshairEnabled="True" PaletteName="Green"> </dxchartsui:WebChartControl> <dx:ASPxGridView ID="ASPxGridView2" runat="server" KeyFieldName="类型" Caption="公司基本面信息" Width="600px" > <SettingsPager Visible="False"> </SettingsPager> <Styles> <AlternatingRow Enabled="true" BackColor="#D5FEFF" /> </Styles> </dx:ASPxGridView> <dx:ASPxGridView ID="ASPxGridView3" runat="server" KeyFieldName="类型" Caption="公司基本面信息" Width="600px" > <SettingsPager Visible="False"> </SettingsPager> <Styles> <AlternatingRow Enabled="true" BackColor="#D5FEFF" /> </Styles> </dx:ASPxGridView> </div> </form> </body> </html> 3.webform.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using DevExpress.XtraCharts; using System.Drawing; using DevExpress.Web; using Framework; using HraWeb.Common; namespace HraWeb { public partial class WebForm2 : BasePage { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = CreateData(); this.ASPxGridView1.DataSource = dt; this.ASPxGridView1.DataBind(); this.ASPxGridView2.DataSource = dt; this.ASPxGridView2.DataBind(); this.ASPxGridView3.DataSource = dt; this.ASPxGridView3.DataBind(); for (int i = 1; i < ASPxGridView1.Columns.Count; i++) this.ASPxGridView1.DataSource = dt; this.ASPxGridView1.DataBind(); for (int i = 1; i < ASPxGridView1.Columns.Count; i++) { ASPxGridView1.Columns[i].HeaderStyle.BackColor = Color.Green; ASPxGridView1.Columns[i].HeaderStyle.ForeColor = Color.White; } for (int i = 1; i < ASPxGridView2.Columns.Count; i++) { ASPxGridView2.Columns[i].HeaderStyle.BackColor = Color.Green; ASPxGridView2.Columns[i].HeaderStyle.ForeColor = Color.White; } for (int i = 1; i < ASPxGridView3.Columns.Count; i++) { ASPxGridView3.Columns[i].HeaderStyle.BackColor = Color.Green; ASPxGridView3.Columns[i].HeaderStyle.ForeColor = Color.White; } } ASPxGridView1.HtmlDataCellPrepared += ASPxGridView1_HtmlDataCellPrepared; ASPxGridView2.HtmlDataCellPrepared += ASPxGridView2_HtmlDataCellPrepared; ASPxGridView3.HtmlDataCellPrepared += ASPxGridView3_HtmlDataCellPrepared; this.DrawBar(); this.DrawLine(); this.DrawPie(); this.DrawBarAndLine(); } private void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if (e.DataColumn.FieldName == "类型") { e.Cell.BackColor = Color.Green; e.Cell.ForeColor = Color.White; } } private static Contract.IService.IDaoService GetDao() { Spring.Context.IApplicationContext ctx = Spring.Context.Support.ContextRegistry.GetContext(); Contract.IService.IDaoService _dao = (Contract.IService.IDaoService)ctx["DaoService"]; return _dao; } private void ASPxGridView2_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if (e.DataColumn.FieldName == "类型") { e.Cell.BackColor = Color.Green; e.Cell.ForeColor = Color.White; } } private void ASPxGridView3_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) { if (e.DataColumn.FieldName == "类型") { e.Cell.BackColor = Color.Green; e.Cell.ForeColor = Color.White; } } public static DataTable GetWeekMoneyAndCost() { var Dao = GetDao(); QueryInfo dtInfo = new QueryInfo(); dtInfo.CustomSQL = @" select '2013年年度' type,t.total_asset value1,t.sale_revenue value2,t.return_total_asset value3,t.inventory_turnover value4, t.current_ratio value5,t.sale_cash_ratio value6,6.5 z_score,6.3 ANN from ccr_credit_input_data t where t.input_date=to_date('2013/12/31','yyyy/mm/dd') and t.company_name=209045 and t.purpose=102601 union all select '2014年年度' type,t.total_asset value1,t.sale_revenue value2,t.return_total_asset value3,t.inventory_turnover value4, t.current_ratio value5,t.sale_cash_ratio value6, 6.5 z_score,6.3 ANN from ccr_credit_input_data t where t.input_date=to_date('2013-12-31','yyyy-mm-dd') and t.company_name=209039 and t.purpose=102601 "; DataTable dt2 = Dao.ExcuteDataSet(dtInfo).Tables[0]; return dt2; } private DataTable CreateData() { QueryInfo dtInfo = new QueryInfo(); dtInfo.CustomSQL = @" select '2013年年度' type,t.total_asset value1,t.sale_revenue value2,t.return_total_asset value3,t.inventory_turnover value4, t.current_ratio value5,t.sale_cash_ratio value6,6.5 z_score,6.3 ANN from ccr_credit_input_data t where t.input_date=to_date('2013/12/31','yyyy/mm/dd') and t.company_name=209045 and t.purpose=102601 union all select '2014年年度' type,t.total_asset value1,t.sale_revenue value2,t.return_total_asset value3,t.inventory_turnover value4, t.current_ratio value5,t.sale_cash_ratio value6, 6.5 z_score,6.3 ANN from ccr_credit_input_data t where t.input_date=to_date('2013-12-31','yyyy-mm-dd') and t.company_name=209039 and t.purpose=102601 "; DataTable dt2 = Dao.ExcuteDataSet(dtInfo).Tables[0]; DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("类型")); dt.Columns.Add(new DataColumn("总资产", typeof(decimal))); dt.Columns.Add(new DataColumn("销售收入", typeof(decimal))); dt.Columns.Add(new DataColumn("总资产报酬率", typeof(decimal))); dt.Columns.Add(new DataColumn("存货周转率", typeof(decimal))); dt.Columns.Add(new DataColumn("流动比率", typeof(decimal))); dt.Columns.Add(new DataColumn("销售现金比率", typeof(decimal))); foreach (DataRow row in dt2.Rows) { dt.Rows.Add(new object[] { row[0], row[1], row[2], row[3], row[4], row[5], row[6] }); } return dt; } /// <summary> /// 绘制柱状图 /// </summary> private void DrawBar() { ChartServices.SetChartTitle(this.WebChartControl1, true, "神经网络结构拟合情况表", true, 2, StringAlignment.Center, ChartTitleDockStyle.Top, true, new Font("宋体", 12, FontStyle.Bold), Color.Blue, 10); //如不需显示图表标题可不用调用本段代码,下同 ChartServices.DrawChart(this.WebChartControl1, "Z SCORE", ViewType.Bar, GetWeekMoneyAndCost(), "TYPE", "Z_SCORE"); ChartServices.DrawChart(this.WebChartControl1, "ANN", ViewType.Bar, GetWeekMoneyAndCost(), "TYPE", "ANN"); } /// <summary> /// 绘制折线图 /// </summary> private void DrawLine() { ChartServices.SetChartTitle(this.WebChartControl3, true, "2012年12月第1周收入情况", true, 2, StringAlignment.Center, ChartTitleDockStyle.Top, true, new Font("宋体", 12, FontStyle.Bold), Color.OrangeRed, 10); ChartServices.DrawChart(this.WebChartControl3, "收益", ViewType.Line, GetWeekMoneyAndCost(), "TYPE", "Z_SCORE"); ChartServices.DrawChart(this.WebChartControl3, "成本", ViewType.Line, GetWeekMoneyAndCost(), "TYPE", "ANN"); } /// <summary> /// 柱状图和折线图在同一图表中 /// </summary> private void DrawBarAndLine() { ChartServices.SetChartTitle(this.WebChartControl2, true, "2012年12月第1周收入情况", true, 2, StringAlignment.Center, ChartTitleDockStyle.Top, true, new Font("宋体", 12, FontStyle.Bold), Color.Red, 10); ChartServices.DrawChart(this.WebChartControl2, "收益", ViewType.Bar, GetWeekMoneyAndCost(), "TYPE", "Z_SCORE"); ChartServices.DrawChart(this.WebChartControl2, "成本", ViewType.Bar, GetWeekMoneyAndCost(), "TYPE", "ANN"); ChartServices.SetChartTitle(this.WebChartControl2, false, "2012年12月第1周收入情况", true, 2, StringAlignment.Center, ChartTitleDockStyle.Top, true, new Font("宋体", 12, FontStyle.Bold), Color.Red, 10); ChartServices.DrawChart(this.WebChartControl2, "收益", ViewType.Line, GetWeekMoneyAndCost(), "TYPE", "Z_SCORE"); ChartServices.DrawChart(this.WebChartControl2, "成本", ViewType.Line, GetWeekMoneyAndCost(), "TYPE", "ANN"); } /// <summary> /// 绘制饼图 /// </summary> private void DrawPie() { ChartServices.SetChartTitle(this.WebChartControl4, true, "2012年12月第1周收入情况", true, 2, StringAlignment.Center, ChartTitleDockStyle.Top, true, new Font("宋体", 12, FontStyle.Bold), Color.Red, 10); ChartServices.DrawChart(this.WebChartControl4, GetWeekMoneyAndCost().Rows[0][0].ToString(), ViewType.Pie, GetWeekMoneyAndCost(), "TYPE", "Z_SCORE"); ChartServices.SetChartTitle(this.WebChartControl6, true, "2012年12月第1周收入情况", true, 2, StringAlignment.Center, ChartTitleDockStyle.Top, true, new Font("宋体", 12, FontStyle.Bold), Color.Red, 10); ChartServices.DrawChart(this.WebChartControl6, GetWeekMoneyAndCost().Rows[0][0].ToString(), ViewType.Pie, GetWeekMoneyAndCost(), "TYPE", "ANN"); } } }