在vue中引入echart的折线图时,echarts.graphic.LinearGradient,不能正常显示的解决方法。

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

前提先是npm安装echart

1、使用npm安装

npm install echarts --save

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


import echarts from 'echarts'
//需要挂载到Vue原型上
Vue.prototype.$echarts = echarts
<-- 3、记得要设置宽高-->

<div id="bottomRight" :style="{width: '580px', height: '500px'}">折线示意图</div>
methods: {
    // 初始化echarts
    ageInit () {
      // 因为初始化echarts 的时候,需要指定的容器 id='bottomRight'
      // 基于刚刚准备好的 DOM 容器,初始化 EChart 实例
      const myChart1 = this.$echarts.init(document.getElementById('bottomRight'))
      const color = ['#6080EB', 'rgba(96, 128, 235, 0.42)', 'rgba(96, 128, 235, 0.03)']
      const xData = ['2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04', '2021-10-05', '2021-10-06', '2021-10-07']
      const yData = [1220, 182, 491, 234, 790, 330, 310]
      // 绘制图表
      myChart1.setOption({
        color: color[0],
        dataZoom: {
          type: 'inside',
          xAxisIndex: [0]
        },
        tooltip: {
          show: true,
          trigger: 'axis'
        },
        grid: {
          top: 10,
          bottom: 40
          // left: 70,
          // right: 20,
        },
        xAxis: {
          boundaryGap: false,
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#d8d8d8'
            }
          },
          axisLabel: {
            color: 'rgba(0, 0, 0, 0.65)'
          },
          data: xData
        },
        yAxis: {
          splitNumber: 4,
          splitLine: {
            show: true
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#d8d8d8'
            }
          },
          axisLabel: {
            color: 'rgba(0, 0, 0, 0.65)'
          }
        },
        series: [
          {
            type: 'line',
            showSymbol: false,
            smooth: true,
            lineStyle: {
              color: color[0],
              width: 3
            },
            areaStyle: {
              // 区域填充样式
              normal: {
                color: new this.$echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: color[1]
                    },
                    {
                      offset: 1,
                      color: color[2]
                    }
                  ],
                  false
                )
              }
            },
            data: yData
          }
        ]
      })
    }
mounted () {
  this.ageInit()
},

效果图

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值