echart 渐变api

645 阅读2分钟

原先的方法可能报错 在min.JS没有的情况下可以用传统的方法 blog.csdn.net/qq_35858830…

参考 blog.csdn.net/bigbear0000…

`new echarts.graphic.LinearGradient(0, 0, 0, 1, [ {offset: 0, color: '#000'}, {offset: 1, color: '#ddd'} ] )

  该方法传入了5个参数:
  
  其中, 前4个参数用于配置渐变色的起止位置, 这4个参数依次对应:`右/下/左/上`四个方位。例如:

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

    0 1 0 0代表从正下方正上方渐变

    1 0 0 1代表从右上方左下方渐变

第5个参数则是一个数组, 用于配置颜色的渐变过程. 每一项为一个对象, 包含offsetcolor两个参数. offset的范围是0 ~ 1, 用于表示位置, color不用多说肯定是表示颜色了. 像示例代码的配置则表示:

  1. 整个渐变过程是从正上方向正下方变化
  2. 起始(offset: 0)颜色为#000, 变化到正中间(offset: 0.5)位置时颜色为#888, 变化到结束(offset: 1)位置时颜色为#ddd
  • 前4个参数用于配置渐变色的起止位置,分别是:右/下/左/上四
  • 第5个参数则是一个数组,用于配置颜色的渐变过程.,每一项为一个对象, 包含offset和color两个参数. offset的范围是0 ~ 1, 用于表示位置, color不用多说肯定是表示颜色了
    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'}
                ]
            )
        }
    }
}

image.png

series: [
  {
         name:'同比',
         type:'bar',
         data: [-60,-70,75,-70],
         itemStyle: {
             normal:{
                 barBorderRadius: 4,
                     color: new echarts.graphic.LinearGradient(
                     0, 0, 1, 0,
                     [
                         {offset: 0, color: 'rgba(224,130,247,1)'},
                         {offset: 1, color: 'rgba(119,108,254,1)'}
                     ]
                  )
             }
         },
     },
     {
         name:'环比',
         type:'bar',
         data: [60,-50,60,60],
         itemStyle: {
             normal:{
                 barBorderRadius: 4,
                 color: new echarts.graphic.LinearGradient(
                     0, 0, 1, 0,
                     [
                         {offset: 0, color: 'rgba(47,172,255,1)'},
                         {offset: 1, color: 'rgba(27,79,226,1)'}
                     ]
               )
             }
         },
     }
 ]
//  ... 省略无关代码

image.png

转载的那个方法

// 线性渐变,前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,相当于在图形包围盒中的百分比,如果 globalCoord 为 `true`,则该四个值是绝对的像素位置
color: {
    type: 'linear',
    x: 0,
    y: 0,
    x2: 0,
    y2: 1,
    colorStops: [{
        offset: 0, color: 'red' // 0% 处的颜色
    }, {
        offset: 1, color: 'blue' // 100% 处的颜色
    }],
    globalCoord: false // 缺省为 false
}
// 径向渐变,前三个参数分别是圆心 x, y 和半径,取值同线性渐变
color: {
    type: 'radial',
    x: 0.5,
    y: 0.5,
    r: 0.5,
    colorStops: [{
        offset: 0, color: 'red' // 0% 处的颜色
    }, {
        offset: 1, color: 'blue' // 100% 处的颜色
    }],
    globalCoord: false // 缺省为 false
}
// 纹理填充
color: {
    image: imageDom, // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串
    repeat: 'repeat' // 是否平铺, 可以是 'repeat-x', 'repeat-y', 'no-repeat'
}

`