I have been working hard to learn how to create doughnut chart in asp web application, I had to use a different method to create doughnut chart. However I am trying to learn if it is possible to display COUNT data from multiple tables in the chart?
I have 4 tables (Table1, Table2, Table3 and Table4) that display different data. I want to count the records that are there in each Table and display in the chart.
I need to count the total number of records inside Table1; I also count the records inside Table2, Table3 and Table4 “based on user”. Then I can display the count in doughnut chart in percentage (%)
For example:
If I count the records in Table1, and its 40; then when Table is counted it is 187, Table3 has 80 and Table4 has 300. Then I display the numbers for each table in the chart.
Below is the Code I used.
N.B. - ONLY A SINGLE TABLE IS USED FOR THIS CHART. BUT I WANT TO ACHIEVE IT WITH MULTIPLE TABLES. THERE IS ALSO STORED PROCEDURE IN THE BELOW CODE. BUT I DON’T WANT TO USE STORED PROCEDURE FOR THIS PARTICULAR TASK.
Please I need great help on this; I will greatly appreciate your help. Thank you
HTML
<asp:Chart ID="Chart1" runat="server" BackGradientStyle="LeftRight"
BorderlineWidth="0" Palette="None" PaletteCustomColors="steelblue">
<Titles>
<asp:Title ShadowOffset="10" Name="Items" />
</Titles>
<Legends>
</Legends>
<Series>
<asp:Series Name="Default" />
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="0" />
</ChartAreas>
</asp:Chart>
protected void Page_Load(object sender, EventArgs e)
{
Bindchart();
}
private void Bindchart()
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
con.Open();
cmd = new SqlCommand("GetSaleData", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable ChartData = ds.Tables[0];
//storing total rows count to loop on each Record
string[] XPointMember = new string[ChartData.Rows.Count];
int[] YPointMember = new int[ChartData.Rows.Count];
for (int count = 0; count < ChartData.Rows.Count; count++)
{
//storing Values for X axis
XPointMember[count] = ChartData.Rows[count]["Items"].ToString();
//storing values for Y Axis
YPointMember[count] = Convert.ToInt32(ChartData.Rows[count]["Figures"]);
}
//binding chart control
Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);
//Setting width of line
Chart1.Series[0].BorderWidth = 10;
//setting Chart type
Chart1.Series[0].ChartType = SeriesChartType.Doughnut;
foreach (Series charts in Chart1.Series)
{
foreach (DataPoint point in charts.Points)
{
switch (point.AxisLabel)
{
case "Q1": point.Color = Color.Maroon; break;
case "Q2": point.Color = Color.Indigo; break;
case "Q3": point.Color = Color.Red; break;
case "Q4": point.Color = Color.Green; break;
}
point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
}
}
con.Close();
}