How to create a stacked column chart?

Raymond Morphy picture Raymond Morphy · Jun 17, 2014 · Viewed 12.6k times · Source

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

Something like this:

enter image description here

Answer

lucidgold picture lucidgold · Jun 17, 2014

Here is an example:

ASPX:

<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# Code-Behind:

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;
}

Or with SQL Data source ds you can do the following directly:

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;