图表
使用 chart 选项在幻灯片中嵌入图表。图表通过扁平的选项对象配置 — 直接传入 type、categories 和 series。
基本图表
{
"slides": [
{
"children": [
{
"chart": {
"x": "1.3cm",
"y": "1.3cm",
"width": "21.2cm",
"height": "9.3cm",
"type": "column",
"title": "季度销售",
"categories": ["Q1", "Q2", "Q3", "Q4"],
"series": [
{ "name": "营收", "values": [10, 20, 15, 25] },
{ "name": "支出", "values": [8, 12, 10, 14] }
],
"showLegend": true,
"style": 2
}
}
]
}
]
}
import { generatePresentation } from "@office-open/pptx";
generatePresentation({
slides: [
{
children: [
{
chart: {
x: "1.3cm",
y: "1.3cm",
width: "21.2cm",
height: "9.3cm",
type: "column",
title: "季度销售",
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [
{ name: "营收", values: [10, 20, 15, 25] },
{ name: "支出", values: [8, 12, 10, 14] },
],
showLegend: true,
style: 2,
},
},
],
},
],
});
图表类型
type | 说明 |
|---|---|
"column" | 纵向柱状图 |
"bar" | 横向柱状图 |
"line" | 折线图 |
"pie" | 饼图 |
"area" | 面积图 |
"scatter" | 散点图 |
"doughnut" | 圆环图 |
"radar" | 雷达图 |
"bubble" | 气泡图(需 xValues/yValues/bubbleSize) |
"stock" | 股票图(高低收) |
"surface" | 3D 曲面图 |
设置 threeD: true 可将柱状图、条形图、折线图、饼图、面积图转为 3D 变体。
饼图
{
"slides": [
{
"children": [
{
"chart": {
"x": "2.6cm",
"y": "2.6cm",
"width": "15.9cm",
"height": "9.3cm",
"type": "pie",
"title": "市场份额",
"categories": ["Chrome", "Safari", "Firefox", "Edge", "Other"],
"series": [{ "name": "浏览器", "values": [65, 18, 3, 5, 9] }]
}
}
]
}
]
}
import { generatePresentation } from "@office-open/pptx";
generatePresentation({
slides: [
{
children: [
{
chart: {
x: "2.6cm",
y: "2.6cm",
width: "15.9cm",
height: "9.3cm",
type: "pie",
title: "市场份额",
categories: ["Chrome", "Safari", "Firefox", "Edge", "Other"],
series: [{ name: "浏览器", values: [65, 18, 3, 5, 9] }],
},
},
],
},
],
});
折线图
{
"slides": [
{
"children": [
{
"chart": {
"x": "1.3cm",
"y": "1.3cm",
"width": "21.2cm",
"height": "9.3cm",
"type": "line",
"title": "月度营收",
"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
"series": [
{ "name": "2024", "values": [10, 15, 13, 20, 25, 30] },
{ "name": "2025", "values": [12, 18, 16, 22, 28, 35] }
],
"style": 2
}
}
]
}
]
}
import { generatePresentation } from "@office-open/pptx";
generatePresentation({
slides: [
{
children: [
{
chart: {
x: "1.3cm",
y: "1.3cm",
width: "21.2cm",
height: "9.3cm",
type: "line",
title: "月度营收",
categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
series: [
{ name: "2024", values: [10, 15, 13, 20, 25, 30] },
{ name: "2025", values: [12, 18, 16, 22, 28, 35] },
],
style: 2,
},
},
],
},
],
});
散点图
{
"slides": [
{
"children": [
{
"chart": {
"x": "1.3cm",
"y": "1.3cm",
"width": "21.2cm",
"height": "9.3cm",
"type": "scatter",
"series": [
{ "name": "数据集 A", "values": [2, 4, 1, 5, 3] },
{ "name": "数据集 B", "values": [8, 6, 9, 3, 7] }
]
}
}
]
}
]
}
import { generatePresentation } from "@office-open/pptx";
generatePresentation({
slides: [
{
children: [
{
chart: {
x: "1.3cm",
y: "1.3cm",
width: "21.2cm",
height: "9.3cm",
type: "scatter",
series: [
{ name: "数据集 A", values: [2, 4, 1, 5, 3] },
{ name: "数据集 B", values: [8, 6, 9, 3, 7] },
],
},
},
],
},
],
});
Chart 选项
| 属性 | 类型 | 说明 |
| ------------ | ------------------- | ------------------------ | -------- |
| x | number | UniversalMeasure | 水平位置 |
| y | number | UniversalMeasure | 垂直位置 |
| width | number | UniversalMeasure | 宽度 |
| height | number | UniversalMeasure | 高度 |
| type | ChartType | 图表类型字符串(见上表) |
| title | string | 图表标题 |
| categories | string[] | 分类标签 |
| series | ChartSeriesData[] | 数据系列 |
| showLegend | boolean | 显示图例 |
| style | number | 图表样式 ID |
系列数据
| 属性 | 类型 | 说明 |
|---|---|---|
name | string | 系列名称 |
values | number[] | 数据值 |