Pie Charts

I am trying to implement a Pie Chart. I can get all of the other charts to work, except Pie Charts. I am wondering if I could get some sample code. Perhaps I am missing something in my code. I could only find sample code for a Bar Chart in the sample code.

Thanks
Pam

Dear Pam,

Thanks for your consideration. Please try the fowllowing code:

Excel excel = new Excel();
Worksheet sheet = excel.Worksheets[0];
Cells cells = sheet.Cells;
cells[“A1”].PutValue(1);
cells[“B1”].PutValue(2);
cells[“C1”].PutValue(3);

int chartIndex = sheet.Charts.Add(ChartType.Pie, 3, 3, 8, 6);
Chart chart = sheet.Charts[chartIndex];
chart.NSeries.Add(“a1:c1”, false);

excel.Save(“chart.xls”, SaveType.OpenInExcel, FileFormatType.Default, this.Response);

This worked for me. However, in my example, suppose I have 2 columns of data, where 1 column is the ‘label’ and the 2nd column contains the values for the labels. That is where I am stuck. Currently, I pass in the left,uppermost cell (A1) and the right, bottommost cell (B5).

so I changes this line in your example

chart.NSeries.Add(“a1:c1”, false);

to

chart.NSeries.Add(“a1:b5”, false);

I can’t get it to graph in the pie chart properly.

Thanks for all your help.

Pam

OK, I got this figured out. Thanks for your pointers!

Pam

@pamvcl,
Aspose.Excel is replaced with much more advanced library Aspose.Cells that also supports creating the pie chart. Give a try to the following sample code which creates Pie chart using the latest version of Aspose.Cells library.

    //Create Aspose.Cells Workbook
    
    Workbook workbook = new Workbook();
 
    //Access Aspose.Cells Worksheet
    Worksheet sheet = workbook.Worksheets[0];
 
    //Add sample data for pie chart
    //Add headings in A1 and B1
    sheet.Cells["A1"].PutValue("Products");
    sheet.Cells["B1"].PutValue("Users");
 
    //Add data from A2 till B4
    sheet.Cells["A2"].PutValue("Aspose.Cells");
    sheet.Cells["B2"].PutValue(10000);
    sheet.Cells["A3"].PutValue("Aspose.Slides");
    sheet.Cells["B3"].PutValue(8000);
    sheet.Cells["A4"].PutValue("Aspose.Words");
    sheet.Cells["B4"].PutValue(12000);
 
    //Chart reference
    Chart productsChart;
 
    //Add a Pie Chart
    int chartIdx = sheet.Charts.Add(ChartType.Pie, 7, 0, 20, 6);
    productsChart = sheet.Charts[chartIdx];
 
    //Gets the cells that define the data to be charted
    int seriesIdx = productsChart.NSeries.Add("=Sheet1!$B$2:$B$4", true);
    Series nSeries = productsChart.NSeries[seriesIdx];
    nSeries.XValues = "=Sheet1!$A$2:$A$4";
 
    //Set chart title
    productsChart.Title.Text = "Users";
 
    //Autofit first column
    sheet.AutoFitColumn(0);
 
    //Save the copy of workbook as OutputAspose.xlsx
    workbook.Save("C:\\Downloads\\OutputAspose.xlsx");

For more details have a look at the following article:
Create a Pie Chart

For working on all types of charts follow the documents section below:
Charts

Download the latest version of Aspose.Cells for .NET from the following link:
Aspose.Cells for .NET (Latest Version)

You can download the latest demos here.