我想在一张 Excel 表中生成具有不同系列的多个图表。但是,我可以使用以下代码绘制一个图表,但是当我在以下代码中创建第二个 XSSFChart 图表和 XSSFClientAnchor 锚时,它无法在 Excel 中绘制图表。如何在一个excel中绘制两个或多个折线图。以下代码有什么问题?
public class MultipleChartWithThreeLines {
private static XDDFLineChartData.Series addLineSeriesToChartData(XDDFChartData chartData, XSSFSheet sheet, String categoryDataRef, String valueDataRef, String seriesTitleRef, PresetColor lineColor) {
XDDFDataSource<Double> categoryData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, CellRangeAddress.valueOf(categoryDataRef));
XDDFNumericalDataSource<Double> valueData = XDDFDataSourcesFactory.fromNumericCellRange(sheet, CellRangeAddress.valueOf(valueDataRef));
XDDFLineChartData.Series series = (XDDFLineChartData.Series) chartData.addSeries(categoryData, valueData);
series.setTitle("", new CellReference(seriesTitleRef)); // https://stackoverflow.com/questions/21855842
series.setSmooth(false); // https://stackoverflow.com/questions/29014848
// define data-point marker
series.setMarkerStyle(MarkerStyle.CIRCLE); // https://stackoverflow.com/questions/39636138
// define line color
// https://stackoverflow.com/questions/24676460
XDDFShapeProperties shapeProperties = series.getShapeProperties();
if (shapeProperties == null) {
shapeProperties = new XDDFShapeProperties();
}
shapeProperties.setLineProperties(solidLineWithColor(lineColor));
series.setShapeProperties(shapeProperties);
// if your series have missing values like https://stackoverflow.com/questions/29014848
// chart.displayBlanksAs(DisplayBlanks.GAP);
return series;
}
private static XDDFLineProperties solidLineWithColor(PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties(fill);
return line;
}
private static XDDFChartLegend addLegendToChart(XSSFChart chart) {
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.BOTTOM);
return legend;
}
private static XSSFChart createChartOnSheet(XSSFSheet sheet, int col1, int row1, int col2, int row2) {
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, col1, row1, col2, row2);
XSSFChart chart = drawing.createChart(anchor);
return chart;
}
private static XDDFChartAxis[] addAxesToChart(XSSFChart chart, String titleCategoryBottom, String titleValueLeft) {
XDDFChartAxis[] axesCatVal = new XDDFChartAxis[4];
// category axis at the bottom
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle(titleCategoryBottom); // https://stackoverflow.com/questions/32010765
axesCatVal[0] = bottomAxis;
// value axis at the left
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle(titleValueLeft);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
axesCatVal[1] = leftAxis;
return axesCatVal;
}
private static void writeWorkbookToFile(XSSFWorkbook wb, String filename) throws IOException {
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
public static void main(String[] args) throws Exception {
String workbookFilename = "e:/Graph_5.xlsx"; //"e:/Graph_5.xlsx";
// open workbook with data
XSSFWorkbook wb = new XSSFWorkbook(workbookFilename);
// draw chart with 3 lines
XSSFSheet sheet = wb.getSheetAt(0);
String sheetName = sheet.getSheetName();
System.out.println("Drawing line-chart on sheet: " + sheetName);
// create chart
XSSFChart chart = createChartOnSheet(sheet, 6, 0, 25, 15);
XSSFChart medianAngleChart = createChartOnSheet(sheet,10,17,25,15);
// add legend to chart
addLegendToChart(chart);
addLegendToChart(medianAngleChart);
// add value (left) and category (bottom) axes
XDDFChartAxis[] axesCatVal = addAxesToChart(chart, "", "Inscribed Angle"); // Add data (as Line Chart)
XDDFChartAxis[] axesCatVal_1 = addAxesToChart(medianAngleChart, "", "Median Angle"); // Add data (as Line Chart)
// add line-chart data-collection to chart
XDDFLineChartData chartData = (XDDFLineChartData) chart.createData(ChartTypes.LINE, axesCatVal[0], (XDDFValueAxis) axesCatVal[1]);
XDDFLineChartData chartData_1 = (XDDFLineChartData) medianAngleChart.createData(ChartTypes.LINE, axesCatVal_1[0], (XDDFValueAxis) axesCatVal_1[1]);
// Line-1
XDDFLineChartData.Series series1 = addLineSeriesToChartData(chartData
, sheet
,sheetName + "!$B$3:$B$66"
, sheetName + "!$D$3:$D$66"
, sheetName + "!$D$2"
, PresetColor.RED
);
System.out.println("added line 1: \n" + series1);
// Line-2
XDDFLineChartData.Series series2 = addLineSeriesToChartData(chartData
, sheet
,sheetName+"!$B$3:$B$66"
, sheetName+"!$E$3:$E$66"
, sheetName+"!$E$2"
, PresetColor.GREEN
);
System.out.println("added line 2: \n" + series2);
// Line-3
XDDFLineChartData.Series series3 = addLineSeriesToChartData(chartData
, sheet
, sheetName+"!$B$3:$B$66"
, sheetName+"!$F$3:$F$66"
, sheetName+"!$F$2"
, PresetColor.BLUE
);
System.out.println("added line 3: \n" + series3);
//second chart Line-1
XDDFLineChartData.Series series4 = addLineSeriesToChartData(chartData_1
, sheet
, sheetName+"!$B$3:$B$66"
, sheetName+"!$G$3:$G$66"
, sheetName+"!$G$2"
, PresetColor.BLUE
);
System.out.println("added line 4: \n" + series4);
chart.plot(chartData);
medianAngleChart.plot(chartData_1);
// save workbook
writeWorkbookToFile(wb,"ChartWithThreeLines.xlsx");
// close workbook
wb.close();
}
}