如何用POI 4.0.0绘制2线时间序列?

时间:2018-09-27 01:12:44

标签: apache-poi

任何人都可以共享使用POI 4.0.0绘制时间序列(例如x =日期,y =数值)图表的工作代码吗?我正在尝试绘制一个2折线图,与POI 4.0.0在X轴上共享日期 但是这段代码:

public function editProductImg($img,$prodId) {
  $checkExist = $this->fetchSingleImage($prodId);
  if(empty($checkExist)) {
    if($this->db->insert_batch('tbl_product_images',$img)) {
      return true;
    } else {
      return false;
    }
  } else {
    foreach($img as $key => $value) {  

        $this->db->query("update tbl_product_images SET Image='".$value['Image']."',SortOrder='".$value['SortOrder']."' WHERE ProductId='".$prodId."'");

        return $this->db->last_query();
    }
  }
}

导致异常:

        ...
        XSSFDrawing drawing = sheet.createDrawingPatriarch();
        XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
        XSSFChart chart = drawing.createChart(anchor);
        XDDFChartLegend legend = chart.getOrAddLegend();
        legend.setPosition(LegendPosition.TOP_RIGHT);

        // Use a category axis for the bottom axis.
        XDDFCategoryAxis bottomAxis = 
        chart.createCategoryAxis(AxisPosition.BOTTOM);
        XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
        leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
        int chartStartRow = SHEET_START_ROW + 1;
        int chartEndRow = chartStartRow + mergedData.size() - 1;

        // This is selecting data from a column of dates
        XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(chartStartRow, chartEndRow, 0, 0));
        // This is time series 1
        XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(chartStartRow, chartEndRow, 1, 1));
        // This is time series 2
        XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(chartStartRow, chartEndRow, 2, 2));


        XDDFChartData data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
        data.addSeries(xs, ys1);
        data.addSeries(xs, ys2);
        chart.plot(data);

我什至会喜欢旧版本POI的代码段,因为我怀疑POI 4.0.0有问题。谢谢

1 个答案:

答案 0 :(得分:0)

亚历克斯(Alex)将日期视为数字的注释是正确的,但是POI 4.0.0图表尚未准备好,您需要从此处应用代码技巧:

Problem running official examples LineChars and ScatterChart with Apache POI 4.0