i am using apache poi to generate line chart in excel. I have two sets of data, and it works well, please look at picture1.
When i use only one set of data, it generate picture2, and you can see the right side of the picture, it show china, usa and japan. This is not what i want, it should show only one serial on right side. How can i fix this?
maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
java code
public static void test() throws Exception {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
String sheetName = "CountryLineChart";
XSSFSheet sheet = wb.createSheet(sheetName);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 7, 26);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText("Area-wise Top Seven Countries");
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("Country");
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("Area & Population");
XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromArray(new String[] { "china", "usa", "japan" });
XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromArray(new Double[] { 12d, 22d, 12d });
XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromArray(new Double[] { 33d, 12d, 16d });
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(countries, area);
series1.setTitle("Area", null);
series1.setSmooth(false);
series1.setMarkerStyle(MarkerStyle.STAR);
// if comment these code, it generate line chart like picture2
// ===========================================================================
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(countries, population);
series2.setTitle("Population", null);
series2.setSmooth(true);
series2.setMarkerStyle(MarkerStyle.SQUARE);
// ===========================================================================
chart.plot(data);
// Write output to an excel file
String filename = "/Users/jiangxingshang/Downloads/tmp/line-chart.xlsx";
try (FileOutputStream fileOut = new FileOutputStream(filename)) {
wb.write(fileOut);
}
}
}