yangys
2024-03-08 39ae7e015cf594bd32b05c5e6e039d20b8e28bea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!--
/**
* @name gunter
* @module C:\project-total\hx_web\src\components\gunter.vue
* @description 甘特图
* @author xiashan
* @date 2020-03-12
* @example <gunter></gunter>
*/
-->
<template>
  <div class="gunter-chart">
    <div class="mod-demo-echarts">
      <div :id="gunterId" class="chart-box"></div>
    </div>
  </div>
</template>
 
<script>
import HighCharts from 'highcharts'
import highchartsMore from 'highcharts/highcharts-more'
highchartsMore(HighCharts)
import ECharts from 'vue-echarts/components/ECharts.vue'
import 'echarts/lib/chart/gauge'
import 'echarts/lib/chart/Pie'
import 'echarts/lib/component/tooltip'
 
export default {
  name: 'gunterchart',
  props: {
    gunterData: {
      type: Object,
      required: true
    },
    successData: {
      type: Boolean
    }
  },
  components: {
    ECharts
  },
  data() {
    return {
      gunterline: null,
      option: {},
      gunterId: 'gunterline' + Math.random().toString(36).substr(2) + Date.now(),
      data: this.gunterData.data
    }
  },
  mounted() {
    this.initgunterline()
  },
 
  activated() {
    // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
    if (this.gunterline) {
      this.gunterline.resize()
    }
  },
  methods: {
    // 初始化
    initgunterline() {
      const _self = this
      this.option = {
        chart: { type: 'xrange', marginTop: 30 },
        title: {
          text: '',
          style: {
            fontSize: '15px'
          }
        },
        xAxis: {
          type: 'datetime',
          tickInterval: 50,
          dateTimeLabelFormats: {
            // week: '%Y/%m/%d'
            week: '%Y/%m/%d %H%M'
          }
        },
        yAxis: {
          title: { text: '' },
          categories: [_self.gunterData.yAxistext],
          reversed: true
        },
        legend: { enabled: false },
        credits: { enabled: false },
        exporting: { enabled: false },
        tooltip: {
          formatter: function() {
            var x = new Date(this.x)
            // console.log(this.x + '---x1-' + x2)
 
            var x2 = new Date(this.x2)
            // console.log(this.x2 + '--x2--' + x2)
            var status = this.point.status
            var start = '<b>' + x.getUTCFullYear() + '-' + (x.getUTCMonth() + 1) + '-' + x.getUTCDate() + '  ' + x.getUTCHours() + ':' + x.getUTCMinutes() + ':' + x.getSeconds() + '</b>'
            var end = '<b>' + x2.getUTCFullYear() + '-' + (x2.getUTCMonth() + 1) + '-' + x2.getUTCDate() + '  ' + x2.getUTCHours() + ':' + x2.getUTCMinutes() + ':' + x2.getSeconds() + '</b>'
            var time = status + ':' + start + '至' + end
            return time
          }
        },
        plotOptions: { series: { turboThreshold: 200000 }},
        lang: {
          noData: '暂无数据显示'// 自定义显示文本
        },
        noData: { // 自定义样式
          style: { // 对无数据标签的CSS样式。 默认值:[object Object].
            fontWeight: 'bold',
            fontSize: '18px',
            color: '#333333'
          }
        },
        series: [
          {
            groupPadding: 0,
            borderColor: 'gray',
            pointWidth: 30,
            data: []
          }
        ]
      }
      // eslint-disable-next-line no-undef
      this.brokenline = echarts.init(document.getElementById(this.gunterId))
      // 等待加载loading
      this.brokenline.showLoading({
        text: 'loading',
        color: '#19678E',
        textColor: '#c9c9c9',
        maskColor: '#f0f0f0',
        zlevel: 0
      })
      this.handleSeriesData()
    },
    handleSeriesData() {
      this.option.series[0].data = this.gunterData.data
      this.option.title.text = this.gunterData.title
      this.brokenline.hideLoading()
      this.brokenline.clear()
      // eslint-disable-next-line no-undef
      Highcharts.chart(this.gunterId, this.option)
    }
  },
  watch: {
    gunterData: {
      handler() {
        if (this.successData) {
          this.handleSeriesData()
        }
      },
      deep: true
    }
  }
}
</script>
<style lang="scss">
  .gunter-chart {
    .mod-demo-echarts {
      .chart-box {
        min-height: 150px;
      }
    }
  }
</style>