Free Support Forum - aspose.com

How to Create multiple charts in same sheet of an excel

Dear Team,


As per my project requirement i need to insert multiple charts in single sheet. I am trying to do it with creating an array list of chart but i am not getting anything.
Please help me with this asap.

Thanks and Regards
Yasaswini

Hi,


Please see the following sample code on how to insert multiple charts (based on same source data) in a single worksheet. Kindly refer to the sample code and write your own code for your requirements:
e.g
Sample code:

[C#]

Workbook workbook =
new Workbook();<o:p></o:p>

Worksheet worksheet = workbook.Worksheets[0];

worksheet.Cells["A1"].PutValue("Finance");

worksheet.Cells["B1"].PutValue(5);

worksheet.Cells["A2"].PutValue("Underwriting");

worksheet.Cells["B2"].PutValue(1);

worksheet.Cells["A3"].PutValue("Advisory");

worksheet.Cells["B3"].PutValue(1);

//First chart - Pie Chart

int intChartIndex = worksheet.Charts.Add(ChartType.Pie, 0, 3, 10, 8);

Chart chart = worksheet.Charts[intChartIndex];

chart.Title.Text = "Overdue Action Points by Area Pie";

chart.Title.TextFont.Size = 9;

chart.Title.TextFont.Color = System.Drawing.Color.Black;

chart.Title.TextFont.IsBold = true;

chart.Legend.Position = LegendPositionType.Right;

chart.Legend.TextFont.Size = 9;

chart.NSeries.Add("B1:B3", true);

chart.NSeries.CategoryData = "A1:A3";

//Second chart - Column Chart

intChartIndex = worksheet.Charts.Add(ChartType.Column,12, 3, 26, 12);

chart = worksheet.Charts[intChartIndex];

chart.Title.Text = "Overdue Action Points by Area Column";

chart.Title.TextFont.Size = 10;

chart.Title.TextFont.Color = System.Drawing.Color.Black;

chart.Title.TextFont.IsBold = true;

chart.Legend.Position = LegendPositionType.Bottom;

chart.Legend.TextFont.Size = 10;

chart.NSeries.Add("B1:B3", true);

chart.NSeries.CategoryData = "A1:A3";

chart.NSeries.IsColorVaried = true;


workbook.Save(“e:\test2\out1.xlsx”);


[JAVA]

<span style=“font-size: 10pt; font-family: “Courier New”;”> Workbook workbook = <span style=“font-size:10.0pt;font-family:
“Courier New”;color:#7F0055”>new
<span style=“font-size: 10pt; font-family: “Courier New”;”> Workbook();<span style=“font-size:10.0pt;font-family:“Courier New””><o:p></o:p>

Worksheet worksheet = workbook.getWorksheets().get(0);

worksheet.getCells().get("A1").putValue("Finance");

worksheet.getCells().get("B1").putValue(5);

worksheet.getCells().get("A2").putValue("Underwriting");

worksheet.getCells().get("B2").putValue(1);

worksheet.getCells().get("A3").putValue("Advisory");

worksheet.getCells().get("B3").putValue(1);

//First chart - Pie Chart

int intChartIndex = worksheet.getCharts().add(ChartType.PIE, 0, 3, 10, 8);

Chart chart = worksheet.getCharts().get(intChartIndex);

chart.getTitle().setText("Overdue Action Points by Area Pie");

chart.getTitle().getFont().setSize(9);

chart.getTitle().getFont().setColor(Color.getBlack());

chart.getTitle().getFont().setBold(true);

chart.getLegend().setPosition(LegendPositionType.RIGHT);

chart.getLegend().getFont().setSize(9);

chart.getNSeries().add("B1:B3", true);

chart.getNSeries().setCategoryData("A1:A3");

//Second chart - Column Chart

intChartIndex = worksheet.getCharts().add(ChartType.COLUMN, 12, 3, 26, 12);

chart = worksheet.getCharts().get(intChartIndex);

chart.getTitle().setText("Overdue Action Points by Area Column");

chart.getTitle().getFont().setSize(10);

chart.getTitle().getFont().setColor(Color.getBlack());

chart.getTitle().getFont().setBold(true);

chart.getLegend().setPosition(LegendPositionType.BOTTOM);

chart.getLegend().getFont().setSize(10);

chart.getNSeries().add("B1:B3", true);

chart.getNSeries().setCategoryData("A1:A3");

chart.getNSeries().setColorVaried(true);

workbook.save("e:\\test2\\out1.xlsx");


The output file is also attached.

We also recommend you to see/browse the topics in the section for your further reference:
https://docs.aspose.com/display/cellsjava/Charts

Hope, this helps a bit.

Thank you.

Thank you Aamjad for your reply, It works fine.

Regards
Yasaswini Challa

@YaSaSwini,

Good to know that the suggested code works for your needs. Feel free to write us back if you need further help or have some other issue or queries, we will be happy to assist you soon.