Charts don't appear in Google Sheets

524 views Asked by At

I am using XSSF to make my charts in Excel.

The charts look perfectly fine in Microsoft Excel.

Problem:

When I import a chart into Google Sheets, the chart doesn't show up.

Is there a way to fix this?

I noticed if I made the chart in Microsoft Excel itself and import into Google Sheets, it works fine and I can see the chart. It seems like the problem occurs when the chart is made through Apache POI.

Here is the code:

void createChart(
  XSSFSheet sheet, String chartName, double max,
  XDDFDataSource<String> cat, XDDFNumericalDataSource<Double> val
 ) {
  try {

  final int graphRow1 = 6;
  final int graphRow2 = 33;
  final int graphCol1 = 4;
  final int graphCol2 = 22;
  final int logoRow1 = 30;
  final int logoRow2 = 33;
  final int logoCol1 = 19;
  final int logoCol2 = 21;

  XSSFDrawing drawing = sheet.createDrawingPatriarch();
  XSSFClientAnchor anchor = drawing.createAnchor(
      0, 0, 0, 0, graphCol1, graphRow1, graphCol2, graphRow2
  );

  XSSFChart chart = drawing.createChart(anchor);
  chart.setTitleText(chartName);

  chart.setTitleOverlay(false);
  chart.getCTChart().getTitle()
      .getTx()
      .getRich()
      .getPArray(0)
      .getRArray(0)
      .getRPr().setSz(1400);


  CTRegularTextRun secondText = chart.getCTChart().getTitle()
      .getTx()
      .getRich()
      .addNewP()
      .addNewR();
  secondText.setT("Net Analysis");
  secondText.addNewRPr().setSz(1050);
  secondText.getRPr().setI(true);

  chart.getFormattedTitle().getParagraph(0).setTextAlignment(TextAlignment.CENTER);
  chart.getFormattedTitle().getParagraph(1).setTextAlignment(TextAlignment.CENTER);

  XDDFCategoryAxis leftAxis = chart.createCategoryAxis(AxisPosition.LEFT);

  XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
  bottomAxis.setTitle("Volume");
  bottomAxis.setCrossBetween(AxisCrossBetween.BETWEEN);

  XDDFBarChartData data =
      (XDDFBarChartData) chart.createData(ChartTypes.BAR, leftAxis, bottomAxis);
  data.setBarDirection(BarDirection.BAR);
  data.setVaryColors(true);

  XDDFChartData.Series series = data.addSeries(cat, val);
  series.setTitle("", null);
  chart.plot(data);

  chart.getCTChartSpace()
      .addNewSpPr()
      .addNewSolidFill()
      .addNewSrgbClr()
      .setVal(HexConverter.parseHexBinary("52c3ba"));

  //needed for image
  //allows to modify anchor otherwise the anchor will resize chart.
  drawing.createGroup(anchor);

  int dx = Units.EMU_PER_CHARACTER * 6;
  int dy = 3 * Units.EMU_PER_CHARACTER / 2;
  anchor.setDx1(dx);
  anchor.setDx2(dx);
  anchor.setDy1(dy);
  anchor.setDy2(dy);
  anchor.setCol1(logoCol1);
  anchor.setCol2(logoCol2);
  anchor.setRow1(logoRow1);
  anchor.setRow2(logoRow2);
  drawing.createPicture(anchor, 0);

  double tickMarkInterval = max % 20 == 0 ? max : max + (20 - (max % 20));
  double maxUnit = tickMarkInterval / 20;

  chart.getCTChart().getPlotArea().getBarChartArray(0).addNewGapWidth().setVal(10);
  chart.getCTChart().getPlotArea().getValAxArray(0).addNewMajorUnit().setVal(maxUnit);

  double scaleMax = max % maxUnit == 0 ? max : max + (maxUnit - (max % maxUnit));
  chart.getCTChart().getPlotArea().getValAxArray(0).getScaling().addNewMax().setVal(scaleMax);


} catch (Exception e) {
  e.printStackTrace();
}
}

cat and val are made here

XDDFDataSource<String> cat =
    XDDFDataSourcesFactory.fromArray((String[]) categoryList.toArray());
XDDFNumericalDataSource<Double> val =
    XDDFDataSourcesFactory.fromArray((Double[]) valueList.toArray());

The lay out of the XLSX is like this:
Row 1 has Category A.
Row 2 has Subcategory A1
Row 3 has Subcategory A2
Row 4 has Subcategory A3
Row 5 has Total of A

Row 7 has Category B.
Row 8 has Subcategory B1
Row 9 has Subcategory B2
Row 10 has Subcategory B3
Row 11 has Total of B

The chart is supposed to contain the name of Category A and B, and Total of A and B. So I assume this wouldn't work in google sheets then?

0

There are 0 answers