POI Line chart axis position and other issues - java

I'm dealing with POI to create charts and I've some issues.
First, even if I change the AxisPosition I always have x-axis at bottom and y-axis at left. Second I would like to have 2 y-axis. Here's a part of my code:
Function to create the chart:
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 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;
}
Code to plot chart
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));
//By the way, this works as well:
//chart1.plot(dataM3s, new ChartAxis[] { chart1.getAxis().get(0), chart1.getAxis().get(1)});
When I change
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(1));
by
chart1.plot(dataOther, chart1.getAxis().get(0), chart1.getAxis().get(2));
I can't open my xlsx file (and as soon as try to create a second value axis): i got message like "unreadable content". sometimes excel suceed in reparing and tells me it repared the drawing shape in /xl/drawings/drawing1.xml. I got this error: error062800_01.xml but not familiar with XML.
If anyone has some idea

Related

How to set Text direction of chart to rotate all text 270 using poi?

In the excel, here's what I expect, as shown below
enter image description here
In the excel, there is a data labels on the chart, the values displayed default to horizontal, I can set text direction to rotate all text 270 in the text options of format data labels. but, I don't know how to implement this in code with apache poi? Could anyone help?
Here is the code:
XSSFDrawing drawing = (XSSFDrawing)sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 27, 5);
XSSFChart chart = drawing.createChart(anchor);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("Week");
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("Face Amount ($MM)");
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
leftAxis.setMaximum(10000);
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange((XSSFSheet) sheet,
new CellRangeAddress(299, 299, 0, NUM_OF_COLUMNS - 1));
XDDFNumericalDataSource<Double> ys = XDDFDataSourcesFactory.fromNumericCellRange((XSSFSheet) sheet,
new CellRangeAddress(300, 300, 0, NUM_OF_COLUMNS - 1));
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
XDDFChartData.Series series1 = data.addSeries(xs, ys);
series1.setTitle("2x", null);
chart.plot(data);
XDDFBarChartData bar = (XDDFBarChartData) data;
bar.setBarDirection(BarDirection.COL);
bar.setGapWidth(3);
bar.setBarGrouping(BarGrouping.STACKED);
//set data labels
XSSFChart xssfChart = (XSSFChart) chart;
CTPlotArea plotArea = xssfChart.getCTChart().getPlotArea();
CTBoolean ctBool = CTBoolean.Factory.newInstance();
ctBool.setVal(true);
plotArea.getBarChartArray(0).getSerArray(0).addNewDLbls().setShowVal(ctBool);
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowLeaderLines();
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().setShowLeaderLines(ctBool);
ctBool.setVal(false);
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().setShowSerName(ctBool);
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().setShowPercent(ctBool);
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().setShowLegendKey(ctBool);
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().setShowCatName(ctBool);
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().setShowLeaderLines(ctBool);
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().setShowBubbleSize(ctBool);
As your code shows, you are setting the data labels using the underlying low level beans of apache poi.
But how can one working with those? There is no documentation about org.openxmlformats.schemas.drawingml.x2006.chart.* classes public available. So we need downloading the sources of ooxml-schemas from maven for example. Then we can use javadoc to create a API documentation. Now we can look how to create and use the classes.
But also we need the meanings of the XML elements and attributes, the classes are creating. For this one could study the Office Open XML specifications. But my preferred method is to create a simple *.xlsx file having the needed settings using Excel's GUI. Then unzip that *.xlsx file and have a look at the XML in the *.xml files stored in that ZIP archive. For the first chart this is /xl/charts/chart1.xml.
Doing this we will find:
<c:dLbls>
<c:txPr>
<a:bodyPr rot="-5400000"/>
<a:p><a:pPr><a:defRPr/></a:pPr></a:p>
</c:txPr>
...
</c:dLbls>
for data labels when text rotation is set.
That is a txPr (text properties) element in dLbls having a bodyPr (body properties) element having a rot attribute set. Value of the rot attribute is rotation angle * 60000. The -5400000 for example is -90.00 * 60000. Also there is a p (paragraph) element having a pPr (paragraph properties) element having a defRPr (default run properties) to set that the data label text runs have default properties.
Putting all together the code used in apache poi for this would be:
...
// text properties having rotation set
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewTxPr()
.addNewBodyPr().setRot((int)(-90.00 * 60000));
// paragraph properties having default run properties set
plotArea.getBarChartArray(0).getSerArray(0).getDLbls().getTxPr()
.addNewP().addNewPPr().addNewDefRPr();
...
where plotArea is CTPlotArea as in your code. And the first series must have dLbls set using addNewDLbls as you done in your code.

Apache POI Charts - Title formatting

I created a line chart and when I add the title to my chart it overlaps my data. How do I add the title above my chart? Also how can I adjust the font/style of the title text? I want to make the text a little smaller.
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
Chart chart = drawing.createChart(anchor);
chart.setTitleText("This is my title");
// Use a category axis for the bottom axis.
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
ChartDataSource<Integer> test = DataSources.fromArray([2011,2012,2013,2014,2015,2016,2017] as Integer[]);
ChartDataSource<Integer> test2 = DataSources.fromArray([4805, 7351, 5333, 7183, 6230, 4050, 6963] as Integer[]);
LineChartData data = chart.getChartDataFactory().createLineChartData();
data.addSeries(test, test2);
chart.plot(data, bottomAxis, leftAxis);
So from this example what I looking for is extra padding/margin above the 8000 where my title would go.
So you don't want the title overlaying the plot area?
The problem is that apache poi was tested using Excel 2007. But after this version multiple default settings have been changed in later versions.
The setting of overlays for example had defaulted to false (do not overlay) in Excel 2007 if it was not explicit set. This was a good choice in my opinion. In later versions the default is true (do overlay) if it is not explicit set. That's nonsense in my opinion. But who cares my opinion.
So if we not want the title overlaying the plot area, we have to set this explicitly.
Styling the title font is possible only using the low level underlying objects. Using this we need add run properties to the title's first paragraph and first text run. Then we can set bold, italic and font size (unit 1/100 pt). And then we add type face for latin and complex script characters.
Example code using Java. (The code in the question seems to be Groovy, but it is not tagged as such and there is no answer to my question about this discrepancy.)
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
public class LineChartProblem {
public static void main(String[] args) throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet("linechart");
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
Chart chart = drawing.createChart(anchor);
((XSSFChart)chart).setTitleText("This is my title");
//set "the title overlays the plot area" to false explicitly
((XSSFChart)chart).getCTChart().getTitle().addNewOverlay().setVal(false);
//set font style for title - low level
//add run properties to title's first paragraph and first text run. Set bold.
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).addNewRPr().setB(true);
//set italic
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().setI(true);
//set font size 20pt
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().setSz(2000);
//add type face for latin and complex script characters
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().addNewLatin().setTypeface("Times New Roman");
((XSSFChart)chart).getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getRPr().addNewCs().setTypeface("Times New Roman");
// Use a category axis for the bottom axis.
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
ChartDataSource<Integer> test = DataSources.fromArray(new Integer[]{2011,2012,2013,2014,2015,2016,2017});
ChartDataSource<Integer> test2 = DataSources.fromArray(new Integer[]{4805, 7351, 5333, 7183, 6230, 4050, 6963});
LineChartData data = chart.getChartDataFactory().createLineChartData();
data.addSeries(test, test2);
chart.plot(data, bottomAxis, leftAxis);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
wb.write(fileOut);
}
}
}
}

How to fixate the domain range of a JFreeChart XY diagram?

I have a JFreeChart chart, which displays measurements from a sensor. The diagram should show how those values change over time.
I create the diagram using following code:
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart(
"Pulse sensor data", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setDrawSeriesLineAsPath(true);
renderer.setSeriesLinesVisible(0, true);
renderer.setSeriesShapesVisible(0, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
There is a problem with this implementation - the domain axis constantly changes, hence, the representation of the signals changes as well. Therefore, it is hard to tell visually whether the signal has changed significantly.
At the start of a measurement session the domain ranges from 0 to approx. 550.
After some time I get more data and now the maximum value is 35000.
If we wait longer, it changes again (actually, with every received measurement a litlle).
How can I change the code such that the diagram shows last X measurements (i. e. the size of the domain axis remains constants) and kind of slides over the time?
Update 1 (01.06.2013 18:06 MSK): I changed the code for adding a new data item to
sensorSeries.add(new Millisecond(new Date()), voltage);
where voltage is the new sensor value.
The code for setting up the chart has also changed:
dataset = new TimeSeriesCollection();
sensorSeries = new TimeSeries("Pulse sensor data");
sensorSeries.setMaximumItemAge(12500);
dataset.addSeries(sensorSeries);
final JFreeChart chart = createChart(dataset);
[...]
private JFreeChart createChart(final XYDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Pulse sensor data", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
true, // include legend
true, // tooltips
false // urls
);
final XYPlot plot = chart.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
final ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setAutoRange(true);
return chart;
}
This works fine except that I sometimes get following exception:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1201, Size: 1201
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.jfree.data.time.TimeSeries.getRawDataItem(TimeSeries.java:422)
at org.jfree.data.time.TimeSeries.getTimePeriod(TimeSeries.java:454)
at org.jfree.data.time.TimeSeriesCollection.getXValue(TimeSeriesCollection.java:428)
at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawPrimaryLine(XYLineAndShapeRenderer.java:987)
at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawItem(XYLineAndShapeRenderer.java:913)
at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3828)
at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3389)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1237)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1672)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
Any ideas how to fix it?
How can I execute adding the data in the event dispatch thread?
As shown here, SwingWorker is ideal for this. Poll or listen to your data source in the worker's doInBackground() method and update the chart's model in process(), which executes on the event dispatch thread.

How to display jfreechart at specified place in jsp

I have a jsp page where i need to display the chart at specified position .But With the following codes chart is getting loaded as png image and all other css design elements of the jsp page is not displaying....
Below is my code.
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("STD", 10);
dataset.setValue("Local", 15);
dataset.setValue("ISD", 47);
dataset.setValue("Inet", 20);
JFreeChart chart = ChartFactory.createPieChart3D(
"Calls by Type", // Title
dataset, // Data
true, // Yes, display the legend
true, // No, don't display tooltips
true // No, no URLs
);
PiePlot3D plot4 = (PiePlot3D) chart.getPlot();
plot4.setForegroundAlpha(0.6f);
plot4.setBackgroundPaint(Color.cyan);
PiePlot piePlot = (PiePlot) chart.getPlot();
StandardPieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator("{0} {1} {2}");
piePlot.setLabelGenerator(labelGenerator);
piePlot.setLegendLabelGenerator(labelGenerator);
response.setContentType("html");
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 400, 300);

Only bars in a JfreeChart graph

This might sound silly, but I'd only want to display bars in a JfreeChart and a transparent background. All examples show how to create a PNG with transparent background but this is not what I want, I just want to display it, no need to create it.
Besides, I find no way to "disable" text in horizontal and vertical axis. I just want the lines for the axis and the bars, simple as that. Here's the code:
private static JFreeChart createActivityBarGraph(CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart(
null, // chart title
null, // domain axis label
null, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
false, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart to be transparent
chart.setBackgroundPaint( new Color(255,255,255,0) );
chart.setBorderVisible(false);
CategoryPlot cPlot = chart.getCategoryPlot();
cPlot.setBackgroundPaint( new Color(255,255,255,0) );
cPlot.setBackgroundAlpha(0.0f);
cPlot.setDomainGridlinePaint(Color.white);
cPlot.setDomainGridlinesVisible(false);
cPlot.setRangeGridlinePaint(Color.white);
cPlot.setOutlineVisible(false);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) cPlot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
BarRenderer renderer = (BarRenderer) cPlot.getRenderer();
renderer.setDrawBarOutline(true);
// set up gradient paints for series...
GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, new Color(0, 0, 64)
);
renderer.setSeriesPaint(0, gp0);
CategoryAxis domainAxis = cPlot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
That said, any help is appreciated.
You can use setTickLabelsVisible(false) and setTickMarksVisible(false) to hide the text on the axes.
try this
chart.getCategoryPlot().setBackgroundPaint(new Color(0,0,0,0));

Categories

Resources