Aspose Cells JAVA Set legends in Doughnut/PIE charts

Hi,
Could you please let me know how the legends are set for a doughnut/pie chart, when we give data area for the chart object.

Thanks,
Balakumar

@BalakumarSeethapathy,

Thanks for your query.

Well, in Pie/Doughnut chart, when you specify chart’s source category data while creating the chart, the legend items are set automatically and accordingly, so you you do not need to do anything. See the sample code segment for your reference:
e.g
Sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Get First Worksheet of the Workbook
Worksheet ws = workbook.Worksheets[0];
//Set Worksheet Type
ws.Type = SheetType.Chart;
//Add new Data Sheet
Worksheet data = workbook.Worksheets.Add(“Data”);
Cells cells = data.Cells;
cells[“A1”].PutValue(“A”);
cells[“A2”].PutValue(“B”);
cells[“A3”].PutValue(“C”);
cells[“B1”].PutValue(35);
cells[“B2”].PutValue(50);
cells[“B3”].PutValue(15);

        ChartCollection charts = ws.Charts;

        int index = charts.Add(ChartType.Pie, 5, 0, 15, 5);
        Chart chart = charts[index];
        chart.NSeries.Add("Data!B1:B3", true);

chart.NSeries.CategoryData = “Data!A1:A3”;
chart.NSeries[0].DataLabels.ShowPercentage = true;
chart.NSeries[0].Points[0].Area.ForegroundColor = Color.Green;
chart.NSeries[0].Points[1].Area.ForegroundColor = Color.CornflowerBlue;
chart.NSeries[0].Points[2].Area.ForegroundColor = Color.Wheat;

        workbook.Save("e:\\test\\out1.xls");

Also, see the documents in the section for your complete reference on charts:

Hope, this helps a bit.

Hi,

thanks for your help!!

Regards,
Balakumar

@BalakumarSeethapathy,

Good to know that your issue is sorted out by the suggested code segment. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Hi,

I have attached the excel sheet, where I have placed the chart data in “Data Sheet” and placed the corresponding chart in “Sheet 1”.

Through program I am not able to provide the data coordinates for the same to generate doughnut chart. I am dealing with R1C1 to provide Category data and data coordinates for the chart.

Could you please modify(Chart details roughly) the previous program given by you, generate the attached chart.

Please let me know if I am not clear.

I am trying to understand the charts here.Excel sample.zip (12.0 KB)

Thanks,
Balakumar

@BalakumarSeethapathy,

Thanks for the sample file.

There is no relevant method available for setting category data as R1C1, so you cannot do that for category data, however, there is respective method to add series data in R1C1 style though. See the following sample code for your reference:
e.g
Sample code:

		//Instantiating a Workbook object
		Workbook workbook = new Workbook();
		//Get First Worksheet of the Workbook
		Worksheet ws = workbook.getWorksheets().get(0);
		//Set Worksheet Type
		ws.setType(SheetType.CHART);
		//Add new Data Sheet
		Worksheet data = workbook.getWorksheets().add("Data");
		Cells cells = data.getCells();
		cells.get("A1").putValue("A");
		cells.get("A2").putValue("B");
		cells.get("A3").putValue("C");
		cells.get("B1").putValue(35);
		cells.get("B2").putValue(50);
		cells.get("B3").putValue(15);

		        ChartCollection charts = ws.getCharts();

		        int index = charts.add(ChartType.DOUGHNUT, 5, 0, 15, 5);
		        Chart chart = charts.get(index);
		        //chart.getNSeries().add("Data!B1:B3", true);
                        //Or
		        chart.getNSeries().addR1C1("Data!R1C2:R3C2", true);
		        
		chart.getNSeries().setCategoryData("Data!A1:A3");
			
			
		chart.getNSeries().get(0).getDataLabels().setShowPercentage(true);
		
		workbook.save("e:\\test2\\out1.xls");

Hope, this helps a bit.

Hi,

Thanks for your clarification on R1C1 in category data method, is there a way or util function which can convert R[N]C[N] to A[N]B[N] ?

That will really help me here.

Regards,
Balakumar

@BalakumarSeethapathy,

Well, you may easily convert R1C1 formula to A1 style and similarly convert A1 formula to R1C1 formatting. CellsHelper is a utility class which has respective static methods, see the sample code segment for your reference:
e.g
Sample code:


cells.get(“B3”).putValue(15);

          ChartCollection charts = ws.getCharts();

          int index = charts.add(ChartType.DOUGHNUT, 5, 0, 15, 5);
          Chart chart = charts.get(index);
          //chart.getNSeries().add("Data!B1:B3", true);
          chart.getNSeries().addR1C1("Data!R1C2:R3C2", true);
          
   
  chart.getNSeries().setCategoryData("Data!A1:A3");

String seriesval = CellsHelper.convertR1C1FormulaToA1(“Data!R1C2:R3C2”, 0, 0);
System.out.println("Series Values: " + seriesval);

String categoryval = CellsHelper.convertR1C1FormulaToA1(“Data!R1C1:R3C1”, 0, 0);
System.out.println("Category Values: " + categoryval);

  chart.getNSeries().get(0).getDataLabels().setShowPercentage(true);
  
  workbook.save("e:\\test2\\out1.xls");

Hope, this helps a bit.