Free Support Forum - aspose.com

Getting rid of the white background for datalabels

Hi,
I am struggling to get the white background that comes on all the data labels displayed in the attached chart.
Am using Apose.Cells for Java
i have tried various settings fo the datalabel object. but it just does not go away.
please tell me what mistake i am making.

my code snippet is something like this:
private static Color DEFAULT_BACKGROUND_COLOR = new Color(0xE0,0xE6,0xF8);

static void printChart(Chart a_chart, boolean w_displayDataLabels)
{

//first set the color for the plotarea
a_chart.getPlotArea().getArea().setForegroundColor(DEFAULT_BACKGROUND_COLOR);
//now display the series…

//now display the datalabels for this series
if (w_displayDataLabels) {
w_nSeries.get(w_seriesCount).getDataLabels().getArea().setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
w_nSeries.get(w_seriesCount).getDataLabels().setBackgroundMode(BackgroundMode.TRANSPARENT);
w_nSeries.get(w_seriesCount).getDataLabels().setValueShown(true);
w_nSeries.get(w_seriesCount).getDataLabels().setLabelPosition((byte)0);
}

NOTE: if i comment the setting of foregroundcolor for a_chart.getPlotArea, it shows me a grey background but the datalabels background is no longer white.
But i want to set my plotarea color to DEFAULT_BACKGROUND_COLOR
attaching both outputs (first bitmap with the issue and 2nd bitmap without calling chart.getPlotArea.setForeground).


Hi,

Thanks for your posting and using Aspose.Cells for Java.

Please download and use the latest version: Aspose.Cells
for Java v7.3.4.3


Please set the BackgroundMode property to Automatic and see if it resolves your issue. I think, you do not need to use sub-properties of Area

If you still find any problem, then please post your sample but simple runnable code and the actual and expected output xls/xlsx files. We will look into your issue and help you asap.

Please see the following modified code for your help.

Java
//first set the color for the plotarea
a_chart.getPlotArea().getArea().setForegroundColor(DEFAULT_BACKGROUND_COLOR);
//now display the series…

//now display the datalabels for this series
if (w_displayDataLabels) {
//w_nSeries.get(w_seriesCount).getDataLabels().getArea().setBackgroundColor(DEFAULT_BACKGROUND_COLOR);
w_nSeries.get(w_seriesCount).getDataLabels().setBackgroundMode(BackgroundMode.AUTOMATIC);
w_nSeries.get(w_seriesCount).getDataLabels().setValueShown(true);
w_nSeries.get(w_seriesCount).getDataLabels().setLabelPosition((byte)0);

Hi,
Setting Backgroundmode to Automatic did not work either.
Attaching the sample code.
I am on an older version. But please help me out.
PFA the sample code below

public static void testAspose() throws Exception
{
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Adding a new worksheet to the Excel object
int sheetIndex = workbook.getWorksheets().addSheet().getIndex();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.getWorksheets().getSheet(sheetIndex);
//Adding a sample value to “A1” cell
worksheet.getCells().getCell(“A1”).setValue(50);
//Adding a sample value to “A2” cell
worksheet.getCells().getCell(“A2”).setValue(100);
//Adding a sample value to “A3” cell
worksheet.getCells().getCell(“A3”).setValue(150);
//Adding a sample value to “B1” cell
worksheet.getCells().getCell(“B1”).setValue(4);
//Adding a sample value to “B2” cell
worksheet.getCells().getCell(“B2”).setValue(20);
//Adding a sample value to “B3” cell
worksheet.getCells().getCell(“B3”).setValue(50);
//Adding a chart to the worksheet
Chart chart = worksheet.getCharts().addChart(ChartType.BAR_CLUSTERED, 5, 0, 25, 5);
chart.getPlotArea().getArea().setForegroundColor(new Color(0xE0,0xE6,0xF8));
//Accessing the instance of the newly added chart
//Adding NSeries (chart data source) to the chart ranging from “A1” cell to “B3”
chart.getNSeries().add(“A1:B3”, true);
NSeries w_nSeries = chart.getNSeries();
ASeries w_aSeries = w_nSeries.get(0);
w_aSeries.getDataLabels().setValueShown(true);
w_aSeries.getDataLabels().setBackgroundMode(BackgroundMode.AUTOMATIC);
w_aSeries.getDataLabels().setLabelPosition((byte)0);
//Set the max value of value axis
chart.getValueAxis().setMaxValue(200);
//Set the min value of value axis
chart.getValueAxis().setMinValue(0);
//Set the major unit
chart.getValueAxis().setMajorUnit(25);
//Category(X) axis crosses at the maxinum value.
//chart.getValueAxis().setCrosses(CrossType.MAXIMUM);
//Set he number of categories or series between tick-mark labels.
chart.getCategoryAxis().setTickLabelSpacing(2);
//Saving the Excel file
workbook.save(“C:\temp\book1.xls”);
}

Hi,

Please see the following code. I have attached the output file. It is working fine.

Your code is obsolete. So I have upgraded your code according to the latest version.

Please see the screenshot below for your reference.

Java


String dirPath = “F:/Shak-Data-RW/Downloads/”;


//Instantiating a Workbook object

Workbook workbook = new Workbook();


//Obtaining the reference of the newly added worksheet by passing its sheet index

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


//Adding a sample value to “A1” cell

worksheet.getCells().get(“A1”).setValue(50);

//Adding a sample value to “A2” cell

worksheet.getCells().get(“A2”).setValue(100);

//Adding a sample value to “A3” cell

worksheet.getCells().get(“A3”).setValue(150);

//Adding a sample value to “B1” cell

worksheet.getCells().get(“B1”).setValue(4);

//Adding a sample value to “B2” cell

worksheet.getCells().get(“B2”).setValue(20);

//Adding a sample value to “B3” cell

worksheet.getCells().get(“B3”).setValue(50);

//Adding a chart to the worksheet


int chartIndex = worksheet.getCharts().add(ChartType.BAR, 5, 0, 25, 5);


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


chart.getPlotArea().getArea().setForegroundColor(Color.fromArgb(0xE0,0xE6,0xF8));

//Accessing the instance of the newly added chart

//Adding NSeries (chart data source) to the chart ranging from “A1” cell to “B3”

chart.getNSeries().add(“A1:B3”, true);

SeriesCollection w_nSeries = chart.getNSeries();

Series w_aSeries = w_nSeries.get(0);

w_aSeries.getDataLabels().setValueShown(true);

w_aSeries.getDataLabels().setBackgroundMode(BackgroundMode.AUTOMATIC);

w_aSeries.getDataLabels().setPosition(0);

//Set the max value of value axis

chart.getValueAxis().setMaxValue(200);

//Set the min value of value axis

chart.getValueAxis().setMinValue(0);

//Set the major unit

chart.getValueAxis().setMajorUnit(25);

//Category(X) axis crosses at the maxinum value.

//chart.getValueAxis().setCrosses(CrossType.MAXIMUM);

//Set he number of categories or series between tick-mark labels.

chart.getCategoryAxis().setTickLabelSpacing(2);

//Saving the Excel file

workbook.save(dirPath + “output.xls”, SaveFormat.EXCEL_97_TO_2003);

Screenshot: