Creating a Pie chart

Hi,

Please find the excel file attachment...

The attached excel file is our requirement

We will have a row of data in the datatable

and we need to frame the data range and category range to get the pie graph

i had displayed the pie-graph but legends are incorrect need to display class1,class2 and class3

please help me out to acheive this requirement and create the pie chart

using range name or any convinent method

Thanks,

S.N.Prasad

Hi Prasad,

Thanks for providing us the template file having your desired chart.

Please see the following sample code that creates your desired chart with Class1, Class2 and Class3 as legend entries to represent category values. I used your template file to set the data source for your desired chart, I created your desired chart in the same worksheet, you may create the chart in different worksheet for your need too.

Sample code:

Workbook workbook = new Workbook();
workbook.Open("f:\\test\\Pie_ChartRequirement.xls");
Worksheet dataSheet = workbook.Worksheets[0];
Cells cells = workbook.Worksheets[0].Cells;

//Create chart
int chartIndex = 0;
chartIndex = dataSheet.Charts.Add(ChartType.Pie, 1, 3, 25, 12);
Chart chart = dataSheet.Charts[chartIndex];

//Set properties of nseries
chart.NSeries.Add("Class-Details!C5,Class-Details!E5,Class-Details!G5", true);
chart.NSeries.CategoryData = "Class-Details!B4,Class-Details!D4,Class-Details!F4";
chart.NSeries.IsColorVaried = true;

for (int i = 0; i < chart.NSeries.Count; i++)
{
chart.NSeries[i].DataLabels.IsValueShown = true;
chart.NSeries[i].DataLabels.TextFont.Name = "Arial";
chart.NSeries[i].DataLabels.TextFont.Size = 12;
}
//Set the legend position type
chart.Legend.Position = LegendPositionType.Right;
chart.Legend.TextFont.Name = "Arial";
chart.Legend.TextFont.Size = 12;

workbook.Save("f:\\test\\mytest_piechart1.xls");

Attached is the output file for your reference.

Thank you.