scatterPlot.vue 2.79 KB
Newer Older
乐宝呗666's avatar
乐宝呗666 committed
1 2 3 4 5 6
<template>
    <div :id="idstr"></div>
</template>

<script>
// 引入基本模板
7
const echarts = require('echarts/lib/echarts')
乐宝呗666's avatar
乐宝呗666 committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 引入提示框和title组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')

export default {
  data () {
    return {
      dataObj: {},
      option: {},
      chart: null,
      clickNum: [1, 1, 1, 1, 1]
    }
  },
  props: ['message', 'idstr'],
  mounted () {
    this.drawLine()
  },
  methods: {
    drawLine () {
27
      const vm = this
乐宝呗666's avatar
乐宝呗666 committed
28 29 30 31 32 33 34 35 36 37 38 39
      const bars = []
      this.message.title.forEach(e => {
        bars.push({ type: 'bar' })
      })
      const source = []
      const x = Object.keys(this.message.data)
      for (let i = 0; i < x.length; i++) {
        const da = this.message.data[x[i]]
        const arr = []
        for (let j = 0; j < this.message.title.length; j++) {
          let flag = true
          da.forEach(arg => {
40 41
            if (arg.type === this.message.title[j]) {
              arr.push(arg.num)
乐宝呗666's avatar
乐宝呗666 committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
              flag = false
            }
          })
          if (flag) {
            arr.push(0)
          }
        }
        arr.unshift(x[i])
        source.push(arr)
      }
      const title = this.message.title
      title.unshift('product')
      source.unshift(title)
      // 基于准备好的dom,初始化echarts实例
      vm.chart = echarts.init(document.getElementById(vm.idstr))
      // 清空图表
      vm.chart.clear()
      vm.option = {
        color: ['#30DDF4', '#E23AA2', '#F5A623', '#D6FFBA'],
61
        grid: {
乐宝呗666's avatar
乐宝呗666 committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
          height: 135,
          top: 50
        },
        legend: {
          show: true,
          textStyle: {
            color: '#ffffff' // 图例文字颜色
          }
        },
        tooltip: {},
        dataset: {
          source: source
        },
        xAxis: {
          type: 'category',
          axisLine: {
            show: true,
            lineStyle: {
              color: '#675bba'
            }
          }
        },
        yAxis: {
          axisLine: {
            show: true,
            lineStyle: {
              color: '#675bba'
            }
          }
        },
        // Declare several bar series, each will be mapped
        // to a column of dataset.source by default.
        series: bars
      }
      vm.drawBar()
      // window.addEventListener("resize",()=>{
      //     chart.resize();
      // });
    },
    drawBar () {
      // 绘制图表
103
      const vm = this
乐宝呗666's avatar
乐宝呗666 committed
104 105 106 107
      vm.chart.setOption(vm.option)
      vm.eventList()
    },
    eventList () {
108
      const vm = this
乐宝呗666's avatar
乐宝呗666 committed
109 110 111 112 113 114
      vm.chart.on('click', function (params) {
      })
    }
  },
  watch: {
    message: function () {
115
      const vm = this
乐宝呗666's avatar
乐宝呗666 committed
116 117 118 119 120 121 122 123 124 125 126 127
      setTimeout(function () {
        vm.drawLine()
      }, 1000)
      // console.log(this.message)
    }
  }
}
</script>

<style lang="less">

</style>