Apache POI折线图点

问题描述

我正在创建带有折线图的Excel文件。我已经创建了图表并用数据填充了它,但是我需要在每个点上显示值。是否可以使用apache poi 4.1.1版本在图表上的点上显示这些值?

图片:Layout of the chart,Currently coming as output

图片:Layout of the chart which i want

示例代码:

createChartFromPivotTable( xssfSheet,"Generated",6,6 + NO_OF_DAYS - 1,5,13,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,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,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 } );

    //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 } );

    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 );
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)