Dear Team,
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();
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]
Workbook workbook = new Workbook();
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:
Hope, this helps a bit.
Thank you.
Thank you Aamjad for your reply, It works fine.
Regards
Yasaswini Challa
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.
Hi @Amjad_Sahi can you please guide me how to get the count of charts if multiple charts is present on single sheet of excel.
You may try ChartCollection.getCount() method for your needs. See the sample line of code for your reference.
worksheet.getCharts().getCount()