Apache poi Excel折线图

时间:2016-09-22 10:06:50

标签: java excel apache charts apache-poi

我正在使用折线图创建Excel文件。我创建了图表并用数据填充,但我无法在图表上创建点数。有谁知道,有没有办法使用apache poi在图表中生成这些点(三角形,正方形,圆形等)?

enter image description here

这是我生成当前char的代码:

    public static void main(String[] args) throws Exception {
        Workbook wb = new XSSFWorkbook();
        Sheet dataSheet = wb.createSheet("linechart");

        final int NUM_OF_ROWS = 10;
        final int NUM_OF_COLUMNS = 4;

        Row row;
        Cell cell;
        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
            row = dataSheet.createRow((short) rowIndex);
            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                cell = row.createCell((short) colIndex);
                cell.setCellValue(rowIndex * ((colIndex + 1) + ((int) (Math.random() * 10))));
            }
        }

        Drawing drawing = dataSheet.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, NUM_OF_COLUMNS + 2, 3, NUM_OF_COLUMNS + 15, 20);

        Chart chart = drawing.createChart(anchor);
        ChartLegend legend = chart.getOrCreateLegend();
        legend.setPosition(LegendPosition.RIGHT);

        LineChartData data = chart.getChartDataFactory().createLineChartData();

        ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
        ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(dataSheet, new CellRangeAddress(0, NUM_OF_ROWS - 1, 0, 0));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(dataSheet, new CellRangeAddress(0, NUM_OF_ROWS - 1, 1, 1));
        ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(dataSheet, new CellRangeAddress(0, NUM_OF_ROWS - 1, 2, 2));
        ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(dataSheet, new CellRangeAddress(0, NUM_OF_ROWS - 1, 3, 3));

        LineChartSeries series1 = data.addSeries(xs, ys1);
        series1.setTitle("one");
        LineChartSeries series2 = data.addSeries(xs, ys2);
        series2.setTitle("two");
        LineChartSeries series3 = data.addSeries(xs, ys3);
        series3.setTitle("three");

        chart.plot(data, bottomAxis, leftAxis);

        XSSFChart xssfChart = (XSSFChart) chart;
        CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
        plotArea.getLineChartArray()[0].getSmooth();
        CTBoolean ctBool = CTBoolean.Factory.newInstance();
        ctBool.setVal(false);
        plotArea.getLineChartArray()[0].setSmooth(ctBool);
        for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
            ser.setSmooth(ctBool);
        }

        FileOutputStream fileOut = new FileOutputStream("chart.xlsx");
        wb.write(fileOut);
        fileOut.close();
    }

2 个答案:

答案 0 :(得分:5)

解决方案:

// the business object
case class Person(id: Long, name: String, city: String)

// the dataset to query over
val dataset = Seq(Person(0, "Jacek", "Warsaw")).toDS

// the query
// Note that we work with names only (out of 3 attributes in Person)
val query = dataset.groupBy(upper('name) as 'name).count

scala> query.explain(extended = true)
...
TRACE SparkOptimizer:
=== Applying Rule org.apache.spark.sql.catalyst.optimizer.ColumnPruning ===
 Aggregate [upper(name#126)], [upper(name#126) AS name#160, count(1) AS count#166L]   Aggregate [upper(name#126)], [upper(name#126) AS name#160, count(1) AS count#166L]
!+- LocalRelation [id#125L, name#126, city#127]                                       +- Project [name#126]
!                                                                                        +- LocalRelation [id#125L, name#126, city#127]
...
== Parsed Logical Plan ==
'Aggregate [upper('name) AS name#160], [upper('name) AS name#160, count(1) AS count#166L]
+- LocalRelation [id#125L, name#126, city#127]

== Analyzed Logical Plan ==
name: string, count: bigint
Aggregate [upper(name#126)], [upper(name#126) AS name#160, count(1) AS count#166L]
+- LocalRelation [id#125L, name#126, city#127]

== Optimized Logical Plan ==
Aggregate [upper(name#126)], [upper(name#126) AS name#160, count(1) AS count#166L]
+- LocalRelation [name#126]

== Physical Plan ==
*HashAggregate(keys=[upper(name#126)#171], functions=[count(1)], output=[name#160, count#166L])
+- Exchange hashpartitioning(upper(name#126)#171, 200)
   +- *HashAggregate(keys=[upper(name#126) AS upper(name#126)#171], functions=[partial_count(1)], output=[upper(name#126)#171, count#173L])
      +- LocalTableScan [name#126]

答案 1 :(得分:3)

以下代码将为您提供您正在寻找的内容

    CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
    plotArea.getLineChartArray()[0].getSmooth();
    CTMarker ctMarker = CTMarker.Factory.newInstance();
    ctMarker.setSymbol(CTMarkerStyle.Factory.newInstance());
    CTBoolean ctBool = CTBoolean.Factory.newInstance();
    ctBool.setVal(false);
    plotArea.getLineChartArray()[0].setSmooth(ctBool);
    for (CTLineSer ser : plotArea.getLineChartArray()[0].getSerArray()) {
        ser.setSmooth(ctBool);
        ser.setMarker(ctMarker);
    }