如果在 VChart 中使用自定义主题,可以通过两种方式实现,分别是在 spec 中定义 theme,以及通过ThemeManager
注册主题。因为在 Openinula-VChart 中,并不需要引用 VChart 的 npm 包。因此 Openinula-VChart 中透出了 VChart 基类,命名为VChartCore
,方便开发者在 VChart 的基类上通过静态方法注册自定义主题。
VChart 的主题配置请参考VChart 主题。
import React from 'openinula';
import { VChartCore, BarChart, Bar, Axis, Legend } from '@visactor/openinula-vchart';
const theme = {
colorScheme: {
default: [
'#5383F4',
'#7BCF8E',
'#FF9D2C',
'#FFDB26',
'#7568D9',
'#80D8FB',
'#1857A3',
'#CAB0E8',
'#FF8867',
'#B9E493',
'#2CB4A8',
'#B9E4E3'
]
},
series: {
bar: {
barMaxWidth: 15,
label: {
visible: true,
position: 'top',
formatMethod: text => text + '%'
}
}
},
component: {
axis: {
label: {
style: { fontFamily: 'Times New Roman' }
}
}
},
markByName: {
bar: {
style: {
cornerRadius: 15
}
}
}
};
// 注册主题
VChartCore.ThemeManager.registerTheme('userTheme', theme);
// 应用主题
VChartCore.ThemeManager.setCurrentTheme('userTheme');
function MyChart(props) {
const data = [
{ 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' }
];
return (
<BarChart data={[{ id: 'id0', values: data }]}>
<Bar xField="type" yField="value" seriesField="type" />
</BarChart>
);
}
export default MyChart;