scatterPlot.vue 3 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 40 41
      const bars = []
      this.message.title.forEach(e => {
        bars.push({ type: 'bar' })
      })
      const source = []
      const x = Object.keys(this.message.data)
      // console.log(x)
      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 => {
            // console.log(arg["type"], this.message.title[j])
42 43
            if (arg.type === this.message.title[j]) {
              arr.push(arg.num)
乐宝呗666's avatar
乐宝呗666 committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
              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 = {
62 63
        color: ['#30DDF4', '#E23AA2', '#F5A623', '#D6FFBA'],
        grid: {
乐宝呗666's avatar
乐宝呗666 committed
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 103 104 105 106 107
          height: 135,
          top: 40
        },
        legend: {
          show: true,
          textStyle: {
            color: '#ffffff' // 图例文字颜色
          }
          // data: this.message.title
          // left: 'right'
        },
        tooltip: {},
        dataset: {
          source: source
        },
        xAxis: {
          type: 'category',
          axisLine: {
            show: true,
            lineStyle: {
              color: '#675bba'
            }
          }
        },
        yAxis: {
          // data: ['10', '20', '30', '40', '50', '60', '70'],
          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 () {
      // 绘制图表
108
      const vm = this
乐宝呗666's avatar
乐宝呗666 committed
109 110 111 112
      vm.chart.setOption(vm.option)
      vm.eventList()
    },
    eventList () {
113
      const vm = this
乐宝呗666's avatar
乐宝呗666 committed
114 115 116 117 118 119
      vm.chart.on('click', function (params) {
      })
    }
  },
  watch: {
    message: function () {
120
      const vm = this
乐宝呗666's avatar
乐宝呗666 committed
121 122 123 124 125 126 127 128 129 130 131 132
      setTimeout(function () {
        vm.drawLine()
      }, 1000)
      // console.log(this.message)
    }
  }
}
</script>

<style lang="less">

</style>