I am trying to create a bar chart in excel file using Apache poi and following the code mentioned here: Create Bar Chart in Excel with Apache POI.
In my case, I need to use data values coming from a different source instead of the excel cells. How can I use those values in the BarChart?
Assume that the values will come as an array of float/double values. Also, I need to be able to set Bar colors independently. For example, some bars will be green while some others will be red.
I modified the data portion of Create Bar Chart in Excel with Apache POI code in the following manner:
CTBarSer ctBarSer = ctBarChart.addNewSer();
CTSerTx ctSerTx = ctBarSer.addNewTx();
CTStrRef ctStrRef = ctSerTx.addNewStrRef();
ctStrRef.setF("hardcoded" + r);
ctBarSer.addNewIdx().setVal(r-2);
CTAxDataSource cttAxDataSource = ctBarSer.addNewCat();
CTStrData ctStrData = cttAxDataSource.addNewStrLit();
ctStrData.addNewPt().setV("Val1");
ctStrData.addNewPt().setV("Val2");
ctStrData.addNewPt().setV("Val3");
CTNumDataSource ctNumDataSource = ctBarSer.addNewVal();
CTNumData ctNumData = ctNumDataSource.addNewNumLit();
CTNumVal ctNumVal = ctNumData.addNewPt();
ctNumVal.setV(String.valueOf(0.92));
ctNumData = ctNumDataSource.addNewNumLit();
ctNumVal = ctNumData.addNewPt();
ctNumVal.setV(String.valueOf(0.95));
ctNumData = ctNumDataSource.addNewNumLit();
ctNumVal = ctNumData.addNewPt();
ctNumVal.setV(String.valueOf(0.98));
Any help is greatly appreciated. Thanks!
When I output the excel file using print, it gives the following result:
<xml-fragment xmlns:char="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:main="http://schemas.openxmlformats.org/drawingml/2006/main">
<char:plotArea>
<char:layout/>
<char:barChart>
<char:barDir val="col"/>
<char:varyColors val="false"/>
<char:ser>
<char:idx val="0"/>
<char:tx>
<char:strRef>
<char:f>hardcoded2</char:f>
</char:strRef>
</char:tx>
<char:spPr>
<main:ln>
<main:solidFill>
<main:srgbClr val="000000"/>
</main:solidFill>
</main:ln>
</char:spPr>
<char:cat>
<char:strLit>
<char:pt>
<char:v>Val1</char:v>
</char:pt>
<char:pt>
<char:v>Val2</char:v>
</char:pt>
<char:pt>
<char:v>Val3</char:v>
</char:pt>
</char:strLit>
</char:cat>
<char:val>
<char:numLit>
<char:pt>
<char:v>0.92</char:v>
</char:pt>
</char:numLit>
<char:numLit>
<char:pt>
<char:v>0.95</char:v>
</char:pt>
</char:numLit>
<char:numLit>
<char:pt>
<char:v>0.98</char:v>
</char:pt>
</char:numLit>
</char:val>
</char:ser>
<char:axId val="123456"/>
<char:axId val="123457"/>
</char:barChart>
<char:catAx>
<char:axId val="123456"/>
<char:scaling>
<char:orientation val="minMax"/>
</char:scaling>
<char:delete val="false"/>
<char:axPos val="b"/>
<char:tickLblPos val="nextTo"/>
<char:crossAx val="123457"/>
</char:catAx>
<char:valAx>
<char:axId val="123457"/>
<char:scaling>
<char:orientation val="minMax"/>
</char:scaling>
<char:delete val="false"/>
<char:axPos val="l"/>
<char:tickLblPos val="nextTo"/>
<char:crossAx val="123456"/>
</char:valAx>
</char:plotArea>
<char:plotVisOnly val="true"/>
</xml-fragment>