【Echarts系列】平滑折线面积图

【Echarts系列】平滑折线面积图

为了节省后续开发学习成本,这个系列将记录我工作所用到的一些echarts图表。

示例

平滑折线面积图如图所示:
在这里插入图片描述

数据格式

data = [
    {
      'name': '2020年',
      'value': 150
    },
    {
      'name': '2021年',
      'value': 168
    },
    {
      'name': '2022年',
      'value': 159
    },
    {
      'name': '2023年',
      'value': 127
    },
    {
      'name': '2024年',
      'value': 99
    }
  ]

代码

Vue版本以及脚本语言的选择各有不同,核心内容主要是option,重点关注该部分内容即可。

<template>
  <div class="chart" ref="areaLineRef"></div>
</template>

<script lang="ts">
import { Component, Prop, Ref, Vue, Watch } from 'vue-property-decorator'
import echarts from 'echarts'

@Component({
  name: 'AreaLine',
  components: {}
})
export default class AreaLine extends Vue {
  @Prop() data!: any
  @Ref() areaLineRef!: any
  private chart: any = {}

  @Watch('data')
  onDataChange() {
    this.createChart()
  }

  createChart() {
    this.chart = echarts.init(this.areaLineRef)

    const data = this.data
    let names = []
    let values = []
    data.forEach(item => {
      names.push(item.name)
      values.push(item.value)
    })

    const option = {
      grid: {
        left: -35,
        right: 0,
        top: 0,
        bottom: 0,
        containLabel: true
      },
      tooltip: {
        trigger: 'axis',
        label: {
          show: true
        }
      },
      xAxis: {
        axisLine: {
          show: true     //是否显示X轴轴线
        },
        splitLine: {
          show: false	 //是否显示X轴方向上的分隔线
        },
        axisLabel: {     //设置X轴的坐标标签样式
          textStyle: {
            color: '#757790'
          }
        },
        axisTick: {
          show: true,
          alignWithLabel: true, //坐标刻度与标签对齐
          lineStyle: {          //设置X轴刻度样式
            width: 5,
            color: '#757790'
          }
        },
        data: names
      },
      yAxis: {
        axisLabel: {          //不展示坐标标签为0的
          formatter: function(value) {
            if (Number(value) === 0) {
              return ''
            } else {
              return value
            }
          },
          textStyle: {
            padding: [0, 0, -25, 8],   //通过padding设置来实现坐标轴标签在轴线内
            color: '#757790',
            align: 'left'
          }
        },
        axisLine: {
          show: false				   //不展示Y轴轴线
        },
        splitLine: {
          show: true,
          lineStyle: {                 //以虚线形式来展示Y轴刻度方向上的分隔线
            type: 'dashed',
            color: '#E5E5E5'
          }
        },
        axisTick: {
          show: false                  //不显示Y轴刻度
        }
      },
      series: [{
        showSymbol: false,
        smooth: true,			 //开启平滑处理
        type: 'line',
        lineStyle: {
          color: '#4885C9',
          width: 4
        },
        itemStyle: {			 //设置折线样式
          color: '#4885C9',
          borderWidth: 1,
          borderColor: '#000'
        },
        areaStyle: {			 //设置面积的线性渐变颜色
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 0,
              color: 'rgba(72, 133, 201, 0.4)'
            }, {
              offset: 1,
              color: 'rgba(72, 133, 201, 0)'
            }], false)
          }
        },
        data: values
      }]
    }
    this.chart.setOption(option)
  }

  mounted() {
    this.createChart()
    window.addEventListener('resize', this.chartResize)
  }

  beforeDestroy() {
    if (this.chart) {
      window.removeEventListener('resize', this.chartResize)
      this.chart.dispose()
    }
  }

  chartResize() {
    if (this.chart) {
      this.chart.resize()
    }
  }
}
</script>
<style lang="scss" scoped>
.chart {
  width: 100%;
  height: 300px;
}
</style>


相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-12 15:52:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-12 15:52:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-12 15:52:03       82 阅读
  4. Python语言-面向对象

    2024-06-12 15:52:03       91 阅读

热门阅读

  1. 爬山算法(Hill Climbing Algorithm)详细介绍

    2024-06-12 15:52:03       34 阅读
  2. 「前端+鸿蒙」鸿蒙应用开发-TS接口-特殊用途

    2024-06-12 15:52:03       32 阅读
  3. 右键菜单注册表位置

    2024-06-12 15:52:03       26 阅读
  4. 图论第8天

    2024-06-12 15:52:03       25 阅读
  5. 讲解机器学习中的 K-均值聚类算法及其优缺点。

    2024-06-12 15:52:03       25 阅读
  6. 10、前后端本地端联调

    2024-06-12 15:52:03       27 阅读
  7. 处理element ui 表格中 按钮 loading问题

    2024-06-12 15:52:03       25 阅读
  8. 调料食品加工污水处理设备配置

    2024-06-12 15:52:03       29 阅读
  9. Spring-core-MethodParameter

    2024-06-12 15:52:03       25 阅读
  10. 手机照片怎么恢复?10个照片恢复应用程序

    2024-06-12 15:52:03       38 阅读