Java Apache POI管理散点图
我正在使用ApachePOIJava Apache POI管理散点图,java,charts,apache-poi,scatter-plot,Java,Charts,Apache Poi,Scatter Plot,我正在使用ApachePOI库,版本为ApachePOI4.1.1。 我使用的示例ScatterChart.java创建了一个散射图,但图中有线而不是点 有什么建议吗 这就是我得到的: 这就是我想要的: Excel中的默认散点图在数据点之间总是有线条 如果您正在查看示例代码,则有一个方法private static void solidLineSeries将这些行格式化为具有彩色填充 您需要的是一个私有静态void setLineNoFill,它在数据点之间设置一条未填充的线 另外,您需要使用
库,版本为ApachePOI4.1.1
。
我使用的示例ScatterChart.java
创建了一个散射图,但图中有线而不是点
有什么建议吗
这就是我得到的:
这就是我想要的:
Excel中的默认散点图在数据点之间总是有线条 如果您正在查看示例代码,则有一个方法
private static void solidLineSeries
将这些行格式化为具有彩色填充
您需要的是一个私有静态void setLineNoFill
,它在数据点之间设置一条未填充的线
另外,您需要使用和设置标记样式和标记大小
完整示例:
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);
}
}
import java.io.FileOutputStream;
导入java.io.IOException;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.CellRangeAddress;
导入org.apache.poi.xddf.usermodel.*;
导入org.apache.poi.xddf.usermodel.chart.*;
导入org.apache.poi.xssf.usermodel.*;
/**
*演示如何创建简单的散点图。
*/
公共类散点图{
公共静态void main(字符串[]args)引发IOException{
尝试(XSSFWorkbook wb=new XSSFWorkbook()){
XSSFSheet sheet=wb.createSheet(“第1张”);
行的最终整数=3;
_列的最终int NUM_=10;
//创建一行并在其中放置一些单元格。行基于0。
行行;
细胞;
for(int-rowIndex=0;rowIndex
答案取决于创建散点图所使用的apache poi版本和代码。请在您的问题中提供这两个。嗨,Axel,我使用的是ApachePOI的4.1.1版本。请编辑您的问题并提供所有必要的信息。什么代码用于创建散点图?请在您的问题中提供该代码。嗨,阿克塞尔,在我的代码中,setLineNoFill遗漏了。谢谢