Bind PieChart Using IDataReader via Stored Procedure

Posted: October 11, 2011 in ASP.Net Microsoft Chart

You can bind a PieChart using a stored procedure and IDataReader as follows:
ASPX

<asp:Chart id="MyChart" ImageStorageMode="UseImageLocation" runat="server" Height="350px" Width="675px" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" EnableViewState="true">
  <titles><asp:Title Font="Trebuchet MS, 9.25pt, style=Bold" Text="Title" Name="MyChartTitle" </asp:Title></titles>
  <titles><asp:Title Font="Trebuchet MS, 8.25pt, style=Bold" Text="Sub Title" Name="MyChartSubTitle"></asp:Title></titles>
  <legends><asp:Legend Alignment="Center" Docking="Right" Font="Trebuchet MS, 8.25pt, style=Bold" IsTextAutoFit="false" Name="Default" LegendStyle="Column"></asp:Legend></legends>
  <series>
    <asp:Series Name="Default" ChartType="Pie" XValueMember="XVal" YValueMembers="YVal" Font="Trebuchet MS, 7.25pt, style=Bold"></asp:Series>  
  </series>
  <chartareas>
    <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
  </chartareas>
</asp:Chart>  

Code Behind

try
{
  Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase DAL = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(MyConnectionString);
  System.Data.Common.DbCommand dbgCommand = DAL.GetStoredProcCommand("MyStoredProcedure");
  IDataReader rdr = DAL.ExecuteReader(dbgCommand);

  try
  {
    DataTable dt = new DataTable();
    dt.Load(rdr);

    if (dt.Rows.Count != 0)
    {
      MyChart.DataSource = dt;
      MyChart.DataBind();
      MyChart.Series["Default"].ChartType = SeriesChartType.Pie;
      MyChart.Series[0]["PieDrawingStyle"] = "SoftEdge";
      MyChart.Series["Default"]["PieLabelStyle"] = "Disabled";
      MyChart.Series["Default"].Points[0]["Exploded"] = "true";
      MyChart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
      MyChart.Legends[0].Enabled = false;
      MyChart.Titles[0].Text = "Title";
      MyChart.Titles[1].Text = "Sub Title";
      MyChart.Series["Default"]["PieLabelStyle"] = "Outside";
    }
    else
    {
      ...
    }
  }
  finally
  {
    rdr.Close();
    rdr.Dispose();
  }
}
catch (Exception ex)
{
  ...
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s