Echarts 渐变色使用

9 min read
{
    type: 'bar',
    itemStyle: {
        normal: {
            color: new echarts.graphic.LinearGradient(
                0, 0, 0, 1,
                [
                    {offset: 0, color: '#000'},
                    {offset: 0.5, color: '#888'},
                    {offset: 1, color: '#ddd'}
                ]
            )
        }
    }
}

此配置是一个常见的柱状图配置代码, 柱子的color配置使用了echarts.graphic.LinearGradient来声明渐变色. 可以看到, 使用时传入了5个参数.

其中,

前4个参数用于配置渐变色的起止位置, 这4个参数依次对应右/下/左/上四个方位

而0 0 0 1则代表渐变色从正上方开始

第5个参数则是一个数组, 用于配置颜色的渐变过程

每一项为一个对象, 包含offset和color两个参数. offset的范围是0 ~ 1, 用于表示位置

series: [
    {
      type: 'line',
      stack: '总量',
      symbol: 'circle',
      smooth: true,
      symbolSize: 8,
      itemStyle: {
        normal: {
          color: '#0092f6',
          lineStyle: {
            color: '#68A9FE',
            width: 3
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: '#ABC6EE'
              },
              {
                offset: 1,
                color: 'rgba(255,255,255,0)'
              }
              
            ])
          }
        }
      },
      markPoint: {
        itemStyle: {
          normal: {
            color: 'red'
          }
        }
      },
      data: [120, 132, 101, 134, 90, 230, 210, 182, 191, 234, 290, 330, 41]
    }
  ]

image-20220427173306506