Saving a chart image using excel data

Hi,


I am trying to use your sample code [Export Worksheet or Chart into Image with Desired Width and Height|Documentation] which creates an image using the data provided from an excel sheet. Can you kindly explain the values that I need to put in the following line to use the data from the excel sheet attached:
int chartIndex = worksheet.getCharts().add(int type, int upperLeftRow, int upperLeftColumn, int lowerRightRow, int lowerRightColumn)

Also it will be a great help if you can guide me to create and save the chart image using the excel sheet that is attached. I have attached how the chart image will look using the data from the excel sheet.

Thanks,
Gaurang.

Hi,


Thanks for the screen shot.

Please see the sample below on how to create your desired chart and render image of the chart for your reference for your needs. I used your template file for the source range for the chart:
e.g
Sample code:

Workbook workbook = new Workbook(“chart.xlsx”);
Worksheet sheet = workbook.getWorksheets().get(0);
int index = sheet.getCharts().add(ChartType.COLUMN, 11, 1, 30, 13);
Chart chart = sheet.getCharts().get(index);
Cells cells = sheet.getCells();
chart.getNSeries().add(“Sheet1!B2:B5”, true);
chart.getNSeries().add(“Sheet1!C2:C5”, true);
chart.getNSeries().add(“Sheet1!D2:D5”, true);
chart.getNSeries().setCategoryData(“Sheet1!A2:A5”);


for (int i = 0; i < chart.getNSeries().getCount(); i++){
chart.getNSeries().get(i).setName(cells.get(0, i+1).getValue().toString());
}
chart.toImage(“out1.png”, new ImageOrPrintOptions());

Hope, this helps a bit.

Thank you.

Hi Amjad,


Thanks a lot for your quick reply. Using the code sample given by you I was able to save the chart as image. But I did not understand the meaning of the values in the code written below. Could you kindly help me to understand how the mentioned values were evaluated?
int index = sheet.getCharts().add(ChartType.COLUMN, 11, 1, 30, 13);

Thanks,
Gaurang.

Hi,



See the description of the add() overload with further comments for ChartCollection class for your complete reference:

add

public int add(int type, int upperLeftRow, int upperLeftColumn, int lowerRightRow, int lowerRightColumn)
Adds a chart to the collection.
Parameters:
type - A ChartType value. Chart type
upperLeftRow - Upper left row index. // 11 (zero based) means the chart would be drawn from 12th row in the sheet
upperLeftColumn - Upper left column index. //1 (zero based) means the chart would be drawn from the second column i.e., B

— So starting left cell/point of the chart to be drawn is B12

lowerRightRow - Lower right row index //30 (zero based) means the very first row after the end of chart on the sheet. i.e., 31st row
lowerRightColumn - Lower right column index //13 (zero based) means the very first column after the end of chart on the sheet would be i.e., 14th column in the sheet = N.

–So ending (right) cell/point of the chart is M

–So the resultant chart’s area is B12:M30


Thank you.



Hope, it helps a bit now.

Thank you.