安装
npm install @antv/gpt-vis也可以使用 pnpm add @antv/gpt-vis 或 yarn add @antv/gpt-vis。
绘制图表
在页面中添加可见容器,挂载后再创建实例。GPT-Vis 在浏览器中渲染。
<div id="chart" style="width: 100%; height: 360px;"></div>import { GPTVis } from '@antv/gpt-vis';
const chart = new GPTVis({
container: '#chart',
height: 360,
});
chart.render({
type: 'line',
title: 'Monthly revenue (USD)',
data: [
{ time: 'Jan', value: 12000 },
{ time: 'Feb', value: 18000 },
{ time: 'Mar', value: 15000 },
{ time: 'Apr', value: 24000 },
],
});改用 Syntax
同一个 render() 方法也接受 Syntax 字符串,可根据模型或应用选择格式。
chart.render(`vis line
title Monthly revenue (USD)
data
- time Jan
value 12000
- time Feb
value 18000
- time Mar
value 15000
- time Apr
value 24000`);复用主题
从主题工作台下载 JSON 文件,再传给构造函数。实例会在后续 render() 调用中保留该主题。
import { GPTVis, type ThemeSpec } from '@antv/gpt-vis';
import theme from './my-theme.json';
const chart = new GPTVis({
container: '#chart',
theme: theme as ThemeSpec,
});
chart.render({ type: 'line', data });
// A later render can use another chart type with the same theme.
chart.render({ type: 'table', data });每个实例渲染到一个容器。同时显示多张图表时,为每个容器创建实例,并复用同一主题对象。
清理
组件卸载或不再需要图表时,调用 chart.destroy()。框架生命周期示例参见 React 和 Vue。