CORE
Charts
docx 和 pptx 共享的图表组件 — 柱状图、折线图、饼图、面积图和散点图
@office-open/core 提供了 @office-open/docx 和 @office-open/pptx 共享的图表组件。图表通过 ChartSpace 定义,由 ChartCollection 管理。
图表类型
| 类型 | 类 | 说明 |
|---|---|---|
| 柱状图 | BarChart | 垂直或水平柱状 |
| 折线图 | LineChart | 带可选标记的折线系列 |
| 饼图 | PieChart | 饼图或环形图 |
| 面积图 | AreaChart | 填充面积系列 |
| 散点图 | ScatterChart | XY 散点图 |
ChartSpace
ChartSpace 是图表的根元素。它包装图表类型、标题、坐标轴和数据。
import { ChartSpace, BarChart } from "@office-open/core";
const chart = new ChartSpace({
title: { text: "营收" },
chartType: {
type: BarChart,
data: {
categories: ["Q1", "Q2", "Q3", "Q4"],
series: [{ name: "2024", values: [10, 20, 30, 40] }],
},
},
});
ChartCollection
ChartCollection 管理文档或演示文稿中的多个图表:
import { ChartCollection } from "@office-open/core";
const charts = new ChartCollection();
charts.add(chart);
图表标题
import { ChartTitle } from "@office-open/core";
const title = new ChartTitle("销售报告");
系列数据
每种图表类型接受 SeriesData,包含类别和系列值:
const data = {
categories: ["一月", "二月", "三月"],
series: [
{ name: "产品 A", values: [10, 20, 15] },
{ name: "产品 B", values: [5, 12, 8] },
],
};
坐标轴
使用坐标轴的图表(柱状、折线、面积、散点)支持坐标轴配置:
import { CatAx, ValAx } from "@office-open/core";
// 类别轴和数值轴根据图表类型选项自动创建
在文档中使用
图表通常通过文档/演示文稿 API 创建,而非直接使用。参见: