DOCX
图表
在文档中添加柱状图、折线图、饼图、面积图和散点图
使用 ChartCollection 配合 @office-open/core 中的图表类型类来在文档中嵌入图表。
基本柱状图
import { Document, Packer, Paragraph, ChartCollection, BarChart } from "@office-open/docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new ChartCollection({
width: 500,
height: 300,
charts: [
new BarChart({
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [{ name: "Sales", values: [10, 20, 15, 25] }],
}),
],
}),
],
}),
],
},
],
});
多系列图表
new BarChart({
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [
{ name: "Product A", values: [10, 20, 15, 25] },
{ name: "Product B", values: [15, 10, 20, 30] },
],
});
折线图
import { LineChart } from "@office-open/docx";
new LineChart({
categories: ["Jan", "Feb", "Mar", "Apr", "May"],
series: [{ name: "Revenue", values: [100, 150, 200, 180, 250] }],
});
饼图
import { PieChart } from "@office-open/docx";
new PieChart({
categories: ["Desktop", "Mobile", "Tablet"],
series: [{ name: "Traffic", values: [50, 35, 15] }],
});
面积图
import { AreaChart } from "@office-open/docx";
new AreaChart({
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [{ name: "Revenue", values: [100, 200, 150, 250] }],
});
散点图
import { ScatterChart } from "@office-open/docx";
new ScatterChart({
series: [
{
name: "Dataset 1",
xValues: [1, 2, 3, 4, 5],
yValues: [2, 4, 1, 5, 3],
},
],
});