Blame view

src/components/lineChart.vue 3 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
      }
    },
    watch: {
adf82b69   liuqimichale   调取接口
39
40
41
42
      chartData:function () {
        this.$nextTick(()=>{
          this.initChart()
        })
feb955a5   liuqimichale   车流量section
43
44
45
      }
    },
    mounted() {
feb955a5   liuqimichale   车流量section
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      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,
159ba68d   liuqimichale   火狐兼容性
75
            bottom:'-4px',
8a59623c   liuqimichale   进出场
76
77
78
            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
            },
159ba68d   liuqimichale   火狐兼容性
126
127
            splitNumber:3,
            splitLine: {lineStyle: {color: '#10377c'}}
8a59623c   liuqimichale   进出场
128
129
          },
          series: [
feb955a5   liuqimichale   车流量section
130
            {
4831b33d   liuqimichale   进出场
131
              name:legendData[0],
feb955a5   liuqimichale   车流量section
132
              type:'line',
4831b33d   liuqimichale   进出场
133
              data:yData.indata,
8a59623c   liuqimichale   进出场
134
135
136
              lineStyle: {
                color: '#FFB130'
              }
feb955a5   liuqimichale   车流量section
137
138
            },
            {
4831b33d   liuqimichale   进出场
139
              name:legendData[1],
feb955a5   liuqimichale   车流量section
140
              type:'line',
4831b33d   liuqimichale   进出场
141
              data:yData.outdata,
8a59623c   liuqimichale   进出场
142
143
144
              lineStyle: {
                color: '#08B9FC'
              }
feb955a5   liuqimichale   车流量section
145
146
147
148
149
150
            }
          ]
  
        })
      },
      initChart() {
8a59623c   liuqimichale   进出场
151
        this.chart = echarts.init(this.$el)
feb955a5   liuqimichale   车流量section
152
153
154
155
156
        this.setOptions(this.chartData)
      }
    }
  }
  </script>