Anita Mathew Anita Mathew - 3 years ago 156
ASP.NET (C#) Question

ASP.NET Pie chart only displaying YValueMembers

I am trying to create a chart to display the percentage of checked and unchecked answer booklets.But the pie chart is only showing the unchecked value which is the YValueMember. How to resolve this?

protected void DropDown_Subjects_SelectedIndexChanged(object sender, EventArgs e)
Chart4.Visible = true;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand cmd = new SqlCommand("select checked_percent, unchecked_percent From(select COUNT(*) * 100.0 / (select count(*)from[newexam2017].[dbo].[newexam2017] where sub_code = '" + DropDown_Subjects.SelectedValue + "') as checked_percent from[newexam2017].[dbo].[newexam2017] where CheckBy is not null and sub_code = '" + DropDown_Subjects.SelectedValue + "' )checked,(select COUNT(*) * 100.0 / (select count(*)from[newexam2017].[dbo].[newexam2017] where sub_code = '" + DropDown_Subjects.SelectedValue + "')as unchecked_percent from[newexam2017].[dbo].[newexam2017] where CheckBy is null and sub_code = '" + DropDown_Subjects.SelectedValue + "')unchecked", connection);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

DataTable ChartData = ds.Tables[0];
Chart4.DataSource = ChartData;

Chart4.Series[0].Points.DataBind(ChartData.DefaultView, "checked_percent", "unchecked_percent", "");

for (int i = 0; i < Chart4.Series[0].Points.Count; i++)
Chart4.Series[0].Points[i].Label = string.Format("{0:0.00}%", ChartData.Rows[i]["checked_percent"], "{0:0.00}%", ChartData.Rows[i]["unchecked_percent"]);


asp code:

<asp:Chart ID="Chart4" runat="server" BackColor="DarkSlateBlue" BackGradientStyle="LeftRight"
BorderlineWidth="0" Height="440px" Palette="SeaGreen" PaletteCustomColors="24, 0, 0"
Width="560px" BorderlineColor="128, 128, 255" OnLoad="Chart4_Load">

<asp:Title Name="DefaultTitle" Font="Trebuchet MS, 15pt, style=Bold"
Text = "Overall Scoring Progress" />
<%-- <Legends>
<asp:Legend Name="DefaultLegend" Enabled="True" Docking="Top" />

<asp:Series Name="Series1" IsValueShownAsLabel="true" YValuesPerPoint="10" ChartType="Pie"></asp:Series>
<asp:ChartArea Name="ChartArea4" >


What I want:


What I get :


Answer Source

In a pie chart, both, checked_percent and unchecked_percent, must be Y-values. Like this:

 protected void Chart4_Load(object sender, EventArgs e)
    Chart4.Series[0].Points.Add(new DataPoint(0, (double)ChartData.Rows[0]["unchecked_percent"]));
    Chart4.Series[0].Points.Add(new DataPoint(1, (double)ChartData.Rows[0]["checked_percent"]));

enter image description here

EDIT: Including custom labels:

enter image description here

protected void Chart4_Load(object sender, EventArgs e)
    DataPoint dp = new DataPoint(0, (double)ChartData.Rows[0]["unchecked_percent"]);
    dp.Label = string.Format("unchecked\n{0:0.00}%", ChartData.Rows[0]["unchecked_percent"]);

    dp = new DataPoint(1, (double)ChartData.Rows[0]["checked_percent"]);
    dp.Label = string.Format("checked\n{0:0.00}%", ChartData.Rows[0]["checked_percent"]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download