I am using the apache poi
library, version apache poi 4.1.1
. I create a scattering chart using the example ScatterChart.java
of https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ but there are line instead dot in chart.
Any suggestion?
This is what I get:
This is what I want:
The default scatter chart in Excel
always has lines between the data points.
If you are looking at the example code, there is a method private static void solidLineSeries
which formats those lines to have colored fill.
What you need is a private static void setLineNoFill
which sets a not filled line between the data points.
Additional you need set XDDFScatterChartData.Series
marker style and marker size using XDDFScatterChartData.Series.setMarkerStyle and XDDFScatterChartData.Series.setMarkerSize.
Complete Example:
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.*;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.*;
/**
* Illustrates how to create a simple scatter chart.
*/
public class ScatterChart {
public static void main(String[] args) throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet("Sheet 1");
final int NUM_OF_ROWS = 3;
final int NUM_OF_COLUMNS = 10;
// Create a row and put some cells in it. Rows are 0 based.
Row row;
Cell cell;
for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex);
cell.setCellValue(colIndex * (rowIndex + 1.0));
}
}
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
XSSFChart chart = drawing.createChart(anchor);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("x"); // https://stackoverflow.com/questions/32010765
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("f(x)");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFDataSource<Double> xs = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
XDDFNumericalDataSource<Double> ys2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
XDDFScatterChartData data = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);
XDDFScatterChartData.Series series1 = (XDDFScatterChartData.Series) data.addSeries(xs, ys1);
series1.setTitle("2x", null); // https://stackoverflow.com/questions/21855842
series1.setSmooth(false); // https://stackoverflow.com/questions/39636138
series1.setMarkerStyle(MarkerStyle.CIRCLE);
series1.setMarkerSize((short)5);
setLineNoFill(series1);
XDDFScatterChartData.Series series2 = (XDDFScatterChartData.Series) data.addSeries(xs, ys2);
series2.setTitle("3x", null);
series2.setMarkerStyle(MarkerStyle.CIRCLE);
series2.setMarkerSize((short)5);
setLineNoFill(series2);
chart.plot(data);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx")) {
wb.write(fileOut);
}
}
}
private static void setLineNoFill(XDDFScatterChartData.Series series) {
XDDFNoFillProperties noFillProperties = new XDDFNoFillProperties();
XDDFLineProperties lineProperties = new XDDFLineProperties();
lineProperties.setFillProperties(noFillProperties);
XDDFShapeProperties shapeProperties = series.getShapeProperties();
if (shapeProperties == null) shapeProperties = new XDDFShapeProperties();
shapeProperties.setLineProperties(lineProperties);
series.setShapeProperties(shapeProperties);
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments