vue渐变堆叠面积图设置渐变色 - echart图表

如图所示:在这里插入图片描述
代码如下:
<template>
  <div class="ml20 mr20 mb20 colorFOp">
    <div class="head tc mb20 ">测试案例</div>
      <div ref="test" class="testChart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  data() {
    return {
      myTestChart:{},
    };
  },
  mounted() {
    this.testChart()
    window.onresize = ()=>{ //窗口变化自动重载
      this.myTestChart.resize()
    }
  
  },
  methods: {
    //添加千分位
    formatCurrency(value){ 
      if (value == 0) return 0;
      if(value == ".") return '';
      if(value == "N/A") return 'N/A';
      if (!value) return '';
      let val = Number(value) // 提前保留两位小数
      let intPart = parseInt(val) // 获取整数部分
      let intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
      let floatPart = '0' // 预定义小数部分
      val = val.toString() // 将number类型转为字符串类型,方便操作
      let value2Array = val.split('.')
      
      if (value2Array.length === 2) { // =2表示数据有小数位
        floatPart = value2Array[1].toString() // 拿到小数部分
        return intPartFormat + '.' + floatPart
      } else {
        return intPartFormat
      }
    },
    testChart(){
      this.myTestChart = echarts.init(this.$refs.test);
      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {           
            type: 'shadow'       
          }
        },
        grid: {
          left: '15',
          right: '15',
          bottom: '5',
          top:'60',
          containLabel: true //防止标签溢出
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap:false,//设置x轴左右起始位置不留白,贴边展示
            splitLine:{ //横线
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                type: 'solid',
                color: '#fff',//左边线的颜色
                width:'1'//坐标线的宽度
              }
            },
            axisLabel: {
              textStyle: {
                color: '#fff',//坐标值得具体的颜色
              }
            },
            data: Array.from({length:4},(v,k)=>`${k+1}`)
          }
        ],
        yAxis: [
          {
            name: 'money',
            nameTextStyle:{
              color:"#65ABE7", 
              fontSize:12,  
              padding:[0,0,0,-30]
            },
            type: 'value',
            // inverse: true,//倒叙
            axisTick:{ //隐藏刻度
              show:false,
            },
            splitLine:{ //竖线
              show: false
            },
            axisLine: {
              show: true,
              lineStyle: {
                type: 'solid',
                color: '#fff',//左边线的颜色
                width:'1'//坐标线的宽度
              }
            },
            axisLabel: {
              textStyle: {
                color: '#fff',//坐标值得具体的颜色
              }
            },
          }
        ],
        color:['#00CBDE','#F00067'],
        series: [{
          name: '柱子',
          type:'line',
          symbol: 'none', //线上原点
          smooth: true, //平滑
          data: [7,17,9,13],
          areaStyle: { //设置渐变色
            /*
              真实环境使用往往是API读取数据,循环series,因此需要定义color数组利用index读取对应颜色
              let color = [
                {
                  a:'73, 213, 248, .5', 
                  b:'73, 213, 248, 0'
                },
                {
                  a:'250, 141, 21, .5' ,
                  b:'250, 141, 21, 0'
                },
                {
                  a:'160, 8, 73, .5' ,
                  b:'160, 8, 73, 0'
                }
              ]
              写法如下:
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {offset: 0, color: `rgba(${color[i].a})`}, 
                {offset: 1, color: `rgba(${color[i].b})`}
              ]),
            */
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {offset: 0, color: 'rgba(73, 213, 248, .5)'}, 
              {offset: 1, color: 'rgba(73, 213, 248, 0)'}
            ]),
          },
          itemStyle:{
            normal:{
              lineStyle:{
                width: 0.8 //线本身宽度
              }
            }
          }
        },{
          name: '折线',
          type:'line',
          symbol: 'none', //线上原点
          smooth: true, //平滑
          data: [2,12,6,2],
          areaStyle: { //设置渐变色
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {offset: 0, color: 'rgba(250, 141, 21, .5)'}, 
              {offset: 1, color: 'rgba(250, 141, 21, 0)'}
            ]),
          },
          itemStyle:{
            normal:{
              lineStyle:{
                width: 0.8 //线本身宽度
              }
            }
          }
        },{
          name: '折线',
          type:'line',
          symbol: 'none', //线上原点
          smooth: true, //平滑
          data: [15,18,14,16],
          areaStyle: { //设置渐变色
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {offset: 0, color: 'rgba(160, 8, 73, .5)'}, 
              {offset: 1, color: 'rgba(160, 8, 73, 0)'}
            ]),
          },
          itemStyle:{
            normal:{
              lineStyle:{
                width: 0.8 //线本身宽度
              }
            }
          }
        }]
      }

      // 使用刚指定的配置项和数据显示图表。
      this.myTestChart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
  .testChart{
    margin:100px auto;
    width:850px;
    height: 50vh;
  }
  .title{
    font-size: 16px;
    color: #00F8FD;
    line-height: 42px;
  }
</style>

番外篇:

还有俩月就当爹了,慌得一笔。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

刘斩仙的笔记本

富贵险中求

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

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

打赏作者

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

抵扣说明:

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

余额充值