[vue]echarts的使用问题

可以直接按照官网的方法使用

官网的例子是个 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三知之灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值