echarts 折线图

封装折线图组件 组件名diagran.vue

<!--柱形图、折线-->
<template>
  <div class="chartBarLine" ref="chartBarLine"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  props: ['option'],
  data () {
    return {
      chartDom: null,
      chartData: this.option
    }
  },
  watch: {
    option: {
      handler (val) {
        this.chartData = val
        this.$nextTick(() => {
          this.chartInit()
        })
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.chartInit()
    })
  },
  methods: {
    chartInit () {
      this.chartDom = echarts.init(this.$refs.chartBarLine)
      // 名称数组
      const names = this.chartData.map(m => { return m.name })

      // 随访数数组
      const data = this.chartData.map(m => { return m.num })

      // 死亡数数组
      const data2 = this.chartData.map(m => { return m.numDeath })

      this.chartDom.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#283b56'
            }
          }
        },
        legend: {
          bottom: 15
        },
        grid: {
          top: '10%',
          left: '0%',
          right: '7%',
          bottom: '20%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#cdd5e2'
            }
          },
          axisLabel: {
            textStyle: {
              color: '#666666'
            },
            interval: 0
          },
          data: names
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: true,
            lineStyle: {
              color: '#cdd5e2'
            }
          },
          splitLine: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: '#666666'
            }
          }
        },
        series: [
          {
            name: '学不会1',
            type: 'bar',
            barWidth: 12,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#1465DC'
                  },
                  {
                    offset: 1,
                    color: '#1465DC'
                  }
                ]),
                barBorderRadius: 6
              }
            },
            data: data
          },
          {
            name: '学不会2',
            type: 'line',
            lineWidth: 12,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: '#67D652'
                  },
                  {
                    offset: 1,
                    color: '#67D652'
                  }
                ]),
                barBorderRadius: 6
              }
            },
            data: data2
          }
        ]
      })
      window.addEventListener('resize', () => {
        this.chartDom.resize()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.chartBarLine {
  width: 100%;
  height: 100%;
}
</style>

页面引用组件

<template>
  <div>
    <div class="left-charts">
      <Diagran :option='option' />
    </div>
  </div>
</template>

<script>
import Diagran from './diagran.vue'
export default {
  data () {
    return {
      option: [
        {
          "numDeath": 5065,
          "num": 0,
          "name": "测试1"
        },
        {
          "numDeath": 4503,
          "num": 0,
          "name": "测试2"
        },
        {
          "numDeath": 637,
          "num": 0,
          "name": "测试3"
        },
        {
          "numDeath": 460,
          "num": 0,
          "name": "测试4"
        },
        {
          "numDeath": 840,
          "num": 0,
          "name": "测试5"
        },
        {
          "numDeath": 1968,
          "num": 0,
          "name": "测试6"
        },
        {
          "numDeath": 1431,
          "num": 0,
          "name": "测试7"
        }
      ]
    }
  },
  components: {
    Diagran
  }
}
</script>

<style lang="less" scoped>
.left-charts {
  width: 600px;
  height: 500px;
  margin-left: 100px;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学不会•

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

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

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

打赏作者

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

抵扣说明:

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

余额充值