title: How do i create excel chart with SXSSFSheet using apache POI?
body: <p>My project is using <strong>SXSSFWorkbook</strong> in order to generate excel files as the excel files generated in the application can be of large size as well. Now I need to include excel charts in these excel files as well and i am not able to create charts with <strong>SXSSFWorkbook</strong>. The only examples I get is with <strong>XSSFWorkbook</strong>. Is there a way I can have excel chart with <strong>SXSSFWorkbook</strong>?</p>
  <p>The examples I get for <strong>XSSFWorkbook</strong> is shown below. I am looking for similar example with <strong>SXSSFWorkbook</strong></p>
  <pre><code>    XSSFWorkbook wb = new XSSFWorkbook()        
      XSSFSheet sheet = wb.createSheet(&quot;barchart&quot;);
      XSSFDrawing drawing = sheet.createDrawingPatriarch();
      XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 3, 1, 10, DATA_START_ROW - 2);
  
      XSSFChart chart = drawing.createChart(anchor);
      chart.setTitleText(&quot;Trend (Claim Type)&quot;);
      chart.setTitleOverlay(false);
  
      XDDFChartLegend legend = chart.getOrAddLegend();
      legend.setPosition(LegendPosition.TOP_RIGHT);
  
      XDDFCategoryAxis bottomAxis = chart
              .createCategoryAxis(AxisPosition.BOTTOM);
      bottomAxis.setTitle(&quot;Period&quot;);
      XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
      leftAxis.setTitle(&quot;Cost&quot;);
  
      XDDFDataSource&lt;String&gt; periods = XDDFDataSourcesFactory
              .fromStringCellRange(sheet,
                      new CellRangeAddress(DATA_START_ROW, DATA_START_ROW + data.size(), 1, 1));
  
      XDDFNumericalDataSource&lt;Double&gt; allowed = XDDFDataSourcesFactory
              .fromNumericCellRange(sheet,
                      new CellRangeAddress(DATA_START_ROW, DATA_START_ROW + data.size(), 2, 2));
  
      XDDFNumericalDataSource&lt;Double&gt; total = XDDFDataSourcesFactory
              .fromNumericCellRange(sheet,
                      new CellRangeAddress(DATA_START_ROW, DATA_START_ROW + data.size(), 3, 3));
  
      XDDFLineChartData pdata = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
  
      XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) pdata.addSeries(periods, total);
      series1.setTitle(&quot;Total Paid&quot;, null);
      series1.setSmooth(false);
      series1.setMarkerStyle(MarkerStyle.STAR);
  
      XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) pdata.addSeries(periods, allowed);
      series2.setTitle(&quot;Allowed&quot;, null);
      series2.setSmooth(true);
      series2.setMarkerSize((short) 6);
      series2.setMarkerStyle(MarkerStyle.SQUARE);
  
      chart.plot(pdata);
  
  
      // Write output to an excel file
      try (FileOutputStream fileOut = new FileOutputStream(
              getReportFolderPath())) {
  
          //wb.write(fileOut);
      } catch (Exception ex) {
  
      }
  </code></pre>
  <p>UPDATE -&gt; I have tried doing as below but still getting array out of bound while doing chart.plot(pdata). What I am trying to do is create a XSSFSheet using SXSSFWorkbook and plot graph in this sheet. However, creating a ClientAnchor using SXSSFDrawing and creating XSSFChart like XSSFChart chart = sxssfDrawing.createChart(anchor)</p>
  <pre><code>        sxssfSheet.createDrawingPatriarch();
          XSSFSheet xssfSheet = workbook.getXSSFWorkbook().getSheet(sheet.getSheetName());
          chart.drawChart(sxssfSheet, xssfSheet, xssfSheet.getDrawingPatriarch());
  
      public void drawChart(SXSSFSheet sSheet, XSSFSheet xSheet, XSSFDrawing drawing) {
      SXSSFDrawing sxssfDrawing = sSheet.createDrawingPatriarch();
      ClientAnchor anchor = sxssfDrawing.createAnchor(0, 0, 0, 0, 3, 1, 10, chartIndexDTO.getDataRowStartIndex() - 2);
  
      XSSFChart chart = drawing.createChart(anchor);
      chart.setTitleText(&quot;Claim Type vs Allowed Amount&quot;);
      chart.setTitleOverlay(false);
  
      XDDFChartLegend legend = chart.getOrAddLegend();
      legend.setPosition(LegendPosition.TOP_RIGHT);
  
      XDDFCategoryAxis bottomAxis = chart
              .createCategoryAxis(AxisPosition.BOTTOM);
      bottomAxis.setTitle(&quot;Claim Type&quot;);
      XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
      leftAxis.setTitle(&quot;Allowed&quot;);
      leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  
      XDDFDataSource&lt;String&gt; xAxis = XDDFDataSourcesFactory
              .fromStringCellRange(xSheet,
                      new CellRangeAddress(chartIndexDTO.getDataRowStartIndex(), chartIndexDTO.getDataRowEndIndex(), 0, 0));
  
      XDDFNumericalDataSource&lt;Double&gt; yAxis = XDDFDataSourcesFactory
              .fromNumericCellRange(xSheet,
                      new CellRangeAddress(chartIndexDTO.getDataRowStartIndex(), chartIndexDTO.getDataRowEndIndex(), 1, 1));
  
      XDDFChartData pdata = chart
              .createData(ChartTypes.BAR, bottomAxis, leftAxis);
      XDDFChartData.Series series1 = pdata.addSeries(xAxis, yAxis);
      series1.setTitle(&quot;Allowed&quot;, null);
      pdata.setVaryColors(true);
      chart.plot(pdata);
  
      XDDFBarChartData bar = (XDDFBarChartData) pdata;
      bar.setBarDirection(BarDirection.COL);
  }
  </code></pre>