可以直接按照官网的方法使用
官网的例子是个 PieChart ,而在实践过程中使用的是个 BarChart
发现 PieChart 可以正常展示,但是 BarChart 的渲染存在问题,对比后发现options 比Pie多了一个配置项xAxis/yAxis,猜测是缺少相关的组件引起的
因为官网内容不全面,只能到本地node_modules模块查看所有的echarts/components,并测试,最终发现是 GridComponent 组件
import VChart from "vue-echarts";
import { use } from "echarts/core";
import { CanvasRenderer } from "echarts/renderers";
import { BarChart } from "echarts/charts";
import { TitleComponent, TooltipComponent, LegendComponent, GridComponent, ToolboxComponent } from "echarts/components";
use([
CanvasRenderer,
BarChart,
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
ToolboxComponent
]);
以上方式可以通过全局注册,也可以通过单页面注册
全局注册:
app.component(displayName,VChart from)
单页面注册:
script
export default defineComponent({
name: "name",
components: {
"v-chart": VChart
},
setup() {
let echartOptions = ref({
title: {
text: "GGGGG",
left: "left"
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
}
},
// toolbox: {
// feature: {
// dataView: { show: true, readOnly: false },
// magicType: { show: true, type: ['line', 'bar'] },
// restore: { show: true },
// saveAsImage: { show: true }
// }
// },
legend: {
data: ['次数', '金额']
},
xAxis: [
{
type: 'category',
data: ['请求', '响应', '成功'],
axisPointer: {
type: 'shadow'
}
}
],
yAxis: [
{
type: 'value',
name: '次数',
min: 0,
max: null,
interval: 100000,
axisLabel: {
formatter: '{value}'
}
},
{
type: 'value',
name: '金额',
min: 0,
max: null,
interval: 200000000,
axisLabel: {
formatter: '{value} 元',
rotate: '-45'
}
}
],
series: [
{
name: '次数',
type: 'bar',
// tooltip: {
// valueFormatter: function (value) {
// return value + ' ml';
// }
// },
data: []
},
{
name: '金额',
type: 'bar',
yAxisIndex: 1,
tooltip: {
valueFormatter: function (value) {
return value;
}
},
label: {
show: true,
formatter: '{@金额}',
rotate: 90,
position: ['50%', '50%']
},
data: []
}
]
});
let numbers = [166692, 175771, 37135];
let money = [279741888.3, 155141883.26, 28392392.81];
const getMaxAndInterval = (number) => {
const length = number.toString().length;
if (length >= 3) {
const interval = Math.pow(10, length - 2);
const oneInt = Math.pow(10, length - 1);
return {
max: Math.ceil(number / oneInt) * oneInt,
interval: Math.ceil(number / oneInt) * interval
}
}
return {
max: 100,
interval: 10
}
}
const numberOpt = getMaxAndInterval(Math.max(...numbers));
const moneyOpt = getMaxAndInterval(Math.ceil(Math.max(...money)));
echartOptions.value.series[0].data = numbers;
echartOptions.value.series[1].data = money;
echartOptions.value.yAxis[0].max = numberOpt.max;
echartOptions.value.yAxis[1].max = moneyOpt.max;
echartOptions.value.yAxis[0].interval = numberOpt.interval;
echartOptions.value.yAxis[1].interval = moneyOpt.interval;
return {
echartOptions
}
}
})
template
<v-chart class="echarts" :option="echartOptions" :autoresize="true"></v-chart>