I've generated a line chart using Apache POI. I need to change the default colors I got in the chart. Can I use RGB codes to define specific colors to each line?
My code is as follows.
Drawing drawing = sheet4.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 1, 17, 22);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.RIGHT);
LineChartData data = chart.getChartDataFactory().createLineChartData();
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 0, 0));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 1, 1));
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 3, 3));
ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 4, 4));
ChartDataSource<Number> ys4 = DataSources.fromNumericCellRange(sheet1, new CellRangeAddress(1, 380, 8, 8));
LineChartSeries series1 = data.addSeries(xs, ys1);
series1.setTitle("Value 1");
LineChartSeries series2 = data.addSeries(xs, ys2);
series2.setTitle("Value 2");
LineChartSeries series3 = data.addSeries(xs, ys3);
series3.setTitle("Value 3");
LineChartSeries series4 = data.addSeries(xs, ys4);
series4.setTitle("Value 4");
chart.plot(data, bottomAxis, leftAxis);
XSSFChart xssfChart = (XSSFChart) chart;
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
plotArea.getLineChartArray()[0].getSmooth();
CTBoolean ctBool = CTBoolean.Factory.newInstance();
ctBool.setVal(false);
plotArea.getLineChartArray()[0].setSmooth(ctBool);
for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
ser.setSmooth(ctBool);
}
I am always a friend of complete examples. Then others can reproducing this and don't need guess how the
Excel
sheet might look like.The following complete example does creating a new
Excel
workbook in Office Open XML format (*.xlsx
) having a sheet containing some example data and the line chart.First code part creates a line chart how
apache poi
version3.17
creates it per default.Then in second code part, I do customizing the chart. The code parts are commented to show what they shall do. After that customizing the chart also is
OpenOffice
/Libreoffice
Calc
compatible. Theapache poi
default charts are notOpenOffice
/Libreoffice
Calc
compatible. At least up to version3.17
.For customizing one needs knowledge about the internal low level objects
apache poi
uses as basic objects. Those are contained in ooxml-schemas-1.3.jar . Unfortunately there is not more aAPI
doc public available. So one must downloadooxml-schemas-1.3-sources.jar
and then dojavadoc
one's own to get a documentation.Also one should know that files in Office Open XML format (
*.xlsx
) simply areZIP
archives. So one can unzip them and have a look at/xl/charts/chart1.xml
. When one does this afterapache poi
has created the chart and do comparing that XML with the XML after re-saving after done the wanted changings inExcel
, then one will see the needed XML changings.Example:
Result: