[英]How to change the orientation in the chart
有沒有辦法改變圖表的方向? 我需要將圖表和表格放在同一方向。 如您所見,圖表的頂部元素是client 8
,表格的頂部元素是client 1
。 我需要兩者的順序完全相同。 我怎樣才能做到這一點? 我應該做什么改變?
這是生成上面圖表的部分代碼。
public ByteArrayInputStream generateFileDetailsWithChart(ClientDataObjectRequest clientDataObjectRequest) {
ByteArrayOutputStream file = new ByteArrayOutputStream();
try{
try(XSSFWorkbook workbook = new XSSFWorkbook()){
XSSFSheet sheet = workbook.createSheet(clientDataObjectRequest.getMetricName());
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setUnderline(Font.U_SINGLE);
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFillBackgroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
headerCellStyle.setFont(headerFont);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 2, 1, 15, 15 + clientDataObjectRequest.getCompanyChartData().size());
Row headerRow = sheet.createRow(anchor.getRow2() + 1);
headerRow.createCell(0).setCellValue(Constants.CLIENT_NAME);
headerRow.createCell(1).setCellValue("Value ("+clientDataObjectRequest.getDataFormatCodeValue()+")");
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
headerRow.getCell(i).setCellStyle(headerCellStyle);
}
CellStyle rowCellStyle = workbook.createCellStyle();
if (clientDataObjectRequest.getDataFormatCodeValue().equalsIgnoreCase(Constants.DATA_FORMAT_PERCENTAGE)) {
rowCellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));
for (int i = 0; i < clientDataObjectRequest.getCompanyChartData().size(); i++) {
Row row = sheet.createRow(anchor.getRow2() + 2 + i);
row.createCell(0).setCellValue(clientDataObjectRequest.getCompanyChartData().get(i).getClientName());
row.createCell(1).setCellValue(clientDataObjectRequest.getCompanyChartData().get(i).getValue()/100);
//rowCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00%"));
row.getCell(1).setCellStyle(rowCellStyle);
}
}
if (clientDataObjectRequest.getDataFormatCodeValue().equalsIgnoreCase(Constants.DATA_FORMAT_CURRENCY)) {
rowCellStyle.setDataFormat(workbook.createDataFormat().getFormat("$0.00"));
for (int i = 0; i < clientDataObjectRequest.getCompanyChartData().size(); i++) {
Row row = sheet.createRow(anchor.getRow2() + 2 + i);
row.createCell(0).setCellValue(clientDataObjectRequest.getCompanyChartData().get(i).getClientName());
row.createCell(1).setCellValue(clientDataObjectRequest.getCompanyChartData().get(i).getValue());
row.getCell(1).setCellStyle(rowCellStyle);
}
}
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText(clientDataObjectRequest.getMetricName());
chart.getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().setSz(2000);
chart.setTitleOverlay(false);
//XDDFChartLegend legend = chart.getOrAddLegend();
//legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFTitle title = getOrSetAxisTitle(bottomAxis);
title.setOverlay(false);
title.setText(Constants.CLIENT_NAME);
title.getBody().getParagraph(0).addDefaultRunProperties().setFontSize(12d);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
title = getOrSetAxisTitle(leftAxis);
title.setOverlay(false);
title.setText("Value ("+clientDataObjectRequest.getDataFormatCodeValue()+")");
title.getBody().getParagraph(0).addDefaultRunProperties().setFontSize(12d);
if(clientDataObjectRequest.getDataFormatCodeValue().equalsIgnoreCase(Constants.DATA_FORMAT_PERCENTAGE)) {
leftAxis.setMaximum(1.0);
}
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
sheet.shiftColumns(0,1,2);
sheet.autoSizeColumn(2);
sheet.autoSizeColumn(3);
XDDFDataSource<String> clientNames = XDDFDataSourcesFactory.fromStringCellRange(sheet,
new CellRangeAddress(anchor.getRow2() + 2,anchor.getRow2() + 1 + clientDataObjectRequest.getCompanyChartData().size(),2,2));
XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(anchor.getRow2() + 2,anchor.getRow2() + 1 + clientDataObjectRequest.getCompanyChartData().size(),3,3));
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
XDDFChartData.Series series = data.addSeries(clientNames, values);
series.setTitle(clientDataObjectRequest.getMetricName(), null);
setDataLabels(series,7,true); // pos 7 = INT_OUT_END, showVal = true
chart.plot(data);
XDDFBarChartData bar = (XDDFBarChartData) data;
bar.setBarDirection(BarDirection.BAR);
solidFillSeries(data, 0, PresetColor.BLUE);
Row bottomRow = sheet.createRow(anchor.getRow2() + clientDataObjectRequest.getCompanyChartData().size() + 3);
bottomRow.createCell(0).setCellValue(Constants.COPYRIGHT_FOOTER);
workbook.write(file);
}
} catch (Exception e){
}
return new ByteArrayInputStream(file.toByteArray());
}
這是我的 controller 正在接收的主體。
{
"metricName":"Turnover Rate",
"dataFormatCodeValue": "currency",
"clientDataRequest":[
{
"clientName":"client 1",
"value":"1"
},
{
"clientName":"client 2",
"value":"2"
},
{
"clientName":"client 3",
"value":"3"
},
{
"clientName":"client 4",
"value":"4"
},
{
"clientName":"client 5555555",
"value":"5"
},
{
"clientName":"client 6",
"value":"6"
},
{
"clientName":"client 7",
"value":"7"
},
{
"clientName":"client 8",
"value":"8.5"
}
]
}
圖表中 y 軸的默認方向是自下而上。 在 x 軸交叉處的底部是最小值,頂部是最大值。 這就是你所擁有的。
這可以使用XDDFChartAxis.setOrientation -> AxisOrientation.MAX_MIN進行更改。 在你的情況下:
...
XDDFCategoryAxis bottomAxis = ...
...
bottomAxis.setOrientation(AxisOrientation.MAX_MIN);
...
但是你設置leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
. 因此,由於 min(零)現在位於頂部, AxisCrosses.AUTO_ZERO
現在也位於頂部。 所以你還需要設置:
...
XDDFValueAxis leftAxis = ...
...
leftAxis.setCrosses(AxisCrosses.MAX);
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.