Blame view

src/components/charts/lineChart.vue 5.64 KB
77ebf04d   梁保满   个人版
1
2
3
4
5
  <template>
    <div class="chart" :id="id"></div>
  </template>
  
  <script>
6bca489d   LH_PC   云平台二期UI
6
  import 'echarts/lib/component/dataZoom'
77ebf04d   梁保满   个人版
7
8
9
10
11
12
  export default {
    name: "lineChart",
    props: {
      id: String,
      params: Array,
      xAxis: Array,
22095aba   梁保满   接口联调
13
      colors: Array,
ce278878   梁保满   2-2 bugfix
14
      formatterYAxis: true,
6bca489d   LH_PC   云平台二期UI
15
      tooltipFormatter: false
77ebf04d   梁保满   个人版
16
17
18
19
20
21
22
    },
    watch: {
      params: {
        handler: function (val) {
          if (val.length) {
            this.initData();
          }
6bca489d   LH_PC   云平台二期UI
23
          console.log(val)
77ebf04d   梁保满   个人版
24
25
26
27
28
29
30
31
32
        },
        deep: true,
      },
    },
    data() {
      return {
        chart: null,
      };
    },
079cb4cf   梁保满   即时测导出
33
    created() { },
77ebf04d   梁保满   个人版
34
35
36
37
    mounted() {
      this.initData();
    },
    methods: {
6bca489d   LH_PC   云平台二期UI
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
      extension(chart) {
        // 注意这里,是以X轴显示内容过长为例,如果是y轴的话,需要把params.componentType == 'xAxis'改为yAxis
        // 判断是否创建过div框,如果创建过就不再创建了
        // 该div用来盛放文本显示内容的,方便对其悬浮位置进行处理
        var elementDiv = document.getElementById('extension')
        if (!elementDiv) {
          var div = document.createElement('div')
          div.setAttribute('id', 'extension')
          div.style.display = 'block'
          document.querySelector('html').appendChild(div)
        }
        chart.on('mouseover', function (params) {
          if (params.componentType == 'xAxis') {
            var elementDiv = document.querySelector('#extension')
            //设置悬浮文本的位置以及样式
            var elementStyle =
              'position: absolute;z-index: 99999;color: #fff;padding: 5px;display: inline;border-radius: 4px;background-color: #303133;box-shadow: rgba(0, 0, 0, 0.3) 2px 2px 8px'
            elementDiv.style.cssText = elementStyle
            elementDiv.innerHTML = params.value
            document.querySelector('html').onmousemove = function (event) {
              var elementDiv = document.querySelector('#extension')
              var xx = event.pageX - 10
              var yy = event.pageY + 15
              elementDiv.style.top = yy + 'px'
              elementDiv.style.left = xx + 'px'
            }
          }
        })
        chart.on('mouseout', function (params) {
          //注意这里,我是以X轴显示内容过长为例,如果是y轴的话,需要改为yAxis
          if (params.componentType == 'xAxis') {
            var elementDiv = document.querySelector('#extension')
  
            elementDiv.style.cssText = 'display:none'
          }
        })
      },
77ebf04d   梁保满   个人版
75
76
77
      setOption() {
        const that = this;
        const options = {
079cb4cf   梁保满   即时测导出
78
          color: this.colors || ["#9772ff", "#79cd91", "#72b8ff"],
77ebf04d   梁保满   个人版
79
80
81
82
          backgroundColor: "#fff",
          tooltip: {
            trigger: "item",
            confine: true,
ce278878   梁保满   2-2 bugfix
83
84
            formatter(v) {
              let html = `<p>${v.seriesName}</p>`
6bca489d   LH_PC   云平台二期UI
85
              html += `${v.marker} ${v.name}:${Number(v.value)}${that.tooltipFormatter ? '%' : ''}`
ce278878   梁保满   2-2 bugfix
86
87
              return html
            },
77ebf04d   梁保满   个人版
88
          },
6bca489d   LH_PC   云平台二期UI
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
          dataZoom: [{
            type: 'slider',//给x轴设置滚动条
            show: true, //flase直接隐藏图形
            xAxisIndex: [0],
            bottom: 0,
            height: 20,
            showDetail: false,
            startValue: 0,//滚动条的起始位置
            endValue: 9 //滚动条的截止位置(按比例分割你的柱状图x轴长度)
          },
          {
            type: 'inside',//设置鼠标滚轮缩放
            show: true,
            xAxisIndex: [0],
            startValue: 0,
            endValue: 9
          }],
77ebf04d   梁保满   个人版
106
107
          legend: {
            show: true,
079cb4cf   梁保满   即时测导出
108
109
110
111
            top: 0,
            right: 20,
            itemHeight: 15,
            icon: "circle"
77ebf04d   梁保满   个人版
112
          },
6bca489d   LH_PC   云平台二期UI
113
114
115
116
117
118
119
120
121
122
123
124
          grid: {
            top: '3%',
            right: '3%',
            bottom: '3%',
            left: '0%',
            // 包含文本
            containLabel: true,
            // 是否显示网格线
            show: true,
            // 边框颜色
            borderColor: 'rgba(0, 240, 255, 0.3)',
          },
77ebf04d   梁保满   个人版
125
126
127
128
129
          xAxis: {
            type: "category",
            data: this.xAxis,
            axisLine: { show: true, lineStyle: { color: "#e2e2e2" } },
            axisTick: {
6bca489d   LH_PC   云平台二期UI
130
              alignWithLabel: false,
77ebf04d   梁保满   个人版
131
132
              show: false,
            },
6bca489d   LH_PC   云平台二期UI
133
            triggerEvent: true,
77ebf04d   梁保满   个人版
134
            axisLabel: {
079cb4cf   梁保满   即时测导出
135
              color: "#333",
384a2a54   梁保满   请求头添加班主任信息,bug修改
136
137
              interval: 0,
              width: Math.ceil(600 / this.xAxis.length),
6bca489d   LH_PC   云平台二期UI
138
139
140
141
142
143
              formatter: function (value) {
                if (value.length > 3) {
                  return `${value.slice(0, 3)}...`
                }
                return value
              }
77ebf04d   梁保满   个人版
144
145
146
147
148
149
150
151
152
153
            },
          },
          yAxis: {
            type: "value",
            axisLine: { show: false },
            splitLine: {
              show: true,
            },
            axisLabel: {
              color: "#666",
079cb4cf   梁保满   即时测导出
154
              formatter: function (name) {
22095aba   梁保满   接口联调
155
                return that.formatterYAxis ? `${name} %` : name
079cb4cf   梁保满   即时测导出
156
              },
77ebf04d   梁保满   个人版
157
158
159
            },
          },
          grid: {
079cb4cf   梁保满   即时测导出
160
161
162
            top: 36,
            left: 20,
            right: 20,
384a2a54   梁保满   请求头添加班主任信息,bug修改
163
            bottom: 10,
77ebf04d   梁保满   个人版
164
165
166
167
168
169
170
            containLabel: true,
          },
          series: that.params.map((item) => {
            return {
              name: item.name,
              type: "line",
              symbol: "circle",
079cb4cf   梁保满   即时测导出
171
              symbolSize: "6",
77ebf04d   梁保满   个人版
172
              lineStyle: {
079cb4cf   梁保满   即时测导出
173
                width: 1,
77ebf04d   梁保满   个人版
174
175
176
177
              },
              data: item.value,
            };
          }),
6bca489d   LH_PC   云平台二期UI
178
        };  
77ebf04d   梁保满   个人版
179
180
181
182
183
184
185
186
187
188
189
        return options;
      },
      initData() {
        if (!this.chart) {
          const div = document.getElementById(this.id);
          this.chart = this.$echarts.init(div);
        }
        const options = this.setOption();
        this.chart?.clear();
        this.chart.setOption(options, true);
        this.chart.off("click");
6bca489d   LH_PC   云平台二期UI
190
        this.extension(this.chart)
77ebf04d   梁保满   个人版
191
192
193
194
195
196
197
198
199
200
201
202
203
        this.chart.on("click", "series", (params) => {
          // this.$emit("clickPieChart", params);
        });
      },
    },
  };
  </script>
  
  <style lang="scss" scoped>
  .chart {
    height: 100%;
  }
  </style>