lineChart.vue 2.34 KB
<template>
  <div class="chart-box">
    <div class="chart-line" id="line"></div>
  </div>
</template>
<script>
import * as echarts from 'echarts'

export default {
  name: 'LineChart',
  props: ['list'],
  data() {
    return {
      option: {
        tooltip: {
          trigger: 'axis',
          // formatter: `{b}\r \n成交金额:{c}\n \n 订单总数:{d}单`
          formatter: function(params) {
            var tips = ''
            if (params != null && params.length > 0) {
              tips += params[0].name + '<br />'
              for (var i = 0; i < params.length; i++) {
                params[i].marker = '<span style="display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px;background-color:#5850FF;"></span>'
                tips += params[i].marker + '成交金额: ' + params[i].value + '元'
              }
            }
            return tips
            // var res = `<div>${params[0].name}</div>
            // <div>成交金额:${params[0].data}元</div>
            // <div>订单总数:${params[0].value}</div>`
            // return res
          }
        },
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          type: 'value'
        },
        // legend: {
        //   height: 260,
        //   width: 1184
        // },
        grid: {
          x: 80,
          y: 20,
          x2: 30,
          y2: 40
        },
        series: [
          {
            data: [],
            type: 'line',
            itemStyle: {
              normal: {
                lineStyle: {
                  color: '#5850FF' // 改变折线颜色
                }
              }
            }
          }
        ]
      },
      myChart: {}
    }
  },
  watch: {
    list(val) {
      const xData = val.map(i => i.dataDateStr)
      const yData = val.map(i => i.amount)
      this.option.xAxis.data = xData
      this.option.series[0].data = yData
      this.option.series[0].count = val.map(i => i.count)
      this.option && this.myChart.setOption(this.option)
    }
  },
  methods: {},
  mounted() {
    let chartDom = document.getElementById('line')
    this.myChart = echarts.init(chartDom)
    // this.option && this.myChart.setOption(this.option)
  }
}
</script>

<style lang="scss" scoped>
.chart-box {
  height: 196px;
  width: 100%;
}
.chart-line {
  height: 196px;
}
</style>