DOCX
图表
在文档中添加图表 — 柱状图、折线图、饼图、圆环图、雷达图、气泡图等
使用 chart 在文档中嵌入图表。图表使用扁平的选项对象 — 直接传入 type、categories 和 series。
基本柱状图
{
"sections": [
{
"children": [
{
"paragraph": {
"children": [
{
"chart": {
"type": "column",
"categories": ["Q1", "Q2", "Q3", "Q4"],
"series": [{ "name": "Sales", "values": [10, 20, 15, 25] }],
"title": "季度销售额",
"transformation": { "width": "13.2cm", "height": "7.9cm" }
}
}
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
paragraph: {
children: [
{
chart: {
type: "column",
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [{ name: "Sales", values: [10, 20, 15, 25] }],
title: "季度销售额",
transformation: { width: "13.2cm", height: "7.9cm" },
},
},
],
},
},
],
},
],
});
图表类型
type 属性选择图表类型:
| 类型 | 值 |
|---|---|
| 柱状图 | "column" |
| 条形图 | "bar" |
| 折线图 | "line" |
| 饼图 | "pie" |
| 面积图 | "area" |
| 散点图 | "scatter" |
| 圆环图 | "doughnut" |
| 雷达图 | "radar" |
| 气泡图 | "bubble" |
| 股票图 | "stock" |
| 曲面图 | "surface" |
设置 threeD: true 可将柱状图、条形图、折线图、饼图、面积图转为 3D 变体。
多系列图表
{
"sections": [
{
"children": [
{
"paragraph": {
"children": [
{
"chart": {
"type": "column",
"categories": ["Q1", "Q2", "Q3", "Q4"],
"series": [
{ "name": "产品 A", "values": [10, 20, 15, 25] },
{ "name": "产品 B", "values": [15, 10, 20, 30] }
],
"transformation": { "width": "13.2cm", "height": "7.9cm" }
}
}
]
}
}
]
}
]
}
{
chart: {
type: "column",
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [
{ name: "产品 A", values: [10, 20, 15, 25] },
{ name: "产品 B", values: [15, 10, 20, 30] },
],
transformation: { width: "13.2cm", height: "7.9cm" },
},
}
折线图
{
"sections": [
{
"children": [
{
"paragraph": {
"children": [
{
"chart": {
"type": "line",
"categories": ["一月", "二月", "三月", "四月", "五月"],
"series": [{ "name": "收入", "values": [100, 150, 200, 180, 250] }],
"transformation": { "width": "13.2cm", "height": "7.9cm" }
}
}
]
}
}
]
}
]
}
{
chart: {
type: "line",
categories: ["一月", "二月", "三月", "四月", "五月"],
series: [{ name: "收入", values: [100, 150, 200, 180, 250] }],
transformation: { width: "13.2cm", height: "7.9cm" },
},
}
饼图
{
"sections": [
{
"children": [
{
"paragraph": {
"children": [
{
"chart": {
"type": "pie",
"categories": ["桌面端", "移动端", "平板"],
"series": [{ "name": "流量", "values": [50, 35, 15] }],
"transformation": { "width": "10.6cm", "height": "7.9cm" }
}
}
]
}
}
]
}
]
}
{
chart: {
type: "pie",
categories: ["桌面端", "移动端", "平板"],
series: [{ name: "流量", values: [50, 35, 15] }],
transformation: { width: "10.6cm", height: "7.9cm" },
},
}
散点图
散点图使用 xValues 和 yValues 而非 categories:
{
"sections": [
{
"children": [
{
"paragraph": {
"children": [
{
"chart": {
"type": "scatter",
"categories": [],
"series": [
{
"name": "数据集 1",
"values": [],
"xValues": [1, 2, 3, 4, 5],
"yValues": [2, 4, 1, 5, 3]
}
],
"transformation": { "width": "13.2cm", "height": "7.9cm" }
}
}
]
}
}
]
}
]
}
{
chart: {
type: "scatter",
categories: [],
series: [
{
name: "数据集 1",
values: [],
xValues: [1, 2, 3, 4, 5],
yValues: [2, 4, 1, 5, 3],
},
],
transformation: { width: "13.2cm", height: "7.9cm" },
},
}
chart 选项
| 选项 | 类型 | 说明 |
|---|---|---|
type | string | 图表类型(见上表) |
categories | string[] | 类别标签 |
series | { name, values }[] | 系列数据 |
transformation | { width, height } | 显示尺寸 |
title | string | 图表标题 |
showLegend | boolean | 是否显示图例(默认:true) |
style | number | 样式预设(2–48) |
threeD | boolean | 启用 3D 渲染 |
floating | object | 浮动定位选项 |