Aspose Slides Java.. Color each series point

Hi Aspose Team,


I am using Aspose.Slides for Java, I am using a ChartTypeEx.StackedColumn chart. I want to color each data point of a series with different color, Could you please help me doing that with an example. I tried the below but getting NullPointerException.

ChartPointEx chartPointEx = new ChartPointEx(series);
chartPointEx.setIndex(0);
chartPointEx.getFormat().getFill().getSolidFillColor().setColor(new java.awt.Color(com.aspose.slides.PresetColorEx.LightGray));

Appreciate your immediate reply.

Thanks,
Sravanthi

Hi Sravanthi,


Thanks for inquiring Aspose.Slides.

I have observed the requirements shared by you and suggest you to please try using the sample code shared over this link. Please also note that instead of using PresetColorEx.LightGray, please try using java.awt.Color.GRAY. The PresetColorEx seems to have some issues at the moment and this will get resolved in upcoming merged release of Aspose.Slides for Java.

Many Thanks,

I did see this link, but could not find a way to change the color of fill in each bar of the Stacked Column chart.

Hi,


I have created the sample code to serve the purpose for your needs. Please try using the following sample code to serve the purpose.

public static void AddNewChart()
{
//Instantiate PresentationEx class that represents PPTX file
PresentationEx pres = new PresentationEx();

//Access first slide
SlideEx sld = pres.getSlides().get_Item(0);

// Add chart with default data
ChartEx chart = sld.getShapes().addChart(ChartTypeEx.ClusteredColumn, 0, 0, 500, 500);

//Setting chart Title
chart.getChartTitle().getText().setText(“Sample Title”);
chart.getChartTitle().getText().setCenterText(true);
chart.getChartTitle().setHeight(20f);
chart.hasTitle(true);

//Set first series to Show Values
chart.getChartData().getSeries().get_Item(0).getLabels().setShowValue(true);


//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

//Getting the chart data worksheet
ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory();

//Delete default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
int s = chart.getChartData().getSeries().getCapacity();

//Adding new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, “Series 1”), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, “Series 2”), chart.getType());

//Adding new categories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, “Caetegoty 1”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, “Caetegoty 2”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, “Caetegoty 3”));

//Take first chart series
ChartSeriesEx series = chart.getChartData().getSeries().get_Item(0);

//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 1, 30));

//Setting fill color for series
series.getFormat().getFill().setFillType(FillTypeEx.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(Color.RED);

//Take second chart series
series = chart.getChartData().getSeries().get_Item(1);

//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 2, 60));

//Setting fill color for series
series.getFormat().getFill().setFillType(FillTypeEx.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(Color.GREEN);

//create custom lables for each of categories for new series

//first label will be show Category name
DataLabelEx lbl = new DataLabelEx(series);
lbl.setShowCategoryName( true);
lbl.setId (0);
series.getLabels().add(lbl);

//Show series name for second label
lbl = new DataLabelEx(series);
lbl.setShowSeriesName(true);
lbl.setId(1);
series.getLabels().add(lbl);

//show value for third label
lbl = new DataLabelEx(series);
lbl.setShowValue(true);
lbl.setShowSeriesName(true);
lbl.setSeparator("/");
lbl.setId( 2);
series.getLabels().add(lbl);

// show value and custom text
lbl = new DataLabelEx(series);
lbl.getTextFrame().setText( “My text”);
lbl.setId( 3);
series.getLabels().add(lbl);

// Save presentation with chart
pres.write(“C:\Presentations\AsposeChart.pptx”);
}


However, if you ought to change the color of individual dataPoint for any particular series then I regret to share that this support is presently unavailable in this API. However, this support will be available in new merged API which will be released by end of next week.


Many Thanks,

Hi Mudassir,

Many thanks for your reply. Will look forward for an update on changing the color of each Datapoint in a particular series.

I have few more questions which I unable to achieve.

  1. Decrease the font size of the data labels.

  2. Decrease the font size of a particular data label.

Below is the code I am using. Also attached a zip file having expected and actual PPT.

public static void main(String[] args) {

//Instantiate PresentationEx class that represents PPTX file

PresentationEx pres = new PresentationEx();

//Access first slide

SlideEx slide = pres.getSlides().get_Item(0);

// Add chart with default data

ChartEx chart = slide.getShapes().addChart(ChartTypeEx.StackedColumn, 0, 0, 500, 500);

//Setting chart Title

chart.getChartTitle().getText().setText(“Chart Title”);

chart.getChartTitle().getText().setCenterText(true);

chart.getChartTitle().setHeight(20f);

chart.hasTitle(true);

//Set first series to Show Values

((ChartSeriesEx) chart.getChartData().getSeries().get_Item(0)).getLabels().setShowValue(true);

//Setting the index of chart data sheet

int defaultWorksheetIndex = 0;

//Getting the chart data worksheet

ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory();

//Delete default generated series and categories

chart.getChartData().getSeries().clear();

chart.getChartData().getCategories().clear();

//Adding new series

chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, “CumulativeTotal”), chart.getType());

chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, “Decomp”), chart.getType());

//Adding new categories

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, “Year Ago”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, “Unmodeled Product”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, “Seasonality and Holiday”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 4, 0, “Unemployment”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 5, 0, “Cannibalization From Trade”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 6, 0, “Base Price Gap”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 7, 0, “New Product”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 8, 0, “Unexplained”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 9, 0, “Item Distribution”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 10, 0, “Competitive Trade”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 11, 0, “Trend”));

chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 12, 0, “Current”));

//Take first chart series

ChartSeriesEx totalSeries = (ChartSeriesEx) chart.getChartData().getSeries().get_Item(0);

//Now populating series data

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 1, 10000));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 1, 10000));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 1, 11000));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 4, 1, 13000));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 5, 1, 14500));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 6, 1, 17500));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 7, 1, 17500));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 8, 1, 16500));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 9, 1, 14500));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 10, 1, 10500));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 11, 1, 9500));

totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 12, 1, 9000));

//Setting fill color for series

totalSeries.getFormat().getFill().setFillType(FillTypeEx.Solid);

totalSeries.getFormat().getFill().getSolidFillColor().setColor(Color.GRAY);

//Take second chart series

ChartSeriesEx changeSeries = (ChartSeriesEx) chart.getChartData().getSeries().get_Item(1);

//Now populating series data

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 2, null));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 2, 1000));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 2, 2000));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 4, 2, 1500));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 5, 2, 3000));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 6, 2, 0));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 7, 2, 1000));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 8, 2, 2000));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 9, 2, 4000));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 10, 2, 1000));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 11, 2, 500));

changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 12, 2, null));

//Setting fill color for series

changeSeries.getFormat().getFill().setFillType(FillTypeEx.Solid);

changeSeries.getFormat().getFill().getSolidFillColor().setColor(new Color(60, 179, 113));

//Removing the legend

chart.hasLegend(false);

//Show value for start bar

DataLabelEx startDataLabel = new DataLabelEx(totalSeries);

startDataLabel.setId(0);

startDataLabel.setShowValue(true);

/* startDataLabel.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(8f); // Throwing Null Pointer REFER POINT 2
*/

totalSeries.getLabels().add(startDataLabel);

//Show value for end bar

DataLabelEx endDataLabel = new DataLabelEx(totalSeries);

endDataLabel.setId(11);

endDataLabel.setShowValue(true);

/*
endDataLabel.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(8f);
*/

//Throwing Null Pointer REFER POINT 2

totalSeries.getLabels().add(endDataLabel);

//Show value for all change bars

changeSeries.getLabels().setShowValue(true);

changeSeries.getLabels().setPosition(LegendDataLabelPositionEx.Center);

/*
changeSeries.getLabels().getTextProperties().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(8f);
*/

//Throwing Null Pointer REFER POINT 1

// Save presentation with chart

int suffix = (int)Math.floor(Math.random()*100);

pres.write(“E:\AsposeChart_” + suffix + “.pptx”);

}

I am using aspose.slides-3.0.0.jar for Java 6.

Hi,

I have observed the expected presentation file shared by you and regret to share that at the moment setting the fill color for individual series point inside series is unavailable in Aspose.Slides. However, you can alter the font related properties of chart series labels. Please use the following sample code to serve the purpose.

public static void testChartLabels()
{
//Instantiate PresentationEx class that represents PPTX file
PresentationEx pres = new PresentationEx();

//Access first slide
SlideEx slide = pres.getSlides().get_Item(0);

// Add chart with default data
ChartEx chart = slide.getShapes().addChart(ChartTypeEx.StackedColumn, 0, 0, 500, 500);

//Setting chart Title
chart.getChartTitle().getText().setText(“Chart Title”);
chart.getChartTitle().getText().setCenterText(true);
chart.getChartTitle().setHeight(20f);
chart.hasTitle(true);

//Set first series to Show Values
((ChartSeriesEx) chart.getChartData().getSeries().get_Item(0)).getLabels().setShowValue(true);

//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;

//Getting the chart data worksheet
ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory();

//Delete default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();

//Adding new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, “CumulativeTotal”), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, “Decomp”), chart.getType());

//Adding new categories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, “Year Ago”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, “Unmodeled Product”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, “Seasonality and Holiday”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 4, 0, “Unemployment”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 5, 0, “Cannibalization From Trade”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 6, 0, “Base Price Gap”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 7, 0, “New Product”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 8, 0, “Unexplained”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 9, 0, “Item Distribution”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 10, 0, “Competitive Trade”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 11, 0, “Trend”));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 12, 0, “Current”));
//Take first chart series
ChartSeriesEx totalSeries = (ChartSeriesEx) chart.getChartData().getSeries().get_Item(0);

//Now populating series data
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 1, 10000));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 1, 10000));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 1, 11000));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 4, 1, 13000));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 5, 1, 14500));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 6, 1, 17500));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 7, 1, 17500));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 8, 1, 16500));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 9, 1, 14500));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 10, 1, 10500));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 11, 1, 9500));
totalSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 12, 1, 9000));

//Setting fill color for series
totalSeries.getFormat().getFill().setFillType(FillTypeEx.Solid);
totalSeries.getFormat().getFill().getSolidFillColor().setColor(Color.GRAY);
//Take second chart series
ChartSeriesEx changeSeries = (ChartSeriesEx) chart.getChartData().getSeries().get_Item(1);

//Now populating series data
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 2, null));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 2, 1000));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 2, 2000));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 4, 2, 1500));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 5, 2, 3000));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 6, 2, 0));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 7, 2, 1000));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 8, 2, 2000));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 9, 2, 4000));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 10, 2, 1000));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 11, 2, 500));
changeSeries.getValues().add(fact.getCell(defaultWorksheetIndex, 12, 2, null));

//Setting fill color for series
changeSeries.getFormat().getFill().setFillType(FillTypeEx.Solid);
changeSeries.getFormat().getFill().getSolidFillColor().setColor(new Color(60, 179, 113));
//Removing the legend
chart.hasLegend(false);
//Show value for start bar
DataLabelEx startDataLabel = new DataLabelEx(totalSeries);
setLabelFont(startDataLabel.getTextProperties(),8.0f, “Arial”);
// setLabelFont(startDataLabel.getTextFrame(),8.0f, “Arial”);

startDataLabel.setId(0);
startDataLabel.setShowValue(true);
/*
startDataLabel.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(8f); //Throwing Null Pointer REFER POINT 2
*/
totalSeries.getLabels().add(startDataLabel);
//Show value for end bar
DataLabelEx endDataLabel = new DataLabelEx(totalSeries);
//setLabelFont(endDataLabel);

endDataLabel.setId(11);
endDataLabel.setShowValue(true);

setLabelFont(endDataLabel.getTextProperties(),8.0f, “Arial”);

//Throwing Null Pointer REFER POINT 2
totalSeries.getLabels().add(endDataLabel);
//Show value for all change bars
changeSeries.getLabels().setShowValue(true);
changeSeries.getLabels().setPosition(LegendDataLabelPositionEx.Center);
/*
changeSeries.getLabels().getTextProperties().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setFontHeight(8f);
//Throwing Null Pointer REFER POINT 1
*/
int cnt=chart.getChartData().getSeries().size();
setLabelFont(totalSeries.getLabels().getTextProperties(),8.0f, “Arial”);
setLabelFont(changeSeries.getLabels().getTextProperties(),8.0f, “Arial”);

// Save presentation with chart
int suffix = (int)Math.floor(Math.random()*100);
pres.write(“D:\Aspose Data\AsposeChart_” + suffix + “.pptx”);
}

public static void setLabelFont(TextFrameEx textFrame, float fontHeight,String font)
{

PortionFormatEx porFormat =textFrame.getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat();
porFormat.setFontHeight(fontHeight);
porFormat.setLatinFont(new FontDataEx(font));
}

In the example, I have changed the all labels as well as individual label fonts. Please share, if I may help you further in this regard.

Many Thanks,

Hi Mudassir,


Thanks a lot for working on this and giving me a sample code. Well, even this gave exceptions with the version I am using. When I tried with the latest evaluation version it went fine and I got the expected result.

Regards,
Sravanthi

Hi Sravanthi,


That is really appreciable that things have started to work on your end. Please share, if I may help you further in this regard.

Many Thanks,