asp.net 如何创建堆积柱形图?

ujv3wf0j  于 2022-12-30  发布在  .NET
关注(0)|答案(1)|浏览(145)

I have following SQL Server Result. How to create a stacked column chart with asp.net 4 chart control?

Hospital     September    October   April
OMD             10          20       15
IRH             15          16        18

大概是这样的

4c8rllxm

4c8rllxm1#

下面是一个例子:

美国邮政:

<asp:Chart ID="chart1" runat="server">
    <Series>
        <asp:Series Name="Series1" Label="September"></asp:Series>
        <asp:Series Name="Series2" Label="October"></asp:Series>
        <asp:Series Name="Series3" Label="April"></asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
    </ChartAreas>
</asp:Chart>

<asp:Button ID="Button1" runat="server" Text="Graph" onclick="Button1_Click" />

C#代码隐藏:

protected void Button1_Click(object sender, EventArgs e)
{
    // September Data
    Chart1.Series[0].Points.Add(new DataPoint(0, 10));
    Chart1.Series[0].Points.Add(new DataPoint(1, 15));

    // October Data
    Chart1.Series[1].Points.Add(new DataPoint(0, 20));
    Chart1.Series[1].Points.Add(new DataPoint(1, 16));

    // April Data
    Chart1.Series[2].Points.Add(new DataPoint(0, 15));
    Chart1.Series[2].Points.Add(new DataPoint(1, 18));

    foreach (Series cs in chart1.Series)
        cs.ChartType = SeriesChartType.StackedColumn;
}

或者使用SQL数据源ds,您可以直接执行以下操作:

DataTable dt = new DataTable();
dt = ds.Tables[0];

DataView dv = new DataView(dt);

chart1.DataSource = dv;
chart1.DataBind();

foreach (Series cs in chart1.Series)
    cs.ChartType = SeriesChartType.StackedColumn;

相关问题