POI créer line chart avec 2 axes verticaux
Salut,
travaillant avec POI j'ai besoin de créer un line chart avec deux axes verticaux (plusieurs séries à plotter avec 2 unités différentes)
Voici dans un premier temps mon code pour créer le graphique:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private XSSFChart createChart(Sheet sheet,int[] anchors, String xLabel, String yLabel, String title){
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(anchors[0], anchors[1], anchors[2],
anchors[3], anchors[4], anchors[5], anchors[6], anchors[7] );
XSSFChart chart = (XSSFChart) drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.TOP);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
ValueAxis rightAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.RIGHT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
//Function to set axis title took from stackoverflow how-to-set-axis-labels-in-a-line-chart-using-apache-poi
setValueAxisTitle(chart,0,yLabel);
setValueAxisTitle(chart,0,"Second Axis");
setCatAxisTitle(chart,0,xLabel);
chart.setTitle(title);
chart.getAxis();
return chart;
} |
Et voici le morceau de code pour plotter les données:
Code:
1 2 3 4 5 6 7 8 9
| XSSFChart chart1=createChart(sheet,anchor1,"Temps","Courbes",title );
LineChartData dataM3s = chart1.getChartDataFactory().createLineChartData();
ChartDataSource<Number> yM3s = DataSources.fromNumericCellRange(sheet,
new CellRangeAddress(3, (numOfRows / numOfColumns) + 2, colToWrite, colToWrite));
LineChartSeries chartSeriesM3s = dataM3s.addSeries(xs, yM3s);
chartSeriesM3s.setTitle(metric.getLibelle());
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(1));
//Ceci fonctionne également
//chart1.plot(dataM3s, new ChartAxis[] { chart1.getAxis().get(0), chart1.getAxis().get(1)}); |
Quand je change:
Code:
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(1));
par
Code:
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(2));
ça ne fonctionne pas (idem si je met les 3 axes).
Le fichier xlsx est bien créer mais lorsque j'essaye de l'ouvrir j'ai un message m'indiquant qu'il y du contenu illisible et que si je considère la source sûre je peux l'ouvrir. Ce que je fais, et là j'ai un message comme quoi le fichier est corrompu. Excel a une fois réussi à réparer le fichier et j'ai eu l'erreur suivante:
Enregistrements réparés: Dessin dans la partie /xl/drawings/drawing1.xml (Forme de dessin) error062800_01.xml
d'ailleurs, même si je plotte pas les 3 axes mais seulement deux (0,1) le fichier se corromp. A priori mes axes sont effectivement bien créés mais je ne comprends pas le problème. D'autaznt qu'après avoir testé, même si je modifie la position des axes (bottomAxis a TOP par exemple), l'axe des x est toujours en bas...
Si quelqu'un à une piste pour solutionner tout ça ^^
Merci d'avance!