构建XSSFScatterChartData
并使用方法XSSFChart.plot(ChartData data, ChartAxis... chartAxis)
填充后,该图包含标记,但是通过一行链接..
我认为问题来自方法XSSFScatterChartData.addStyle
,它默认设置STScatterStyle.LINE_MARKER
。
以下是我用于生成图表的方法的副本:
private void setTrainingTimeGraph(Sheet trainingTimeSheet, Sheet resultsSheet) {
Drawing drawing = trainingTimeSheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 5, 5, 20, 30);
XSSFChart chart = (XSSFChart) drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
chart.setTitleText("Training time over Fscore");
XSSFScatterChartData data = chart.getChartDataFactory().createScatterChartData();
ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
setValueAxisTitle((XSSFChart) chart,0,"Fscore");
setValueAxisTitle((XSSFChart) chart,1, "Training Time");
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(resultsSheet, new CellRangeAddress(16, 29, 10, 10));
ChartDataSource<Number> ys = DataSources.fromNumericCellRange(resultsSheet, new CellRangeAddress(16, 29, 18, 18));
data.addSerie(xs, ys);
chart.plot(data,bottomAxis, leftAxis);
}
所以添加@AxelRichter代码设置为不填充我的散点图数据系列:
...
data.addSerie(xs, ys);
chart.plot(data,bottomAxis, leftAxis);
//set line properties of first scatter chart data serie to no fill:
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.addNewSpPr().addNewLn().addNewNoFill();
我设法摆脱连接标记的线。最后!
但第二部分并不是我想要的。让我更好地解释一下:
当我在我的scatterPlot中悬停每个点时,会弹出一些文字("label/x_value"
,x_value
,y_value
)。图例中的值与其"label/x_value"
相同。我想为每个数据点和图例中的每个值设置"label/x_value"
。
提前致谢!
答案 0 :(得分:1)
String videoID = (String) ((JSONObject) ((JSONObject) jsonObject.getJSONArray("items").get(0)).get("id")).get("videoId");
散点图也总是在标记之间有一条线。如果你不想看到这条线,它的填充颜色必须设置为无填充。
参考文献: CTScatterSer - &gt; CTShapeProperties - &gt; CTLineProperties - &gt; CTLineProperties.addNewNoFill
对于拥有数据标签,系列需要CTDLbl设置。
完整示例:
Excel
此代码需要FAQ-N10025中提到的所有模式import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.ss.util.CellRangeAddress;
public class CreateExcelScatterChart {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("chart");
final int NUM_OF_ROWS = 2;
final int NUM_OF_COLUMNS = 20;
Row row;
Cell cell;
for (int rowIndex = 0; rowIndex < 1; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex);
cell.setCellValue(4*colIndex * (rowIndex + 1));
}
}
for (int rowIndex = 1; 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(Math.sin(Math.PI*colIndex/10*2));
}
}
Drawing<?> drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 3, 13, 20);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
ScatterChartData data = chart.getChartDataFactory().createScatterChartData();
ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
ScatterChartSeries chartSerie = data.addSerie(xs, ys1);
chartSerie.setTitle("My Title");
chart.plot(data, bottomAxis, leftAxis);
//set line properties of first scatter chart data serie to no fill:
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.addNewSpPr().addNewLn().addNewNoFill();
//set data labels for first scatter chart data serie
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.addNewDLbls()
.addNewNumFmt().setFormatCode("0.0");
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.getDLbls().getNumFmt()
.setSourceLinked(false);
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.getDLbls()
.addNewShowLegendKey().setVal(false);
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.getDLbls()
.addNewShowCatName().setVal(false);
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.getDLbls()
.addNewShowSerName().setVal(false);
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.getDLbls()
.addNewShowPercent().setVal(false);
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.getDLbls()
.addNewShowBubbleSize().setVal(false);
((XSSFChart)chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray(0)
.getDLbls()
.addNewShowVal().setVal(true);
wb.write(new FileOutputStream("CreateExcelScatterChart.xlsx"));
wb.close();
}
}
的完整jar。
此代码使用当前的上一个稳定版本ooxml-schemas-1.3.jar
版本apache poi
。注意:3.17
的{{1}}代码现在正在开发中。因此代码可能无法在其他版本中使用。
答案 1 :(得分:0)
所以基于@Axel Richter的回复。
以下是用作基础的代码:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.charts.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.ss.util.CellRangeAddress;
public class CreateExcelScatterChart {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("chart");
final int NUM_OF_ROWS = 2;
final int NUM_OF_COLUMNS = 20;
Row row;
Cell cell;
//x values
for (int rowIndex = 0; rowIndex < 1; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex);
cell.setCellValue(4*colIndex * (rowIndex + 1));
}
}
// y values
for (int rowIndex = 1; 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(Math.sin(Math.PI*colIndex/10*2));
}
}
Drawing<?> drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 3, 13, 20);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
ScatterChartData data = chart.getChartDataFactory().createScatterChartData();
ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);
for (int i = 0; i < NUM_OF_COLUMNS; i++) {
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, i, i));
ChartDataSource<Number> ys = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, i, i));
ScatterChartSeries chartSerie = data.addSerie(xs, ys);
chartSerie.setTitle("My Title " + i);
}
chart.plot(data, bottomAxis, leftAxis);
//set line properties of first scatter chart data serie to no fill:
CTScatterSer[] scatterChartSeries = ((XSSFChart) chart).getCTChart().getPlotArea().getScatterChartArray(0).getSerArray();
for (int i = 0; i < scatterChartSeries.length; i++) {
scatterChartSeries[i].addNewSpPr().addNewLn().addNewNoFill();
}
wb.write(new FileOutputStream("CreateExcelScatterChart.xlsx"));
wb.close();
}
}
当传递悬停时,它将生成20个系列,每个系列都带有标题,并且图例中的标题相同。每个标记之间没有线条。所以基本上它就是我正在寻找的东西。
感谢您的回答。
Ps:如果你想在每个标记上使用一些标签,请按@Richet答案上的//set data labels for first scatter chart data serie