【问题标题】:How can i display multi series column chart in asp.net如何在asp.net中显示多系列柱形图
【发布时间】:2016-10-13 13:28:22
【问题描述】:

在我的 Web 应用程序中,我需要显示来自数据表的柱形图数据绑定我在图表区域的 X 轴中有 3 列我需要显示 col1 一侧和第 2 列,我需要显示不同的颜色系列显示图例也来自数据表。

我需要像这样我该怎么做请任何人都可以帮助我如何做到这一点。

谢谢

【问题讨论】:

    标签: c# asp.net .net data-visualization mschart


    【解决方案1】:

    假设您的 DataTable 具有以下列/类型:

        Columns.Add("Month", typeof(DateTime));
        Columns.Add("Charges", typeof(double));
        Columns.Add("Payments", typeof(double));
    

    ASPX:

        <asp:Chart ID="Chart1" runat="server" Height="400px" Width="600px">
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1">
                    <AxisY>
                        <MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
                        <LabelStyle Format="C2" />
                    </AxisY>
                    <AxisX>
                        <MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
                    </AxisX>
                </asp:ChartArea>
            </ChartAreas>
            <Legends>
                <asp:Legend Name="Legend1">
                </asp:Legend>
            </Legends>
        </asp:Chart>
    

    CS:

        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (DataRow r in dt.Rows)
            {
                Series s = new Series(string.Format("{0} {1}", ((DateTime)r["Month"]).ToString("MMM"), ((DateTime)r["Month"]).Year));
                s.ChartType = SeriesChartType.Column;
                s.Points.AddXY("Charges", new object[] { r["Charges"] });
                s.Points.AddXY("Payments", new object[] { r["Payments"] });
    
                Chart1.Series.Add(s);
            }
        }
    


    编辑:假设您的 DataTable 具有以下列/类型:

        Columns.Add("Month", typeof(string));
        Columns.Add("Charges", typeof(double));
        Columns.Add("Payments", typeof(double));
    

        Columns.Add("Month");
        Columns.Add("Charges");
        Columns.Add("Payments");
    

    那你应该把代码改成这样:

        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (DataRow r in dt.Rows)
            {
                Series s = new Series((string)r["Month"]);
                s.ChartType = SeriesChartType.Column;
                s.Points.AddXY("Charges", new object[] { r["Charges"] });
                s.Points.AddXY("Payments", new object[] { r["Payments"] });
    
                Chart1.Series.Add(s);
            }
        }
    

    【讨论】:

    • 您的表中有哪些列类型?
    • 在我的数据表中,我已经有 Month 列和像 Aug-2016 这样的值,我怎样才能把它变成系列
    • 表中每一列的类型是什么? Month 列的类型是什么? Payments 列的类型是什么? Charges 列的类型是什么?
    • 当我为Month取列时,值08/01/2016 00:00:00在我更改后会变成这样Aug-2016,列Charge205711.7900它将更改为Rupee values和@987654342 @ 值也一样。请告诉我该怎么做我搜索了很多我没有得到的
    • 我编辑了我的答案,请看看是否适合你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2015-09-17
    相关资源
    最近更新 更多