API 参考

Chart

本 API 页面目前提供英文正文。代码签名与标识符不随界面语言变化。
Packages@univerjs-pro/engine-chart, @univerjs-pro/sheets-chart, @univerjs-pro/slides-chart, @univerjs-pro/boards-chart, @univerjs-pro/docs-chart, @univerjs-pro/chart-ui

Charts use direct host Facade methods. Create detached chart information with newChart(type), insert it with insertChart(info), and then edit the returned live FChart.

Import @univerjs-pro/engine-chart/facade and the matching host facade. Import @univerjs-pro/chart-ui/facade to add image export.

Host lifecycle

HostCreate and insertRead
SheetsFWorksheet.newChart(type), insertChart(info)getChart(id), getCharts()
SlidesFSlide.newChart(type), insertChart(info)getChart(id), getCharts()
BoardsFBoard.newChart(type), insertChart(info)getChart(id), getCharts()
DocsFDocument.newChart(type), insertChart(info)getChart(id), getCharts()

newChart() returns a type-specific builder. It does not mutate the host until build() is passed to insertChart().

Sheets example

TypeScript
const workbook = univerAPI.getActiveWorkbook()if (!workbook) throw new Error('No active workbook')const worksheet = workbook.getActiveSheet()const info = worksheet  .newChart(univerAPI.Enum.ChartTypeString.Column)  .setSource({    range: 'A1:D6',    orientation: univerAPI.Enum.ChartSourceOrientation.Columns,  })  .setPosition('F2')  .setSize(640, 360)  .setCategoryField(0)  .setValueFields([1, 2, 3])  .setTitle('Quarterly revenue')  .build()const chart = await worksheet.insertChart(info)chart.setTitle('Quarterly revenue (updated)').bringToFront()

Use getChart(id) or getCharts() to resolve live charts. Common setters update an inserted chart immediately; setDataSource(), update(), remove(), and image export are asynchronous.

TypeScript
const updatedInfo = chart.toBuilder(univerAPI.Enum.ChartTypeString.Line).setSubtitle('FY 2026').build()await chart.update(updatedInfo)const svg = await chart.exportImage({ format: 'svg' })

exportImage() returns undefined when the current host has no UI renderer. Call await chart.remove() only when you want to delete the inserted chart.

Register shared themes on univerAPI:

TypeScript
registerTheme(name: string, theme: IEchartTheme): void

Live data sources

FChart.setDataSource() replaces the host source without changing chart configuration. Sheet charts accept a range string. Slide, Board, and Document charts accept inline tables or resource references to a Sheet range or a complete Base table.

TypeScript
await sheetChart.setDataSource('A1:C12')await slideChart.setDataSource([  ['Quarter', 'Revenue'],  ['Q1', 120],  ['Q2', 180],])await boardChart.setDataSource({  unit: { selector: 'base-unit-id', type: 'base' },  part: {    kind: 'table',    tableId: 'tasks-table-id',    tableName: 'Tasks',  },})

Referenced Sheet sources use a range part with the sheet identity and A1 range. Referenced Base sources currently select a complete table.

Detached builders

APIMethods
Builder lifecyclesetSource, setAbsolutePosition, setSize, build
Titles and legendsetTitle, clearTitle, setSubtitle, clearSubtitle, setLegend, clearLegend
Appearance and datasetTheme, setPalette, setAppearance, setAggregation, setCategoryField, setCategoryFields, setMultiLevelCategoryAxis, setValueFields, setInvalidValueStrategy, setAutoGradientFill
Type-specific buildersAxis, series, line, pie, bubble, candlestick, histogram, hierarchy, gauge, and relation-chart methods

Builder methods only change a detached draft. build() returns an insertable information snapshot.

Multi-level category axes

When adjacent source fields form a hierarchy, pass their zero-based indexes to setCategoryFields() in root-to-leaf order, then enable the multi-level axis.

TypeScript
const info = worksheet  .newChart(univerAPI.Enum.ChartTypeString.Column)  .setSource({    range: 'A1:D7',    orientation: univerAPI.Enum.ChartSourceOrientation.Columns,  })  .setCategoryFields([0, 1])  .setMultiLevelCategoryAxis(true)  .setValueFields([2, 3])  .setPosition('F2')  .build()const chart = await worksheet.insertChart(info)chart.setCategoryFields([0, 1]).setMultiLevelCategoryAxis(true)

clearCategoryFields() removes the ordered category mapping. setMultiLevelCategoryAxis(false) only switches the hierarchical rendering off and preserves the selected fields.

Live charts

APIMethods
Live inspectiongetId, getType, getInfo, toBuilder
Immediate updatessetType, setCategoryFields, clearCategoryFields, setMultiLevelCategoryAxis, the other shared configuration setters, setAbsolutePosition, setSize
Asynchronous updatessetDataSource, update, remove
OrdersetZOrder, bringToFront, bringForward, sendBackward, sendToBack
Image exportexportImage(options?) for a PNG or SVG data URL; requires @univerjs-pro/chart-ui/facade

Host-specific builders

HostSource and placement
SheetsA1 ranges or range specs; setPosition(anchor)
SlidesInline values or a Sheet resource reference; setPlaceholder, setStroke, setZOrder
BoardsInline values or a Sheet resource reference; setContainer, setLane, setZOrder
DocsInline values or a Sheet resource reference; setPosition, setLayout, setInline, setFloating

Slides, Boards, and Docs can reference a Sheet range instead of copying values:

TypeScript
const info = slide  .newChart(univerAPI.Enum.ChartTypeString.Column)  .setSource({    unit: { selector: 'sales-workbook', type: 'sheet' },    part: {      kind: 'range',      sheetName: 'Data',      range: 'A1:B20',      ref: 'Data!A1:B20',    },  })  .setAbsolutePosition(120, 80)  .setSize(640, 360)  .build()const chart = await slide.insertChart(info)

Source: @univerjs-pro/engine-chart, @univerjs-pro/sheets-chart, @univerjs-pro/slides-chart, @univerjs-pro/boards-chart, @univerjs-pro/docs-chart, @univerjs-pro/chart-ui

你觉得这篇文档如何?

© 2026 DreamNum Co., Ltd.