Chart.toImage - Data labels set using ChartPoint.getDataLabels().setText() are not reflected in the image generated

Hi,

Please run the sample below and find the issue
"Data labels set using ChartPoint.getDataLabels().setText() are not reflected in the image generated.".

Thanks & Regards
Muhammed

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;

import com.aspose.cells.ASeries;
import com.aspose.cells.CellsHelper;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartPoint;
import com.aspose.cells.ChartPoints;
import com.aspose.cells.ChartType;
import com.aspose.cells.Color;
import com.aspose.cells.DataLabels;
import com.aspose.cells.DisplayUnitType;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.Font;
import com.aspose.cells.GradientColorType;
import com.aspose.cells.GradientDirectionType;
import com.aspose.cells.GradientFill;
import com.aspose.cells.GradientFillType;
import com.aspose.cells.GradientStyleType;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.LabelPositionType;
import com.aspose.cells.PatternFill;
import com.aspose.cells.TickLabelPositionType;
import com.aspose.cells.TrendlineType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.slides.OleObjectFrame;
import com.aspose.slides.Presentation;
import com.aspose.slides.Slide;

public class ChartPointDataLabelConflict {

public static void main(String args[]) {

System.out.println(CellsHelper.getReleaseVersion());

try {
String[] cellsName = new String[] { "B1", "B2", "B3", "B4",
"C1", "C2", "C3", "C4",
"D1", "D2", "D3", "D4",
"E1", "E2", "E3", "E4",
"F1", "F2", "F3", "F4"
};

//Array of cell data
int[] cellsValue = new int[] { 67, 86, 68, 91,
44, 64, 89, 48,
46, 97, 78, 60,
43, 29, 69, 26,
24, 40, 38, 25 };

Workbook wb = new Workbook();
Worksheet dataSheet = wb.getWorksheets().getSheet(0);
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);

dataSheet.getCells().getCell("A1").setValue("Rinse Conditioners");
dataSheet.getCells().getCell("A2").setValue("Main Wash");
dataSheet.getCells().getCell("A3").setValue("Deodrants");
dataSheet.getCells().getCell("A4").setValue("Tumble Drying Enhancer");
dataSheet.getCells().getCell("A5").setValue("Iron Enhancer");

//Populate DataSheet with data
int size = Array.getLength(cellsName);
for (int i = 0; i < size; i++) {
String cellName = cellsName[i];
int cellValue = cellsValue[i];
dataSheet.getCells().getCell(cellName).setValue(cellValue);
}

//Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();

//Add a chart in ChartSheet with data series from DataSheet
int chartWidth = 600;
int chartHeight = 500;

int chartHeightInRows = 0;
int chartWidthInCols = 0;
int cellWidth = dataSheet.getCells().getColumnWidthPixel(0);
int cellHeight = dataSheet.getCells().getRowHeightPixel(0);
chartWidthInCols = (int)(chartWidth / cellWidth);
chartHeightInRows = (int)(chartHeight / cellHeight);
chartWidth = chartWidthInCols * cellWidth;
chartHeight = chartHeightInRows * cellHeight;

Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED, 0, 0, 0, 0, chartWidth, chartHeight);
chart.getNSeries().add(dataSheetName + "!B1:F4", false);

Font font = new Font();
font.setBold(true);
font.setItalic(true);
font.setSize(10);
font.setColor(Color.BLUE);

for (int i = 0; i < chart.getNSeries().size(); i++) {
ASeries series = chart.getNSeries().get(i);

PatternFill pf = new PatternFill();
pf.setBackgroundColor(wb.getPalette().getColor(i));
pf.setForegroundColor(wb.getPalette().getColor(i));
series.getArea().setFill(pf);

series.getDataLabels().setValueShown(true);
series.getDataLabels().setRotation(90);
series.getDataLabels().setFont(font);
series.getDataLabels().setNumberFormat("###.00");
series.getDataLabels().setLabelPosition(LabelPositionType.OUTSIDE_END);

ChartPoints chartPoints = series.getChartPoints();
for (int k = 0; k < chartPoints.size(); k++) {
ChartPoint chartPoint = chartPoints.getChartPoint(k);
DataLabels dataLabels = chartPoint.getDataLabels();
dataLabels.setText("P" + k);
}
}

String categoryAreaRef = dataSheet.getName() + "!A1:A5";
chart.getNSeries().setCategoryData(categoryAreaRef);

chart.getTitle().setText("Aspose.Cells");

chart.getCategoryAxis().getTitle().setText("Category Axis");
chart.getValueAxis().getTitle().setText("Value Axis");


//Get Chart as image.
ImageOptions imgOpts = new ImageOptions();
imgOpts.setImageFormat(ImageFormat.PNG);
imgOpts.setFashion(FileFormatType.EXCEL2003);

chart.toImage(new FileOutputStream("D:\\Temp\\chart.png"), imgOpts);

wb.getWorksheets().setActiveSheet(chartSheetIdx);
wb.setOleSize(0, chartHeightInRows - 1, 0, chartWidthInCols - 1);

//Save the workbook to stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
wb.save(bout);
wb.save(new FileOutputStream("D:\\Temp\\output.xls"));

Presentation pres = new Presentation();
Slide sld = pres.getSlideByPosition(1);

int slideWidth = (int) pres.getSlideSize().getX();
int slideHeight = (int) pres.getSlideSize().getY();

int x = (slideWidth - chartWidth * 8) / 2;
int y = (slideHeight - chartHeight * 8) / 2;

OleObjectFrame oof = sld.getShapes().addOleObjectFrame(800, 500, chartWidth * 8, chartHeight * 8, "Excel.Sheet.8", bout.toByteArray());
com.aspose.slides.Picture pic = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.png"));
int picId = pres.getPictures().add(pic);
oof.setPictureId(picId);

//Write the presentation on disk
pres.write(new FileOutputStream("D:\\Temp\\output.ppt"));
System.out.println("successfully completed!!!");

}
catch (Exception e) {
e.printStackTrace();
}
}

}

Hi,

After an initial test, we found the issue. We will figure it out soon.

We have logged your issue into our issue tracking system with an issue id: CELLSJAVA-14926.

Thank you.

Hi,

Please try the attached version.

We have fixed the issue of custom text for DataLabels.

Thank you.

Hi,
It works fine for chart type COLUMN_CLUSTERED.

There are two more issues related to this.

1. Change chart type to ChartType.COLUMN_STACKED or ChartType.BAR_CLUSTERED and run the same example. (I didn't test other types. Please ask your QA to test all chart types before delivering the fix)

Chart chart = chartSheet.getCharts().addChart(ChartType.ChartType.COLUMN_STACKED, 0, 0, 0, 0, chartWidth, chartHeight);

You can see numeric value is shown in image instead of Data labels set using ChartPoint.getDataLabels().setText() .

Please see screen shot CustomDataLabelForStackedBar.PNG

2. Comment the following code in the above sample, and see that datalabels are not present in the image.

/*series.getDataLabels().setValueShown(true);
series.getDataLabels().setRotation(90);
series.getDataLabels().setFont(font);
series.getDataLabels().setNumberFormat("###.00");
series.getDataLabels().setLabelPosition(LabelPositionType.OUTSIDE_END);*/

Please see screen shot CustomDataLabelNotPresentInImage.PNG

Please note that I am using version 2.1.2.17

Thanks
Muhammed

Hi,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for the feedback.

We have found your mentioned issue after an initial test. We will look into it and get back to you soon. Your issue has been re-opened (issue id: CELLSJAVA-14926)

Thank You & Best Regards,

Hi,

Please try the attached version.

We have fixed the issue of DataLabels’ text for other Chart types and the
issue of missing DataLabels.

Thank you.

Hi,

Both issues seemes to be fixed.
I have tested for ChartType.COLUMN_STACKED or ChartType.BAR_CLUSTERED only.

For COLUMN_STACKED, one series(bottom one with black color) datalabel shows inside a white rectangle. Why is this so? Please let me know if you need screen shot.

Thanks
Muhammed

Hi,

Muhammed.Karattu:


For COLUMN_STACKED, one series(bottom one with black color) datalabel shows inside a white rectangle. Why is this so? Please let me know if you need screen shot.


Could you give us sample code and screen shots for the issue, we will check it soon.

Thank you.

Hi,

I think its because lower series is having black color.

Looks like excel behaviour. Sorry for the mistake.


Thanks
Muhammed

The issues you have found earlier (filed as 14926) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.