Blame view

src/components/lineChart.vue 2.94 KB
feb955a5   liuqimichale   车流量section
1
2
3
4
5
6
  <template>
    <div :class="className" :style="{height:height,width:width}"></div>
  </template>
  
  <script>
  import echarts from 'echarts'
feb955a5   liuqimichale   车流量section
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  import {debounce} from '../utils/debounce'
  
  export default {
    name: 'lineChart',
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
8a59623c   liuqimichale   进出场
22
        default: '100%'
feb955a5   liuqimichale   车流量section
23
24
25
26
27
28
29
30
31
32
33
34
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      chartData: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
4831b33d   liuqimichale   进出场
35
        chart: null
feb955a5   liuqimichale   车流量section
36
37
38
39
40
41
42
43
      }
    },
    watch: {
      chartData: {
        deep: true,
      }
    },
    mounted() {
4831b33d   liuqimichale   进出场
44
      console.log(this.chartData)
feb955a5   liuqimichale   车流量section
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      this.initChart()
      if (this.autoResize) {
        this.__resizeHandler = debounce(() => {
          if (this.chart) {
            this.chart.resize()
          }
        }, 100)
        window.addEventListener('resize', this.__resizeHandler)
      }
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      if (this.autoResize) {
        window.removeEventListener('resize', this.__resizeHandler)
      }
  
      this.chart.dispose()
      this.chart = null
    },
    methods: {
4831b33d   liuqimichale   进出场
67
      setOptions({xData, yData, legendData} = {}) {
feb955a5   liuqimichale   车流量section
68
69
70
71
        this.chart.setOption({
          tooltip: {
            trigger: 'axis'
          },
8a59623c   liuqimichale   进出场
72
          color:['#FFB130','#01AEFE'],
feb955a5   liuqimichale   车流量section
73
          legend: {
4831b33d   liuqimichale   进出场
74
            data:legendData,
8a59623c   liuqimichale   进出场
75
76
77
78
            bottom:'2%',
            textStyle: {
              color: '#59AAF7'
            }
feb955a5   liuqimichale   车流量section
79
80
          },
          grid: {
8a59623c   liuqimichale   进出场
81
            top:'8%',
feb955a5   liuqimichale   车流量section
82
83
            left: '3%',
            right: '4%',
8a59623c   liuqimichale   进出场
84
            bottom: '14%',
feb955a5   liuqimichale   车流量section
85
86
87
88
            containLabel: true
          },
  
          xAxis: {
8a59623c   liuqimichale   进出场
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            axisLabel : {
              textStyle: {
                color: '#fff'
              }
            },
            axisLine:{
              lineStyle:{
                color:'rgba(89,170,247,.3)'
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
4831b33d   liuqimichale   进出场
105
            data: xData
feb955a5   liuqimichale   车流量section
106
107
          },
          yAxis: {
8a59623c   liuqimichale   进出场
108
109
110
111
112
113
            type: 'value',
            axisLabel : {
              formatter: '{value}',
              textStyle: {
                color: '#fff'
              }
feb955a5   liuqimichale   车流量section
114
            },
8a59623c   liuqimichale   进出场
115
116
117
118
            axisLine:{
              lineStyle:{
                color:'rgba(89,170,247,.3)'
              }
feb955a5   liuqimichale   车流量section
119
            },
8a59623c   liuqimichale   进出场
120
121
122
123
124
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
feb955a5   liuqimichale   车流量section
125
            },
8a59623c   liuqimichale   进出场
126
127
128
            splitNumber:3
          },
          series: [
feb955a5   liuqimichale   车流量section
129
            {
4831b33d   liuqimichale   进出场
130
              name:legendData[0],
feb955a5   liuqimichale   车流量section
131
              type:'line',
4831b33d   liuqimichale   进出场
132
              data:yData.indata,
8a59623c   liuqimichale   进出场
133
134
135
              lineStyle: {
                color: '#FFB130'
              }
feb955a5   liuqimichale   车流量section
136
137
            },
            {
4831b33d   liuqimichale   进出场
138
              name:legendData[1],
feb955a5   liuqimichale   车流量section
139
              type:'line',
4831b33d   liuqimichale   进出场
140
              data:yData.outdata,
8a59623c   liuqimichale   进出场
141
142
143
              lineStyle: {
                color: '#08B9FC'
              }
feb955a5   liuqimichale   车流量section
144
145
146
147
148
149
            }
          ]
  
        })
      },
      initChart() {
8a59623c   liuqimichale   进出场
150
        this.chart = echarts.init(this.$el)
feb955a5   liuqimichale   车流量section
151
152
153
154
155
        this.setOptions(this.chartData)
      }
    }
  }
  </script>