题目描述
如题 java poi操作word生成chart之后,横坐标的日期数据如何能竖着从下到上显示,或者斜着显示?
相关代码
poi的依赖
//poi
compile ('org.apache.poi:poi:4.1.0')
compile ('org.apache.poi:poi-ooxml:4.1.0')
implementation group: 'org.apache.poi', name: 'poi-scratchpad', version: '4.1.0'
关于数据
String[] categories = listToString(categoriesL);
Number[] valueE = listToNumber(valueEL);
Number[] valueA = listToNumber(valueAL);
Number[] valueT = listToNumber(valueTL);
List<Number[]> valueListTemp = new ArrayList<>();
valueListTemp.add(valueE);
valueListTemp.add(valueA);
valueListTemp.add(valueT);
generateChart(doc,categories, valueListTemp);
public static Number[] listToNumber(List<Number> num2){
Number[] numTmp = new Number[num2.size()];
for(int i = 0;i< num2.size();i++){
numTmp[i] = num2.get(i);
}
return numTmp;
}
public static String[] listToString(List<String> num2){
String[] numTmp = new String[num2.size()];
for(int i = 0;i< num2.size();i++){
numTmp[i] = num2.get(i);
}
return numTmp;
}
在word中生成图表的方法
public static void generateChart(XWPFDocument document, String[] categories, List<Number[]> values) {
try {
// create the chart
XWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);
// create data sources
int numOfPoints = categories.length;
String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
String valuesDataRangeA = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
String valuesDataRangeB = chart.formatRange(new CellRangeAddress(1, numOfPoints, 3, 3));
XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
String valuesDataRangeC = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
XDDFNumericalDataSource<Number> valuesDataE = XDDFDataSourcesFactory.fromArray(values.get(0), valuesDataRangeC, 1);
XDDFNumericalDataSource<Number> valuesDataA = XDDFDataSourcesFactory.fromArray(values.get(1), valuesDataRangeA, 2);
XDDFNumericalDataSource<Number> valuesDataT = XDDFDataSourcesFactory.fromArray(values.get(2), valuesDataRangeB, 3);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
// Set AxisCrossBetween, so the left axis crosses the category axis between the categories.
// Else first and last category is exactly on cross points and the bars are only half visible.
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
// LINE:折线图,
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
// 图表加载数据,折线1
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(categoriesData,valuesDataE );
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(categoriesData, valuesDataA);
XDDFLineChartData.Series series3 = (XDDFLineChartData.Series) data.addSeries(categoriesData, valuesDataT);
// 点的类型
series1.setMarkerStyle(MarkerStyle.DOT);
series2.setMarkerStyle(MarkerStyle.DOT);
series3.setMarkerStyle(MarkerStyle.DOT);
// 线不弯曲
series1.setSmooth(false);
series2.setSmooth(false);
series3.setSmooth(false);
data.setVaryColors(false);
series1.setTitle("预测值", null);
series2.setTitle("实际值",null);
series3.setTitle("阈值",null);
/*
// if more than one series do vary colors of the series
((XDDFBarChartData)data).setVaryColors(true);
series = data.addSeries(categoriesData, valuesDataB);
//series.setTitle("b", chart.setSheetTitle("b", 2));
series.setTitle("b", setTitleInDataSheet(chart, "b", 2));
*/
CTPlotArea ctPlotArea = chart.getCTChart().getPlotArea();
CTCatAx ctCatAx = ctPlotArea.getCatAxArray(0);
ctCatAx.addNewLblAlgn().setVal(STLblAlgn.CTR);
ctCatAx.addNewAuto().setVal(true);
// plot chart data
chart.plot(data);
// create legend
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP);
legend.setOverlay(false);
} catch (IOException e) {
e.printStackTrace();
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
预期是横坐标可以斜着或者竖着显示
目前生成的效果是这样,所有的日期如果不加这个红线会非常难看出来横坐标是个什么,关于横坐标的红线是我生成之后自己画的,只是想描述一下正确的时间格式是MM/dd HH:mm
想要的效果是这样的,横坐标的日期的文字方向是从下到上的竖过来,这个直接在word中用可视化界面选个选项就行了,但是用java poi就一直找不到如何设置
想在poi中找这个设置