[英]Apache POI Line chart points
我正在創建一個帶有折線圖的 Excel 文件。 我已經創建了圖表並用數據填充了它,但我需要在每個點上顯示值。 有沒有辦法可以使用 apache poi 4.1.1 版本在圖表中的點上顯示這些值?
圖表的布局,目前作為輸出
我想要的圖表布局
示例代碼:
createChartFromPivotTable( xssfSheet, "Generated", 6, 6 + NO_OF_DAYS - 1, 5, 13, 0, 0, 1, 1, 0 );
private void createChartFromPivotTable( XSSFSheet sheet, String title, int valueFirstRow, int valueLastRow, int startRow, int endRow, int xCol1, int xCol2, int ycol1, int ycol2, int addNum ) {
int lastRowNum = sheet.getLastRowNum();
XSSFDrawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor( 1, 1, 1, 1, 7, startRow, 14, endRow );
XSSFChart chart = drawing.createChart( anchor );
chart.setTitleText( "LineChart" );
chart.setTitleOverlay( false );
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition( LegendPosition.TOP_RIGHT );
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis( AxisPosition.BOTTOM );
bottomAxis.setTitle( "Offer Date" );
XDDFValueAxis leftAxis = chart.createValueAxis( AxisPosition.LEFT );
leftAxis.setTitle( "Count" );
XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange( sheet, new CellRangeAddress( valueFirstRow, valueLastRow, xCol1, xCol2 ) );
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange( sheet, new CellRangeAddress( valueFirstRow, valueLastRow, ycol1, ycol2 ) );
XDDFLineChartData data = ( XDDFLineChartData ) chart.createData( ChartTypes.LINE, bottomAxis, leftAxis );
data.setVaryColors( false );
XDDFLineChartData.Series series = ( XDDFLineChartData.Series ) data.addSeries( xs, ys1 );
series.setTitle( title, null );
series.setSmooth( false );
series.setMarkerStyle( MarkerStyle.STAR );
chart.plot( data );
chart.getCTChart().getPlotArea().getLineChartArray( 0 ).addNewAxId().setVal( bottomAxis.getId() );
chart.getCTChart().getPlotArea().getLineChartArray( 0 ).addNewAxId().setVal( leftAxis.getId() );
// plot area background and border line
if ( chart.getCTChartSpace().getSpPr() == null )
chart.getCTChartSpace().addNewSpPr();
if ( chart.getCTChartSpace().getSpPr().getSolidFill() == null )
chart.getCTChartSpace().getSpPr().addNewSolidFill();
if ( chart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr() == null )
chart.getCTChartSpace().getSpPr().getSolidFill().addNewSrgbClr();
chart.getCTChartSpace().getSpPr().getSolidFill().getSrgbClr().setVal( new byte[] { ( byte ) 255, ( byte ) 255, ( byte ) 255 } );
if ( chart.getCTChartSpace().getSpPr().getLn() == null )
chart.getCTChartSpace().getSpPr().addNewLn();
chart.getCTChartSpace().getSpPr().getLn().setW( Units.pixelToEMU( 1 ) );
if ( chart.getCTChartSpace().getSpPr().getLn().getSolidFill() == null )
chart.getCTChartSpace().getSpPr().getLn().addNewSolidFill();
if ( chart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr() == null )
chart.getCTChartSpace().getSpPr().getLn().getSolidFill().addNewSrgbClr();
chart.getCTChartSpace().getSpPr().getLn().getSolidFill().getSrgbClr().setVal( new byte[] { ( byte ) 0, ( byte ) 0, ( byte ) 0 } );
// line style of cat axis
if ( chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr() == null )
chart.getCTChart().getPlotArea().getCatAxArray( 0 ).addNewSpPr();
if ( chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().getLn() == null )
chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().addNewLn();
chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().getLn().setW( Units.pixelToEMU( 1 ) );
if ( chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().getLn().getSolidFill() == null )
chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().getLn().addNewSolidFill();
if ( chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().getLn().getSolidFill().getSrgbClr() == null )
chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().getLn().getSolidFill().addNewSrgbClr();
chart.getCTChart().getPlotArea().getCatAxArray( 0 ).getSpPr().getLn().getSolidFill().getSrgbClr().setVal( new byte[] { ( byte ) 0, ( byte ) 0, ( byte ) 0 } );
//line style of val axis
if ( chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr() == null )
chart.getCTChart().getPlotArea().getValAxArray( 0 ).addNewSpPr();
if ( chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().getLn() == null )
chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().addNewLn();
chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().getLn().setW( Units.pixelToEMU( 1 ) );
if ( chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().getLn().getSolidFill() == null )
chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().getLn().addNewSolidFill();
if ( chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().getLn().getSolidFill().getSrgbClr() == null )
chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().getLn().getSolidFill().addNewSrgbClr();
chart.getCTChart().getPlotArea().getValAxArray( 0 ).getSpPr().getLn().getSolidFill().getSrgbClr().setVal( new byte[] { ( byte ) 0, ( byte ) 0, ( byte ) 0 } );
XDDFSolidFillProperties fill = new XDDFSolidFillProperties( XDDFColor.from( PresetColor.BLUE ) );
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties( fill );
for ( XDDFChartData.Series series1 : data.getSeries() ) {
XDDFShapeProperties properties = series1.getShapeProperties();
if ( properties == null ) {
properties = new XDDFShapeProperties();
}
properties.setLineProperties( line );
series1.setShapeProperties( properties );
}
}
是的
series.setTitle(" ", chart.setSheetTitle(" ", 0))
series.setShowLeaderLines(true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.