【已解决】在vue中引入echart的折线图时,echarts.graphic.LinearGradient报错,不能正常显示。

在vue中需要用到折线图,且有区域渐变色的效果,那么像下面那样子直接复制过来,在vue中不能渲染出来。
需要将原来的 new echarts.graphic.LinearGradient 改成这样,new this.$echarts.graphic.LinearGradient

报错

在这里插入图片描述

错误代码

在这里插入图片描述

 //自定义环形图颜色
                color: [
                    new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: "#4494F5" },
                        { offset: 1, color: "#08DDF2 " }
                    ]),
                    new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 1, color: "#08DDF280" },
                        { offset: 0, color: "#3D9CF580" },
                    ]),

                ],

修改后

  color: [
                    new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: "#4494F5" },
                        { offset: 1, color: "#08DDF2 " }
                    ]),
                    new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 1, color: "#08DDF280" },
                        { offset: 0, color: "#3D9CF580" },
                    ]),

                ], //自定义环形图颜色

正确展示

在这里插入图片描述

完整步骤

前提先是npm安装echart

1、使用npm安装

npm install echarts --save

2、main.js中引入挂载原型

 
import echarts from 'echarts'
//需要挂载到Vue原型上
Vue.prototype.$echarts = echarts

3、记得要设置宽高-

 <div :class="className" :style="{ height: height, width: width }" ref="Echart" />
   initChart() {
            // 创建 echarts 实例。
            var myChartOne = this.$echarts.init(this.$refs.Echart);
            myChartOne.setOption({
                tooltip: {
                    trigger: "axis",
                    axisPointer: {
                        type: "shadow",
                    },
                },
                tooltip: {
                    trigger: "item",
                },
                //自定义环形图颜色
                color: [
                    new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: "#4494F5" },
                        { offset: 1, color: "#08DDF2 " }
                    ]),
                    new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 1, color: "#08DDF280" },
                        { offset: 0, color: "#3D9CF580" },
                    ]),
                ],
                series: [
                    {
                        type: 'pie',
                        radius: ['25%', '40%'],
                        center: ['50%', '70%'], 
                        startAngle: 180,
                        label: {
                            show: false,
                        },
                        data: [
                            { value: 1048, name: 'Search Engine' },
                            { value: 735, name: 'Direct' },
                            {
                                value: 1048 + 735,
                                itemStyle: {
                                    color: 'none',
                                    decal: {
                                        symbol: 'none'
                                    }
                                },
                                label: {
                                    show: false
                                }
                            }
                        ]
                    }
                ]
            });
        },
mounted () {
  this.initChart()
},

效果图

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue使用ECharts,需要先安装ECharts并在组件引入。在引入ECharts后,可以通过this.$echarts来访问ECharts的API。要使用new echarts.graphic.LinearGradient,需要在组件引入graphic模块。可以在组件这样写: ``` import echarts from 'echarts/lib/echarts' import 'echarts/lib/component/tooltip' import 'echarts/lib/component/title' import 'echarts/lib/component/legend' import 'echarts/lib/chart/line' import 'echarts/lib/chart/bar' import 'echarts/lib/chart/pie' import 'echarts/lib/chart/scatter' import 'echarts/lib/chart/radar' import 'echarts/lib/chart/map' import 'echarts/lib/chart/treemap' import 'echarts/lib/chart/graph' import 'echarts/lib/chart/gauge' import 'echarts/lib/chart/funnel' import 'echarts/lib/chart/parallel' import 'echarts/lib/chart/sankey' import 'echarts/lib/chart/boxplot' import 'echarts/lib/chart/candlestick' import 'echarts/lib/chart/effectScatter' import 'echarts/lib/chart/lines' import 'echarts/lib/chart/heatmap' import 'echarts/lib/chart/pictorialBar' import 'echarts/lib/chart/themeRiver' import 'echarts/lib/chart/sunburst' import 'echarts/lib/chart/custom' import 'echarts/lib/component/graphic' export default { data() { return { chartData: {} } }, mounted() { this.initChart() }, methods: { initChart() { const chartDom = this.$refs.chart const myChart = echarts.init(chartDom) // 在这里使用new echarts.graphic.LinearGradient const gradient = new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#00aaff' }, { offset: 0.7, color: '#998866' }, { offset: 1, color: '#998866' }]) // 在这里使用gradient myChart.setOption({ backgroundColor: gradient, ... }) this.chartData = myChart } } } ``` 这样就可以在Vue组件使用new echarts.graphic.LinearGradient了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值