Apache POI 管理散点图
Apache POI Manage Scatter Chart
我正在使用 apache poi
库,版本 apache poi 4.1.1
。
我使用 https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ 的示例 ScatterChart.java
创建了一个散点图,但图表中有线而不是点。
有什么建议吗?
这是我得到的:
这就是我想要的:
Excel
中的默认散点图总是在数据点之间有线条。
如果您正在查看示例代码,有一种方法 private static void solidLineSeries
可以将这些行格式化为具有彩色填充。
您需要的是 private static void setLineNoFill
,它在数据点之间设置一条未填充的线。
您还需要使用 XDDFScatterChartData.Series.setMarkerStyle and XDDFScatterChartData.Series.setMarkerSize.
设置 XDDFScatterChartData.Series
标记样式和标记大小
完整示例:
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"); //
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); //
series1.setSmooth(false); //
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);
}
}
我正在使用 apache poi
库,版本 apache poi 4.1.1
。
我使用 https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/ 的示例 ScatterChart.java
创建了一个散点图,但图表中有线而不是点。
有什么建议吗?
这是我得到的:
这就是我想要的:
Excel
中的默认散点图总是在数据点之间有线条。
如果您正在查看示例代码,有一种方法 private static void solidLineSeries
可以将这些行格式化为具有彩色填充。
您需要的是 private static void setLineNoFill
,它在数据点之间设置一条未填充的线。
您还需要使用 XDDFScatterChartData.Series.setMarkerStyle and XDDFScatterChartData.Series.setMarkerSize.
设置XDDFScatterChartData.Series
标记样式和标记大小
完整示例:
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"); //
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); //
series1.setSmooth(false); //
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);
}
}