ASP:Chart control - databind pie chart from datatable

iamjonesy picture iamjonesy · Apr 16, 2010 · Viewed 16.3k times · Source

I've got a datatable with two columns "Status" (string) and "Total" (integer).

 Status       Total
 Success      34
 Missing      2
 Failed       10

I want to databind this into a pie chart with each Status per slice but I'm not sure what method of data binder is required?

Thanks, Jonesy

Answer

Chris Van Opstal picture Chris Van Opstal · Apr 16, 2010

Give this a shot:

    DataTable dt = new DataTable();
    dt.Columns.Add("Status");
    dt.Columns.Add("Total");

    dt.Rows.Add("Success", 34);
    dt.Rows.Add("Missing", 2);
    dt.Rows.Add("Failed", 10);

    Chart1.DataSource = dt;
    Chart1.Series["Series1"].XValueMember = "Status";
    Chart1.Series["Series1"].YValueMembers = "Total";
    Chart1.DataBind();

Update: The easiest way to add a legend is probably on the client side:

<Legends>
    <asp:Legend ... />
</Legends>

You can also add it programmatically:

    Chart1.Legends.Add("myLegend");