0

With apache POI and openxmlformats I'm build an excel with a bar chart. Everything is working but now I want to change the color of the axis and the major grid lines and the font size but I cannot find the way to do this.

How can I do these?

Thanks

amachado
  • 1,032
  • 1
  • 15
  • 31

1 Answers1

1

How to get informations about org.openxmlformats.schemas.drawingml.x2006 objects:

All Office Open XML files (*.xlsx, *.docx, *.pptx) are ZIP archives. So we can unzip them and have a look into the internals.

So create a *.xlsx file having a bar chart and do coloring the axis and gridline and do formatting the axis font. Then unzip the *.xlsx file and look at xl/charts/chart1.xml. There you will find something like:

<c:valAx>
...
 <c:majorGridlines>
  <c:spPr>
   <a:ln>
    <a:solidFill>
     <a:srgbClr val="FF0000"/>
    </a:solidFill>
   </a:ln>
  </c:spPr>
 </c:majorGridlines>
...
 <c:spPr>
  <a:ln>
   <a:solidFill>
    <a:srgbClr val="FF0000"/>
   </a:solidFill>
  </a:ln>
 </c:spPr>
...
 <c:txPr>
  <a:bodyPr/>
  <a:p>
   <a:pPr>
    <a:defRPr sz="1200">
     <a:solidFill>
      <a:srgbClr val="FF0000"/>
     </a:solidFill>
    </a:defRPr>
   </a:pPr>
  </a:p>
 </c:txPr>
...
</c:valAx>

Now we need an API doc for org.openxmlformats.schemas.drawingml.x2006. Since grepcode.com seems not more available, I have found http://www.atetric.com/atetric/javadoc/org.apache.poi/ooxml-schemas/1.3/ for this. Start with Interface CTValAx.

So based on my code from Create Bar Chart in Excel with Apache POI the value axis could be formatted like so:

...
        //val axis
        CTValAx ctValAx = ctPlotArea.addNewValAx(); 
        ctValAx.addNewAxId().setVal(123457); //id of the val axis
        ctScaling = ctValAx.addNewScaling();
        ctScaling.addNewOrientation().setVal(STOrientation.MIN_MAX);
        ctValAx.addNewDelete().setVal(false);
        ctValAx.addNewAxPos().setVal(STAxPos.L);
        ctValAx.addNewCrossAx().setVal(123456); //id of the cat axis
        ctValAx.addNewTickLblPos().setVal(STTickLblPos.NEXT_TO);

        //colored major gridlines
        ctValAx.addNewMajorGridlines().addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)255,0,0});

        //colored axis line
        ctValAx.addNewSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)255,0,0});

        //axis font
        org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody ctTextBody
         = ctValAx.addNewTxPr(); //text body properties
        ctTextBody.addNewBodyPr(); //body properties
        org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties ctTextCharacterProperties 
         = ctTextBody.addNewP().addNewPPr().addNewDefRPr(); //character properties
        ctTextCharacterProperties.setSz(12*100); //size in 100th of a point
        ctTextCharacterProperties.addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)255,0,0}); //color
...
Axel Richter
  • 56,077
  • 6
  • 60
  • 87