Vue echarts自定义柱状图颜色以及tooltip

 一、自定义color

1、设置type: 'linear',   //线性渐变 

       前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,相当于在图形包围盒中的百分比,如果 globalCoord 为 `true`,则该四个值是绝对的像素位置 

       在这里我只做了y轴渐变,没有做x轴渐变

 完整代码

 itemStyle: {
              normal: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [{
                    offset: 0, color: '#5CADFF' // 0% 处的颜色
                  }, {
                    offset: 1, color: '#0180FF' // 100% 处的颜色
                  }],
                  global: false // 缺省为 false
                }
              }
            },

2、当值超过平均值时柱状图变色 

效果图如下

 color支持使用回调函数,输出params看一下,可以获取到平均值相关数据

完整代码:,再根据数据做个判断

 itemStyle: {
              normal: {
                // 柱形图圆角,初始化效果
                barBorderRadius:[2,2, 0, 0],
                // color:''
                color: function(params){
                 // console.log('颜色e',params)
                 if(params.value>res.data.comMap.average) {
                   return {
                     type: 'linear',
                     x: 0,
                     y: 0,
                     x2: 0,
                     y2: 1,
                     colorStops: [{
                       offset: 0, color: '#FFE989' // 0% 处的颜色
                     }, {
                       offset: 1, color: '#FFCB78' // 100% 处的颜色
                     }],
                     global: false // 缺省为 false
                   }
                 } else{
                   return {
                     type: 'linear',
                     x: 0,
                     y: 0,
                     x2: 0,
                     y2: 1,
                     colorStops: [{
                       offset: 0, color: '#00CEFF' // 0% 处的颜色
                     }, {
                       offset: 1, color: '#6872FF' // 100% 处的颜色
                     }],
                     global: false // 缺省为 false
                   }
}

 二、自定义tooltip 

实现效果如下 :

 官网是这么说的

tooltip. formatter 

提示框浮层内容格式器,支持字符串模板和回调函数两种形式。

1. 字符串模板

模板变量有 {a}{b}{c}{d}{e},分别表示系列名,数据名,数据值等。 在 trigger 为 'axis' 的时候,会有多个系列的数据,此时可以通过 {a0}{a1}{a2} 这种后面加索引的方式表示系列的索引。 不同图表类型下的 {a}{b}{c}{d} 含义不一样。 其中变量{a}{b}{c}{d}在不同图表类型下代表数据含义为:

  • 折线(区域)图、柱状(条形)图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)

  • 散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}(数值数组), {d}(无)

  • 地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)

  • 饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)

更多其它图表模板变量的含义可以见相应的图表的 label.formatter 配置项。

示例:

formatter: '{b0}: {c0}<br />{b1}: {c1}'

2. 回调函数

回调函数格式:

(params: Object|Array, ticket: string, callback: (ticket: string, html: string)) => string | HTMLElement | HTMLElement[]

支持返回 HTML 字符串或者创建的 DOM 实例。

第一个参数 params 是 formatter 需要的数据集。格式如下:

{
    componentType: 'series',
    // 系列类型
    seriesType: string,
    // 系列在传入的 option.series 中的 index
    seriesIndex: number,
    // 系列名称
    seriesName: string,
    // 数据名,类目名
    name: string,
    // 数据在传入的 data 数组中的 index
    dataIndex: number,
    // 传入的原始数据项
    data: Object,
    // 传入的数据值。在多数系列下它和 data 相同。在一些系列下是 data 中的分量(如 map、radar 中)
    value: number|Array|Object,
    // 坐标轴 encode 映射信息,
    // key 为坐标轴(如 'x' 'y' 'radius' 'angle' 等)
    // value 必然为数组,不会为 null/undefied,表示 dimension index 。
    // 其内容如:
    // {
    //     x: [2] // dimension index 为 2 的数据映射到 x 轴
    //     y: [0] // dimension index 为 0 的数据映射到 y 轴
    // }
    encode: Object,
    // 维度名列表
    dimensionNames: Array<String>,
    // 数据的维度 index,如 0 或 1 或 2 ...
    // 仅在雷达图中使用。
    dimensionIndex: number,
    // 数据图形的颜色
    color: string,


    // 饼图的百分比
    percent: number,


}

在这里你可能不明白params 是什么意思,那你可以输出一下

展开看里面有很多信息,只取颜色就行 

完整片段 

 tooltip: {
          trigger: 'axis',
          backgroundColor:'rgba(21, 41, 68, 1)',
          axisPointer: {
            type: 'shadow'        // 'shadow' as default; can also be 'line' or 'shadow'
          },
/*       formatter:'{a0}: {c0}<br />{a1}: {c1}<br />{a2}: {c2}<br />{a3}: {c3}'*/
          formatter:function(params){
            console.log('colorrrrrrrrrrrrrr', params)
            var res = `
           <div>
                <span
                  style="display:inline-block;
                    margin-right:5px;
                    border-radius:10px;
                    width:10px;
                    height:10px;
                    background-color:${params[3].color.colorStops[1].color}"
                ></span><span>${params[3].seriesName}: ${params[3].data}%</span>
              </div>
              <div>
                <span
                  style="display:inline-block;
                    margin-right:5px;
                    border-radius:10px;
                    width:10px;
                    height:10px;
                   background-color:${params[2].color.colorStops[1].color}"
                ></span><span>${params[2].seriesName}: ${params[2].data}%</span>
              </div>
              <div>
                <span
                style="display:inline-block;
                  margin-right:5px;
                  border-radius:10px;
                  width:10px;
                  height:10px;
                  background-color:${params[1].color.colorStops[1].color}"
                ></span><span>${params[1].seriesName}: ${params[1].data}%</span>
              </div>
              <div>
                <span
                  style="display:inline-block;
                    margin-right:5px;
                    border-radius:10px;
                    width:10px;
                    height:10px;
                    background-color:${params[0].color.colorStops[1].color}"
                    ></span><span>${params[0].seriesName}: ${params[0].data}%</span>
              </div>
                `;
              return res;
          }
        },

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值