[英]Apache POI Line chart points
I am creating an Excel file with line chart.我正在创建一个带有折线图的 Excel 文件。 I've created chart and filled it with data but I need values to be shown on each of the points.
我已经创建了图表并用数据填充了它,但我需要在每个点上显示值。 Is there a way I could show these values on points in a chart using apache poi 4.1.1 version?
有没有办法可以使用 apache poi 4.1.1 版本在图表中的点上显示这些值?
Layout of the chart,Currently coming as output图表的布局,目前作为输出
Layout of the chart which i want我想要的图表布局
Sample Code:示例代码:
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 );
}
}
yes是的
series.setTitle(" ", chart.setSheetTitle(" ", 0))
series.setShowLeaderLines(true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.