有一个很好的例子展示如何使用apache POI在PPT上绘制折线图吗?
我正在使用 apache POIs XSLFSlide 生成 PPT。我没有看到使用 XSLFSlide 绘制线条的示例。
总的来说,这个条形图具有相同的概念。改成多行就好了
public static void main(String[] args) throws Exception {
HSLFSlideShow ppt = new HSLFSlideShow();
try {
//bar chart data. The first value is the bar color, the second is the width
Object[] def = new Object[]{
Color.yellow, 40,
Color.green, 60,
Color.gray, 30,
Color.red, 80,
};
HSLFSlide slide = ppt.createSlide();
HSLFGroupShape group = new HSLFGroupShape();
//define position of the drawing in the slide
Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
group.setAnchor(bounds);
group.setInteriorAnchor(new java.awt.Rectangle(0, 0, 100, 100));
slide.addShape(group);
Graphics2D graphics = new PPGraphics2D(group);
//draw a simple bar graph
int x = 10, y = 10;
graphics.setFont(new Font("Arial", Font.BOLD, 10));
for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
graphics.setColor(Color.black);
int width = ((Integer)def[i+1]).intValue();
graphics.drawString("Q" + idx, x-5, y+10);
graphics.drawString(width + "%", x + width+3, y + 10);
graphics.setColor((Color)def[i]);
graphics.fill(new Rectangle(x, y, width, 10));
y += 15;
}
graphics.setColor(Color.black);
graphics.setFont(new Font("Arial", Font.BOLD, 14));
graphics.draw(group.getInteriorAnchor());
graphics.drawString("Performance", x + 30, y + 10);
FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
ppt.write(out);
out.close();
} finally {
ppt.close();
}
}
下面是使用 Apache POI 5.2.5 创建折线图的代码片段(代码位于
Kotlin
,但可以使用 IDEA 或在线工具轻松移植到 Java)。
该函数返回 ByteArrayOutputStream
,因此调用函数可以保存文件(使用 FileOutputStream
)或直接响应 HTTP 请求(使用正确的响应标头)。
fun getSlideWithLineChart(): ByteArrayOutputStream {
val slideShow = XMLSlideShow()
val slide = slideShow.createSlide()
val categories = arrayOf("02/01/2023", "03/01/2023", "04/01/2023", "05/01/2023", "06/01/2023", "07/01/2023")
val numOfPoints = categories.size
val values = arrayOf(40.0, 55.0, 65.0, 75.0, 75.0, 30.0)
val chart = slideShow.createChart()
slide.addChart(
chart,
Rectangle2D.Double(
1.0 * Units.EMU_PER_CENTIMETER,
1.0 * Units.EMU_PER_CENTIMETER,
20.0 * Units.EMU_PER_CENTIMETER,
15.0 * Units.EMU_PER_CENTIMETER
)
)
// data sources
val categoryDataRange = chart.formatRange(CellRangeAddress(1, numOfPoints, 0, 0))
val categoriesData: XDDFDataSource<String> =
XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0)
val c = values.size + 1
// data source
val valueDataRange = chart.formatRange(CellRangeAddress(1, numOfPoints, c, c))
val valueData = XDDFDataSourcesFactory.fromArray(values, valueDataRange, c)
val bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM)
val leftAxis = chart.createValueAxis(AxisPosition.LEFT)
// set cross axis
bottomAxis.crossAxis(leftAxis)
leftAxis.crossAxis(bottomAxis)
val data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis)
data.setVaryColors(false)
val series = data.addSeries(categoriesData, valueData)
(series as XDDFLineChartData.Series).isSmooth = true
series.setMarkerStyle(MarkerStyle.CIRCLE)
// plot chart
chart.plot(data)
val byteArrayOS = ByteArrayOutputStream()
slideShow.write(byteArrayOS)
return byteArrayOS
}
希望这有帮助。