public void excelBarChart(Inbounds.Inbound_chartExport inbound){
XSSFDrawing drawing = inbound.sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, inbound.col1, inbound.row1, inbound.col2, inbound.row2);
XSSFChart chart = drawing.createChart(anchor);//图表
chart.setTitleText(inbound.title);
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();//图例项
legend.setPosition(LegendPosition.TOP_RIGHT);
// Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);//x轴和位置
bottomAxis.setTitle(inbound.xName);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);//y轴和位置
leftAxis.setTitle(inbound.yName);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);//生成Data
XDDFDataSource xs = XDDFDataSourcesFactory.fromStringCellRange(inbound.sheet, new CellRangeAddress(inbound.startRow, inbound.size, inbound.xStartCol, inbound.xStartCol));//x轴range区域
XDDFNumericalDataSource ys1 = XDDFDataSourcesFactory.fromNumericCellRange(inbound.sheet, new CellRangeAddress(inbound.startRow, inbound.size, inbound.yStartCol, inbound.yStartCol));//y轴range区域
XDDFChartData.Series series1 = data.addSeries(xs, ys1);//生成Series 系列/维度
series1.setTitle(inbound.seriesTitle, null);
chart.plot(data);
XDDFBarChartData bar = (XDDFBarChartData) data;//由数据生成图表,Bar柱状图,Pie饼状图,Line折线图,Scatter散点图
bar.setBarDirection(BarDirection.BAR);//柱状如方向 BAR 竖向 COL横向
CTChart ctChart = chart.getCTChart();
CTPlotArea ctPlotArea = ctChart.getPlotArea();
CTBarChart ctBarChart = ctPlotArea.getBarChartArray(0);
CTBarSer ctBarSer = ctBarChart.getSerArray(0);
CTDLbls newDLbls = ctBarSer.addNewDLbls();//数据标签
CTBoolean ctBoolean = ctBarChart.addNewVaryColors();//多样颜色,true 选用category作为图例项,false选用系列作为图例项,最好false
ctBoolean.setVal(false);
newDLbls.setShowCatName(ctBoolean);//数据标签 显示类别名称
newDLbls.setShowSerName(ctBoolean);//数据标签 显示序列名称
newDLbls.setShowPercent(ctBoolean);
newDLbls.setShowBubbleSize(ctBoolean);
newDLbls.setShowLeaderLines(ctBoolean);
newDLbls.setShowLegendKey(ctBoolean);//图例化标签
newDLbls.addNewShowVal().setVal(true);//数据标签 显示值
solidFillSeries(data, 0, PresetColor.CHARTREUSE);//颜色PresetColor
}
private static void solidFillSeries(XDDFChartData data, int index, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFChartData.Series series = data.getSeries().get(index);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setFillProperties(fill);
series.setShapeProperties(properties);
}