ECharts折线图常见效果


本文记录一些平时常见的折线图的效果图及代码,需要时方便直接套用。本文只列出echarts的配置项代码,不了解echarts的使用方法可查看 官方文档

基础

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

option = {
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'line',
      data: [150, 230, 224, 218, 135, 147, 260]
    }
  ]
}

平滑

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

option = {
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'line',
      smooth: true,
      data: [150, 230, 224, 218, 135, 147, 260]
    }
  ]
}

最大值、最小值、平均值

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

option = {
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'line',
      data: [150, 230, 224, 218, 135, 147, 260],
      markPoint: {
        data: [
          {
            name: '最大值',
            type: 'max'
          },
          {
            name: '最小值',
            type: 'min'
          }
        ]
      },
      markLine: {
        data: [
          {
            name: '平均值',
            type: 'average'
          }
        ]
      }
    }
  ]
}

图表标域

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

option = {
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  visualMap: {
    show: false,
    dimension: 0,
    pieces: [
      {
        lte: 1,
        color: 'green'
      },
      {
        gt: 1,
        lte: 2,
        color: 'red'
      },
      {
        gt: 2,
        lte: 4,
        color: 'green'
      },
      {
        gt: 4,
        lte: 5,
        color: 'red'
      },
      {
        gt: 5,
        color: 'green'
      }
    ]
  },
  series: [
    {
      type: 'line',
      data: [150, 230, 224, 218, 135, 147, 260],
      markArea: {
        itemStyle: {
          color: 'rgba(255, 173, 177, 0.4)'
        },
        data: [
          [
            {
              xAxis: 'Tue'
            },
            {
              xAxis: 'Wed'
            }
          ],
          [
            {
              xAxis: 'Fri'
            },
            {
              xAxis: 'Sat'
            }
          ]
        ]
      }
    }
  ]
}

区域填充

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

option = {
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'line',
      smooth: true,
      data: [150, 230, 224, 218, 135, 147, 260],
      areaStyle: {
        color: 'skyblue'
      }
    }
  ]
}

坐标轴留白策略:紧挨边缘

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

配置项:

option = {
  xAxis: {
    type: 'category',
    // 类目轴中 boundaryGap 可以配置为 true 和 false。默认为 true,设置false可紧挨边缘。
    boundaryGap: false,
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value',
    axisLine: {
      show: true
    }
  },
  series: [
    {
      type: 'line',
      data: [150, 230, 224, 218, 135, 147, 260]
    }
  ]
}

缩放:脱离 0 值比例

配置前效果图:
在这里插入图片描述
配置后效果图:
在这里插入图片描述
配置项:

option = {
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value',
    // 是否是脱离 0 值比例。设置成 true 后坐标刻度不会强制包含零刻度
    // 只在数值轴中(type: 'value')有效。
    scale: true,
    axisLine: {
      show: true
    }
  },
  series: [
    {
      type: 'line',
      // data: [150, 230, 224, 218, 135, 147, 260]
      data: [250, 254, 244, 248, 255, 253, 250]
    }
  ]
}

堆叠

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

高亮聚焦淡出效果图:
在这里插入图片描述
配置项:

option = {
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      type: 'line',
      stack: 'sale',
      data: [150, 230, 224, 218, 135, 147, 260],
      areaStyle: {},
      emphasis: {
        focus: 'self'
      }
    },
    {
      type: 'line',
      stack: 'sale',
      data: [120, 130, 204, 118, 175, 177, 160],
      areaStyle: {},
      emphasis: {
        focus: 'self'
      }
    }
  ]
}

同期比对、多x轴

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

const colors = ['#5470C6', '#EE6666'];
const option = {
  color: colors,
  tooltip: {
    trigger: 'none',
    axisPointer: {
      type: 'cross'
    }
  },
  legend: {},
  grid: {
    top: 70,
    bottom: 50
  },
  xAxis: [
    {
      type: 'category',
      axisTick: {
        alignWithLabel: true
      },
      axisLine: {
        onZero: false,
        lineStyle: {
          color: colors[1]
        }
      },
      axisPointer: {
        label: {
          formatter: function (params) {
            return (
              params.value +
              (params.seriesData.length ? ':' + params.seriesData[0].data : '')
            );
          }
        }
      },
      data: ['2022-1', '2022-2', '2022-3', '2022-4', '2022-5', '2022-6', '2022-7', '2022-8', '2022-9', '2022-10', '2022-11', '2022-12']
    },
    {
      type: 'category',
      axisTick: {
        alignWithLabel: true
      },
      axisLine: {
        onZero: false,
        lineStyle: {
          color: colors[0]
        }
      },
      axisPointer: {
        label: {
          formatter: function (params) {
            return (
              params.value +
              (params.seriesData.length ? ':' + params.seriesData[0].data : '')
            );
          }
        }
      },
      data: ['2021-1', '2021-2', '2021-3', '2021-4', '2021-5', '2021-6', '2021-7', '2021-8', '2021-9', '2021-10', '2021-11', '2021-12']
    }
  ],
  yAxis: [
    {
      type: 'value'
    }
  ],
  series: [
    {
      name: '2021',
      type: 'line',
      xAxisIndex: 1,
      smooth: true,
      emphasis: {
        focus: 'series'
      },
      data: [822, 788, 926, 959, 790, 864, 887, 707, 1056, 487, 610, 723]
    },
    {
      name: '2022',
      type: 'line',
      xAxisIndex: 0,
      smooth: true,
      emphasis: {
        focus: 'series'
      },
      data: [939, 859, 783, 711, 887, 692, 1016, 1084, 1266, 1154, 1103, 970]
    }
  ]
}

渐变面积图、拐点描边样式、悬停显示拐点

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

const option = {
  legend: {
    textStyle: {
      color: '#fff'
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    axisLabel: {
      color: 'rgba(255,255,255,0.8)'
    },
    axisTick: {
      alignWithLabel: true,
      lineStyle: {
        color: 'rgba(255,255,255,0.8)'
      }
    },
    data: ['2022-1', '2022-2', '2022-3', '2022-4', '2022-5', '2022-6', '2022-7', '2022-8', '2022-9', '2022-10', '2022-11', '2022-12']
  },
  yAxis: {
    type: 'value',
    axisLabel: {
      color: 'rgba(255,255,255,0.8)'
    },
    splitLine: {
      lineStyle: {
        color: '#666'
      }
    }
  },
  tooltip: {
    trigger: 'axis'
  },
  series: [
    {
      name: 'A',
      type: 'line',
      symbol: 'circle',
      symbolSize: 12,
      // 是否显示 symbol, 如果 false 则只有在 tooltip hover 的时候显示
      showSymbol: false,
      itemStyle: {
        color: '#0184d5',
        borderColor: 'rgba(221, 220, 107, 0.1)',
        borderWidth: 12
      },
      smooth: true,
      emphasis: {
        focus: 'series'
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: 'rgba(1, 132, 312, 0.4)'
          },
          {
            offset: 0.8,
            color: 'rgba(1, 132, 312, 0.1)'
          }
        ])
      },
      data: [722, 988, 826, 959, 1090, 1264, 987, 907, 856, 1187, 1010, 923]
    },
    {
      name: 'B',
      type: 'line',
      symbol: 'circle',
      symbolSize: 12,
      showSymbol: false,
      itemStyle: {
        color: '#00d887',
        borderColor: 'rgba(221, 220, 107, 0.1)',
        borderWidth: 12
      },
      smooth: true,
      emphasis: {
        focus: 'series'
      },
      lineStyle: {
        color: '#00d887',
        width: 2
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: 'rgba(0, 216, 135, 0.4)'
          },
          {
            offset: 0.8,
            color: 'rgba(0, 216, 135, 0.1)'
          }
        ])
      },
      data: [239, 359, 283, 411, 587, 392, 516, 484, 666, 504, 703, 570]
    }
  ]
}

不定期更新…

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts是一个基于JavaScript的开源可视化库,用于构建交互式的图表和数据可视化界面。在ECharts中,折线图是一种常见的图表类型,而tooltip则是用于显示数据点的详细信息的工具提示框。 要实现ECharts折线图tooltip的轮播效果,可以通过设置tooltip的属性来实现。具体步骤如下: 1. 在ECharts的配置项中,找到tooltip属性,并设置trigger为'axis',表示触发方式为坐标轴触发。 2. 设置tooltip的formatter属性为一个函数,用于自定义tooltip的内容。 3. 在formatter函数中,可以通过params参数获取到当前tooltip所需要显示的数据。根据需求,可以将数据进行处理后返回给tooltip。 4. 在formatter函数中,可以使用setInterval函数来实现tooltip内容的轮播效果。通过定时器不断更新tooltip的显示内容,从而实现轮播效果。 下面是一个示例代码,演示了如何实现ECharts折线图tooltip的轮播效果: ```javascript option = { // 其他配置项... tooltip: { trigger: 'axis', formatter: function(params) { var index = 0; setInterval(function() { index = (index + 1) % params.length; // 根据需求处理数据后返回tooltip内容 var content = params[index].name + ': ' + params[index].value; // 更新tooltip内容 echarts.getInstanceByDom(document.getElementById('chart')).getOption().tooltip[0].formatter = content; echarts.getInstanceByDom(document.getElementById('chart')).hideTip(); echarts.getInstanceByDom(document.getElementById('chart')).showTip({seriesIndex: 0, dataIndex: index}); }, 2000); return params[0].name + ': ' + params[0].value; } }, // 其他配置项... }; ``` 请注意,上述代码中的`chart`是折线图所在的DOM元素的id,需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值