We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to set Pie Chart's data area

I am trying to change Excel Automation to Aspose, could you please help me with this?

//Series oSeries = ((Series)((ChartObject)ws.ChartObjects(1)).Chart.SeriesCollection(1));
//oSeries.XValues = ws.get_Range(ws.Cells[25, 12], ws.Cells[25 + index, 12]);
//oSeries.Values = ws.get_Range(ws.Cells[25, 13], ws.Cells[25 + index, 13]);

How can it be converted to Aspose? Thanks.

Hi,

Following can be the parallel Aspose.Cells code to yours:

//Series oSeries = ((Series)((ChartObject)ws.ChartObjects(1)).Chart.SeriesCollection(1));
//oSeries.XValues = ws.get_Range(ws.Cells[25, 12], ws.Cells[25 + index, 12]);
//oSeries.Values = ws.get_Range(ws.Cells[25, 13], ws.Cells[25 + index, 13]);

//Create chart
int chartIndex = 0;
chartIndex = ws.Charts.Add(ChartType.Pie, 9, 1, 35, 11);
Chart chart = ws.Charts[chartIndex];

//.........

//Add a data series and specify its Values
ASeries oSeries = chart.NSeries[chart.NSeries.Add(ws.Cells[24, 12].Name + ":" + ws.Cells[24+index, 12].Name, true)];
//Add category data / XValues for the data series
oSeries.XValues = ws.Cells[24, 11].Name + ":" + ws.Cells[24 + index, 11].Name;
//Or you can set CategoryData as follows
//chart.NSeries.CategoryData = ws.Cells[24, 11].Name + ":" + ws.Cells[24 + index, 11].Name;

I write here a fill fledge example if it can help you to understand the APIs a bit.

Example code:

//Create a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet.
Worksheet ws = workbook.Worksheets[0];
//Set the name of worksheet
ws.Name = "PieChart";
//Get the cells collection in the sheet.
Cells cells = workbook.Worksheets[0].Cells;
//Put some values into a cells of the Data sheet.
cells["A1"].PutValue("Region");
cells["A2"].PutValue("France");
cells["A3"].PutValue("Germany");
cells["A4"].PutValue("England");
cells["A5"].PutValue("Sweden");
cells["A6"].PutValue("Italy");
cells["A7"].PutValue("Spain");
cells["A8"].PutValue("Portugal");
cells["B1"].PutValue("Sale");
cells["B2"].PutValue(70000);
cells["B3"].PutValue(55000);
cells["B4"].PutValue(30000);
cells["B5"].PutValue(40000);
cells["B6"].PutValue(35000);
cells["B7"].PutValue(32000);
cells["B8"].PutValue(10000);

//Create chart
int chartIndex = 0;
chartIndex = ws.Charts.Add(ChartType.Pie, 9, 1, 35, 11);
Chart chart = ws.Charts[chartIndex];

int index = 6;
//Add a data series and specify its Values
ASeries series = chart.NSeries[chart.NSeries.Add(ws.Cells[1, 1].Name + ":" + ws.Cells[1+index, 1].Name, true)];
//Add category data / XValues for the data series
series.XValues = ws.Cells[1, 0].Name + ":" + ws.Cells[1 + index, 0].Name;
//Or you can set CategoryData as follows
//chart.NSeries.CategoryData = ws.Cells[1, 0].Name + ":" + ws.Cells[1 + index, 0].Name;

//Also, you may set the dataseries like this
//Set properties of nseries
//chart.NSeries.Add("B2:B8", true);
//chart.NSeries.CategoryData = "A2:A8";

//Set properties of chart title
chart.Title.Text = "Sales By Region";
chart.Title.TextFont.Color = Color.Blue;
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Size = 12;


//Set the DataLabels in the chart
DataLabels datalabels;
for (int i = 0; i < chart.NSeries.Count; i++)
{
datalabels = chart.NSeries[i].DataLabels;
datalabels.IsValueShown = true;
datalabels.IsPercentageShown = true;


}

//Save the excel file
workbook.Save("f:\\test\\uuupie_chart1.xls", FileFormatType.Excel2003);

Thank you.