.NET Chart控件使用

1.前台,一个柱状图,一个饼状图
<asp:Chart ID="ChartBar" runat="server" Width="800px" BackColor="#FFFFCC" Palette="BrightPastel"
        BorderWidth="2" BorderColor="#cc9900">
        <Legends>
            <asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold">
            </asp:Legend>
        </Legends>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>
    <asp:Chart ID="ChartPie" runat="server" Width="400px" BackColor="#FFFFCC" Palette="BrightPastel"
        BorderWidth="0" BorderColor="#cc9900">
        <Legends>
            <asp:Legend IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold">
            </asp:Legend>
        </Legends>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1" Area3DStyle-Enable3D="true">
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>
2核心代码

private void InitData()
        {
            //获取每月销售情况
            int[] arraySell = new int[] { 10 };

            //获取月份
            string[] arrayMonths = new string[] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };

            //获取部门
            string[] arrayDeparts = new string[] { "部门1", "部门2", "部门3" };

            Series seriess = new Series("销售情况");
            seriess.BorderWidth = 3;
            seriess.ShadowOffset = 2;

            Series seriesPies = new Series("销售情况");
            seriesPies.ChartType = SeriesChartType.Pie;
            seriesPies.BorderWidth = 3;
            seriesPies.ShadowOffset = 2;

            this.ChartBar.Series.Add(seriess);
            this.ChartPie.Series.Add(seriesPies);

            Title tBar = new Title("销售情况柱状图");
            this.ChartBar.Titles.Add(tBar);

            Title tPie = new Title("销售情况饼状图");
            this.ChartPie.Titles.Add(tPie);

            this.ChartBar.ChartAreas["ChartArea1"].AxisX.Interval = 1;//x轴数据显示间隔
            this.ChartBar.ChartAreas["ChartArea1"].AxisY.Interval = 50;

            //画柱状图

            foreach (string d in arrayDeparts)
            {
                Series tempseries = new Series(string.Format("{0}", d));
                this.ChartBar.Series.Add(tempseries);

                foreach (string m in arrayMonths)
                {
                    foreach (int n in arraySell)
                    {
                        tempseries.Points.AddXY(m, n);
                    }
                }
            }

            //画饼图

            foreach (string d in arrayDeparts)
            {
                foreach (int n in arraySell)
                {
                    seriesPies.Points.AddXY(d, n);
                }
            }


            this.ChartBar.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
            this.ChartBar.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
            //背景色设置
            this.ChartBar.ChartAreas["ChartArea1"].ShadowColor = Color.Transparent;
            this.ChartBar.ChartAreas["ChartArea1"].BackColor = Color.Azure;
            this.ChartBar.ChartAreas["ChartArea1"].BackGradientStyle = GradientStyle.TopBottom;
            this.ChartBar.ChartAreas["ChartArea1"].BackSecondaryColor = Color.White;
            //X,Y坐标线颜色和大小
            this.ChartBar.ChartAreas["ChartArea1"].AxisX.LineColor = Color.Blue;
            this.ChartBar.ChartAreas["ChartArea1"].AxisY.LineColor = Color.Blue;
            this.ChartBar.ChartAreas["ChartArea1"].AxisX.LineWidth = 2;
            this.ChartBar.ChartAreas["ChartArea1"].AxisY.LineWidth = 2;
            this.ChartBar.ChartAreas["ChartArea1"].AxisY.Title = "销量";
            //中间X,Y线条的颜色设置
            this.ChartBar.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Blue;
            this.ChartBar.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Blue;
            //X.Y轴数据显示间隔
            this.ChartBar.ChartAreas["ChartArea1"].AxisX.Interval = 1;  //X轴数据显示间隔
            this.ChartBar.ChartAreas["ChartArea1"].AxisY.Interval = 50;
            //X轴线条显示间隔
            this.ChartBar.ChartAreas["ChartArea1"].AxisX.MajorGrid.Interval = 1;
        }

3.web.config

<httpHandlers>
      <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        validate="false" />
    </httpHandlers>
这一段需要手动加入,如果不添加会报System.Web.HttpException: 为 ChartImg.axd 执行子请求时出错。







最后:效果图








郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。