Echarts 折线图 渐变色 不堆叠

11 篇文章 1 订阅
本文详细介绍了如何在echarts折线图中实现hover提示、多线图不堆叠、坐标轴调整、线宽控制、拐点自定义、图例图标更改、渐变色应用等高级定制。包括设置tooltip提示内容、去除hover线条变粗、以及修改系列样式和图例布局。
摘要由CSDN通过智能技术生成

好记性不如烂笔头,记录下方便后续使用~~~

效果图:
在这里插入图片描述

注意点:
(1)hover 时,背景阴影、自定义提示内容

tooltip: {
  trigger: 'axis',
  axisPointer: {
    type: 'shadow',
  },
  formatter: (params) => {
    console.log('params', params)

    // // 获取xAxis data中的数据
    let dataStr = `<div>
                    <p style="font-size:14px;font-weight:bold;color:#333;margin:0 0px 10px;">
                      ${params[0].name}XX总数 ? 个</p>
                </div>`
    params.forEach((item) => {
      // 获取图标(小圆点)颜色,并且自定义样式为正方形,即要改变小圆点,只需重写marker的html片段,并修改样式即可
      // 获取series中data
      // 获取seriersName
      console.log('item', item)
      dataStr += `<div>
        <div style="font-size:14px; color:#666; margin-top:10px;">
          <span style="display:inline-block;margin-right:0px;width:8px;height:8px;border-radius:4px;background-color:${item.color};"></span>
          <span style="margin-right:5px;">${item.seriesName}</span>
          <span>${item.data}个</span>
        </div>
      </div>`
    })
    return dataStr
  },
},

(2)一张图中多条折线,不堆叠:
series下的【stack: ‘总量’ 】要去掉,否则y轴数值就会相加,与正确数值不符,呈堆叠效果
(3)坐标轴的 name 属性更改位置:使用 padding
在这里插入图片描述

nameTextStyle: {
  padding: [0, 0, 0, -50], // 四个数字分别为上右下左与原位置距离
},

(3)去掉 hover 折线变粗效果(鼠标悬停在折线图上,折线变粗)
方法:设置emphasis.lineStyle和折线图本身lineStyle相同
(4)鼠标悬停在拐点上时,自定义拐点样式:
nomal 正常情况下,emphasis 鼠标hover状态时
在这里插入图片描述

itemStyle: {
  normal: {
    color: '#5393E7', //改变折线点的颜色
    lineStyle: {
      width: 2, //折线宽度
      color: '#5393E7', //改变折线颜色
    },
  },
  emphasis: {
    color: '#5393E7',
    lineStyle: {
      width: 2,
      color: '#5393E7',
    },
    borderColor: '#5393E7', //拐点边框颜色
    borderWidth: 6, //拐点边框大小
  },
},

(5)修改图示图标 为圆角矩形 icon: 'roundRect',
在这里插入图片描述
icon的配置及所代表图形:

icon显示图形
circle圆形
rect矩形
roundRect圆角矩形
triangle三角形
diamond菱形
pin水滴
arrow箭头
none不显示图标
legend: {
  itemHeight: 4,
  itemWidth: 12,
  data: [
    {
      name: '巡检工单',
      icon: 'roundRect',
    },
    {
      name: '故障工单',
      icon: 'roundRect',
    },
  ],
  left: '200', // 距离左边
},

(6)更改 legend 图标和文字的距离:legend的图标和文字居中对齐
调整文字的距离位置padding,调整legend的位置 topleft
参考1:https://segmentfault.com/a/1190000013836680
参考2:https://blog.csdn.net/qq_34672907/article/details/83065909?spm=1001.2014.3001.5501

let lendData = [
  {
    name: '巡检工单',
    icon: 'roundRect',
    value: 40,
  },
  {
    name: '故障工单',
    icon: 'roundRect',
    value: 30,
  },
]
// 修改图示 图标 - 圆角矩形
legend: {
  itemHeight: 4,
  itemWidth: 12,
  data: lendData,
  top: -5, //legend距离上方
  left: '200', // 距离左边
  formatter: function (name) {
    var arr = ['{b|' + name + '}']
    return arr.join('\n')
  },
  textStyle: {
    rich: {
      b: {
        fontSize: 14,
        align: 'center',
        padding: [2, 0, 0, 0], //位置
        color: '#666',
        // lineHeight: 25,
      },
    },
  },
},

(7)改变折线区域的线条颜色和区域渐变色
线条颜色 设置itemStyle
区域渐变色 设置areaStyle

颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上

注意:渐变色最好使用 rgba

series: [
  {
    name: '折线图1',
    type: 'line',
    //如果不想折线那么尖锐,可以加上smooth: true变为柔和曲线
    //smooth: true,
    //stack: '总量',
    symbolSize: 7, // 拐点大小
    areaStyle: {
      color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
        {
          offset: 0,
          color: 'rgba(129,183,255, 1)',
        },
        {
          offset: 1,
          color: 'rgba(129,183,255, 0)',
        },
      ]),
    },
    itemStyle: {
      normal: {
        color: '#5393E7', //改变折线点的颜色
        lineStyle: {
          width: 2, //折线宽度
          color: '#5393E7', //改变折线颜色
        },
      },
      emphasis: {
        color: '#5393E7',
        lineStyle: {
          width: 2,
          color: '#5393E7',
        },
        borderColor: '#5393E7', //拐点边框颜色
        borderWidth: 6, //拐点边框大小
      },
    },
    data: [120, 132, 101, 134, 90, 230, 210, 120, 132, 101, 134, 90],
    zlevel: 11,
  },
]

完整实现:

<template>
  <!-- 不堆叠 -->
  <div id="treeChart" :style="{ height: '242px' }"></div>
</template>

<script>
export default {
  name: 'eCharts',
  data() {
    return {
      value: 60,
    }
  },
  mounted() {
    this.showChart()
  },
  methods: {
    showChart() {
      // 基于准备好的dom,初始化echarts实例
      var myChart = this.$echarts.init(document.getElementById('treeChart'))

      var value = 200 //当前进度
      var maxValue = 300 //进度条最大值
      var option = {
        title: {
          text: '2021折线图分析分析分析分析',
          textStyle: {
            fontFamily: 'Microsoft YaHei',
            fontSize: 14,
            fontWeight: 'normal',
            color: '#666666',
          },
          left: '-5',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          formatter: (params) => {
            console.log('params', params)

            // // 获取xAxis data中的数据
            let dataStr = `<div>
                            <p style="font-size:14px;font-weight:bold;color:#333;margin:0 0px 10px;">
                              ${params[0].name}XX总数 ? 个</p>
                        </div>`
            params.forEach((item) => {
              // 获取图标(小圆点)颜色,并且自定义样式为正方形,即要改变小圆点,只需重写marker的html片段,并修改样式即可
              // 获取series中data
              // 获取seriersName
              console.log('item', item)
              dataStr += `<div>
                <div style="font-size:14px; color:#666; margin-top:10px;">
                  <span style="display:inline-block;margin-right:0px;width:8px;height:8px;border-radius:4px;background-color:${item.color};"></span>
                  <span style="margin-right:5px;">${item.seriesName}</span>
                  <span>${item.data}个</span>
                </div>
              </div>`
            })
            return dataStr
          },
        },
        // 修改图示 图标 - 圆角矩形
        legend: {
          itemHeight: 4,
          itemWidth: 12,
          data: [
            {
              name: '折线图1',
              icon: 'roundRect',
            },
            {
              name: '折线图2',
              icon: 'roundRect',
            },
          ],
          left: '200', // 距离左边
        },
        grid: {
          left: '3%',
          right: '2%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: {
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
          axisTick: {
            show: false, //是否显示刻度线 默认为true
            alignWithLabel: true,
          },
          axisLabel: {
            show: true, //这里的show用于设置是否显示x轴下的字体 默认为true
            interval: 0, //可以设置成 0 强制显示所有标签。如果设置为 1,表示『隔一个标签显示一个标签』,如果值为 2,表示隔两个标签显示一个标签,以此类推。
            textStyle: {
              //textStyle里面写x轴下的字体的样式
              color: '#999',
              fontSize: 14,
            },
          },
          axisLine: {
            onZero: true, // 显示0刻度
            lineStyle: {
              //lineStyle里面写x轴那一条线的样式
              color: '#E6E6E6',
              width: 1, //轴线的粗细
            },
          },
        },
        yAxis: {
          name: '工单数',
          nameTextStyle: {
            padding: [0, 0, 0, -50], // 四个数字分别为上右下左与原位置距离
          },
          splitLine: {
            show: true,
            // 背景虚线
            lineStyle: {
              type: [4, 5],
              dashOffset: 5,
            },
          },
          //用于设置y轴的字体
          axisLabel: {
            show: true, //这里的show用于设置是否显示y轴下的字体 默认为true
            textStyle: {
              //textStyle里面写y轴下的字体的样式
              color: '#999',
              fontSize: 12,
            },
          },
        },
        series: [
          {
            name: '折线图1',
            type: 'line',
            //如果不想折线那么尖锐,可以加上smooth: true变为柔和曲线
            //smooth: true,
            //stack: '总量',
            symbolSize: 7, // 拐点大小
            areaStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(129,183,255, 1)',
                },
                {
                  offset: 1,
                  color: 'rgba(129,183,255, 0)',
                },
              ]),
            },
            itemStyle: {
              normal: {
                color: '#5393E7', //改变折线点的颜色
                lineStyle: {
                  width: 2, //折线宽度
                  color: '#5393E7', //改变折线颜色
                },
              },
              emphasis: {
                color: '#5393E7',
                lineStyle: {
                  width: 2,
                  color: '#5393E7',
                },
                borderColor: '#5393E7', //拐点边框颜色
                borderWidth: 6, //拐点边框大小
              },
            },
            data: [120, 132, 101, 134, 90, 230, 210, 120, 132, 101, 134, 90],
            zlevel: 11,
          },
          {
            name: '折线图2',
            type: 'line',
            //smooth: true,
            //如果想要y轴的数值不相加,就要去掉stack参数。否则数值就会相加。
            //stack: '总量',
            symbolSize: 7,
            areaStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(255,224,191, 1)',
                },
                {
                  offset: 1,
                  color: 'rgba(255,224,191, 0)',
                },
              ]),
            },
            itemStyle: {
              normal: {
                color: '#F3912E',
                lineStyle: {
                  width: 2,
                  color: '#F3912E',
                },
              },
              emphasis: {
                color: '#F3912E',
                lineStyle: {
                  width: 2,
                  color: '#F3912E',
                },
                borderColor: '#F3912E',
                borderWidth: 6,
              },
            },
            data: [220, 182, 191, 234, 290, 330, 310, 220, 182, 191, 234, 290],
            zlevel: 11,
          },
        ],
      }

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option)
      //随着浏览器窗口大小改变而改变
      window.addEventListener('resize', function () {
        myChart.resize()
      })
    },
  },
}
</script>
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Windyluna

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值