echarts中同一段代码,按需引入内容一样,高版本(5.1.2)的会报错?

文章描述了一名开发者在将Echarts从4.8.0升级到5.1.2时遇到的问题。升级后,代码报错,提示找不到grid和legend模块。解决方法是按需引入这两个组件。作者建议,在无明确需求时避免对已有项目的版本升级,以防止出现未预期的bug和额外的调试工作。
摘要由CSDN通过智能技术生成

echarts中同一段代码,按需引入内容一样,高版本(5.1.2)的会报错?

一、场景

2020年写一个app,首页有个echarts折线图,当时package.json版本是4.8.0,我接手继续写,拉取项目后安装依赖,echarts老是提示安装不是,我当时就npm install echart --save,就安装一个高版本的,然后就报错了,提示echarts两个模块没有找到grid和legend
在这里插入图片描述

在这里插入图片描述

二、项目代码

echarts图表中的确也用了grid和legend,低版本4.8.0没引入也不会报错,高版本5.1.2如果没有引入就会报错,最后我把echarts给回退到4.8.0版本,不引人这两行也不会报错了,气死了

var echarts = require('echarts/lib/echarts')
require('echarts/lib/chart/pie')
require('echarts/lib/chart/line')
require('echarts/lib/component/tooltip')
//下面两行是高版本需要加的
require('echarts/lib/component/grid')
require('echarts/lib/component/legend')
let lineOption = {
    tooltip: {
        trigger: 'axis',
        backgroundColor: '#fff',
        borderColor: '#eaeaea',
        borderWidth: 1,
        textStyle: {
            fontSize: 12,
            color: '#616161',
            borderColor: '#e7e7e7',
            borderWidth: 1,
            textBorderWidth: 2,
            textBorderColor: '#e7e7e7',
        },
        axisPointer: {
            type: 'line',
            lineStyle: {
                color: '#409EFF',
            },
        },
    },
    grid: {
        //上下左右位移设置
        top: '15',
        left: '50',
        right: '30',
    },
    legend: {
        //orient: 'vertical',
        bottom: '40',
        left: 'center',
        itemHeight: 5,
        //icon: 'rect',
        //align: 'bottom',

        textStyle: {
            color: '#616161',
            fontSize: 14,
        },
    },
    xAxis: {
        boundaryGap: false,
        // data: [],
        axisLine: {
            lineStyle: {
                color: '#eaeaea',
            },
        },
        axisTick: {
            show: false,
        },
        axisLabel: {
            color: '#616161',
        },
        splitLine: {
            show: false,
        },
    },
    yAxis: {
        axisLine: {
            //y轴线
            show: false,
        },
        axisTick: {
            //y轴刻度
            show: false,
        },
        axisLabel: {
            //y轴文字颜色
            color: '#616161',
        },
        splitLine: {
            //y轴分割线(横向)
            lineStyle: {
                color: '#eaeaea',
                type: 'dotted',
            },
        },
        type: 'value',
    },
    series: [
        {
            type: 'line',
            // data: [],
            itemStyle: {
                //
                color: '#409EFF',
            },
            lineStyle: {
                //折线图样式
                width: 1,
            },
            showSymbol: false, //是否显示拐点
            areaStyle: {
                //折线图区域设置
                normal: {
                    //颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
                    color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                        {
                            offset: 0,
                            color: '#FFFFFF',
                        },
                        {
                            offset: 1,
                            color: '#409EFF',
                        },
                    ]),
                },
            },
        },
    ],
}

三、总结

安装依赖时候,如果老是安装不上就把node_modules删了重新npm i多试试
如果没有需求,千万不要给已经写过不少内容的项目升级版本,容易出bug
报应啊,因为这个我多加班一个小时,第二天又研究了大半天…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Vue3和Echarts 5.1.2实现鼠标悬停扇形部分饼图radius半径变大的示例代码: ```vue <template> <div ref="chart" style="height: 400px"></div> </template> <script> import { defineComponent, ref, onMounted } from 'vue' import * as echarts from 'echarts' import 'echarts-gl' export default defineComponent({ setup() { const chartRef = ref(null) let chart const initChart = () => { chart = echarts.init(chartRef.value) const option = { tooltip: { trigger: 'item', formatter: '{b}: {c} ({d}%)' }, series: [ { type: 'pie', radius: ['40%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' } ] } ] } chart.setOption(option) chart.on('mouseover', { seriesIndex: 0 }, function(params) { const angle = params.angle const startAngle = params.startAngle const endAngle = params.endAngle const radius = params.radius[1] const center = chart.convertToPixel('pie', params.center) const x = center[0] const y = center[1] const shape = { type: 'sector', shape: { cx: x, cy: y, r: radius, startAngle: startAngle, endAngle: endAngle }, style: { fill: '#fff', opacity: 0.5 }, zlevel: 100 } chart.getZr().add(shape) chart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: params.dataIndex }) chart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: params.dataIndex === 0 ? 4 : params.dataIndex - 1 }) chart.dispatchAction({ type: 'pieRadius', seriesIndex: 0, radius: [radius, radius + 10] }) }) chart.on('mouseout', { seriesIndex: 0 }, function(params) { chart.getZr().removeAll() chart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: -1 }) chart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: -1 }) chart.dispatchAction({ type: 'pieRadius', seriesIndex: 0, radius: ['40%', '70%'] }) }) } onMounted(() => { initChart() }) return { chartRef } } }) </script> ``` 在这个示例中,我们使用了Echarts 5.1.2创建一个基本的饼图,并添加了鼠标悬停事件的监听。当鼠标悬停在饼图的扇形部分时,我们绘制一个半透明的扇形部分,亮当前扇形并降低前一个扇形的亮度,并将当前扇形的radius半径增加10个像素。当鼠标移出扇形部分时,我们清除绘制的半透明扇形部分,取消扇形的亮和降低前一个扇形的亮度,并将radius半径还原为原始值。 注意:在使用Echarts 5.1.2的时候,要将饼图的`type`设置为`pie`,否则无法使用`pieRadius`类型的动画。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值