Thursday, January 23, 2014

ASP.NET pie chart sample to set the chart area colors manually in code behind

Check out my previous sample to bind DataTable to Chart control Click Here. From the below code you can understand how to set the chart type, enable 3D chart, set the chart inclination angle, set the chart area colours manually in code behind and finally to format the lable displaying in the chart area. Here i have added the "%" symbol with label value that is displaying in the pie chart.
C# Code:
Chart1.Series["Series1"].Points.DataBind(chartData, "Key", "Value", string.Empty);
Chart1.Series["Series1"].ChartType = SeriesChartType.Pie;
Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;
Chart1.ChartAreas[0].Area3DStyle.Inclination = Convert.ToInt32(50);
foreach (Series charts in Chart1.Series)
{
     foreach (DataPoint point in charts.Points)
     {
             switch (point.AxisLabel)
             {
                      case "Name1": point.Color = Color.Green; break;
                      case "Name2": point.Color = Color.Blue; break;
                      case "Name3": point.Color = Color.Orange; break;
                      case "Name4": point.Color = Color.SandyBrown; break;
             }
             point.Label = string.Format("{0}%", point.YValues[0]);
             point.LegendText = point.AxisLabel;
     }
}

No comments:

Post a Comment