vchart-svg-plugin 介绍
关于 vchart-svg-plugin
vchart-svg-plugin是将 vchart 渲染内容转换成 svg 字符串的插件;主要用于打印、服务端渲染等场景;
快速上手
📦 安装
# npm $ npm install @visactor/vchart-svg-plugin # yarn $ yarn add @visactor/vchart-svg-plugin
📊 一个简单的图表
import VChart from '@visactor/vchart';
import { convertVChartToSvg } from '@visactor/vchart-svg-plugin';
const spec = {
type: 'pie',
data: [
{
id: 'id0',
values: [
{ type: 'oxygen', value: '46.60' },
{ type: 'silicon', value: '27.72' },
{ type: 'aluminum', value: '8.13' },
{ type: 'iron', value: '5' },
{ type: 'calcium', value: '3.63' },
{ type: 'sodium', value: '2.83' },
{ type: 'potassium', value: '2.59' },
{ type: 'others', value: '3.5' }
]
}
],
outerRadius: 0.8,
valueField: 'value',
categoryField: 'type',
title: {
visible: true,
text: 'Statistics of Surface Element Content'
},
legends: {
visible: true,
orient: 'left'
},
label: {
visible: true
},
tooltip: {
mark: {
content: [
{
key: datum => datum['type'],
value: datum => datum['value'] + '%'
}
]
}
}
};
const vchart = new VChart(spec, {
dom: 'chart-container',
animation: false // 注意,不要开启动画,不然需要监听动画结束事件,再进行svg转换
});
vchart.renderSync();
const svgContent = convertVChartToSvg(vchart);
node 端渲染
const VChart = require('@visactor/vchart');
const Canvas = require('canvas');
const { convertVChartToSvg } = require('@visactor/vchart-svg-plugin');
const spec = {
type: 'pie',
data: [
{
id: 'id0',
values: [
{ type: 'oxygen', value: '46.60' },
{ type: 'silicon', value: '27.72' },
{ type: 'aluminum', value: '8.13' },
{ type: 'iron', value: '5' },
{ type: 'calcium', value: '3.63' },
{ type: 'sodium', value: '2.83' },
{ type: 'potassium', value: '2.59' },
{ type: 'others', value: '3.5' }
]
}
],
outerRadius: 0.8,
valueField: 'value',
categoryField: 'type',
title: {
visible: true,
text: 'Statistics of Surface Element Content'
},
legends: {
visible: true,
orient: 'left'
},
label: {
visible: true
},
tooltip: {
mark: {
content: [
{
key: datum => datum['type'],
value: datum => datum['value'] + '%'
}
]
}
}
};
const vchart = new VChart(spec, {
// 声明使用的渲染环境以及传染对应的渲染环境参数
mode: 'node',
modeParams: Canvas,
animation: false
});
vchart.renderSync();
const svgContent = convertVChartToSvg(vchart);