张延森 5 年之前
父節點
當前提交
3c2ff91aae
共有 2 個檔案被更改,包括 40 行新增51 行删除
  1. 33
    41
      src/useless/components/charts/Line.js
  2. 7
    10
      src/useless/pages/person/customerAnalysis/analysis.js

+ 33
- 41
src/useless/components/charts/Line.js 查看文件

2
 import echarts from '../ec-canvas/echarts'
2
 import echarts from '../ec-canvas/echarts'
3
 import './style.scss'
3
 import './style.scss'
4
 
4
 
5
-function initChart(options, callback) {
6
-  return function (canvas, width, height) {
7
-    const chart = echarts.init(canvas, null, {
8
-      width: width,
9
-      height: height
10
-    });
11
-    canvas.setChart(chart);
12
-    chart.setOption(options);    
13
-    callback && callback(chart);
14
-    return chart;
15
-  }
16
-}
17
-
18
 function createLineOptions(source = []) {
5
 function createLineOptions(source = []) {
19
   const colorWithOpacity = op => `rgba(187,156,121, ${op})`
6
   const colorWithOpacity = op => `rgba(187,156,121, ${op})`
20
 
7
 
106
   chart = null;
93
   chart = null;
107
   ecComponent = null
94
   ecComponent = null
108
 
95
 
109
-  state = {
110
-    ec: { lazyLoad: true },
111
-    isDisposed: false,
112
-  }
113
-
114
-  componentDidShow() {
115
-    this.setState({ isDisposed: false })
116
-  }
117
-
118
-  componentDidHide() {
119
-    this.setState({ isDisposed: true })
120
-  }
96
+  state = {}
121
 
97
 
122
   componentDidMount() {
98
   componentDidMount() {
123
-    this.ecComponent = this.$scope.selectComponent(`#${this.props.lid}`)
99
+    this.ecComponent = this.$scope.selectComponent('#mychart-dom-bar')
124
     this.init(this.props)
100
     this.init(this.props)
125
   }
101
   }
126
 
102
 
127
   componentWillReceiveProps(nextProps) {
103
   componentWillReceiveProps(nextProps) {
128
-    // 简单优化, 不显示的组件不更新
129
-    // 但是有个 bug 。如果此时有 props 更新, 会导致页面绘制的数据错误
130
-    if (this.state.isDisposed) return
131
-
132
-    // 简单的性能优化, 只有数据改变了才重新绘图
133
-    if (JSON.stringify(nextProps.source) != JSON.stringify(this.props.source)) {
134
-      this.init(nextProps)
135
-    }
104
+    this.resetData(nextProps)
136
   }
105
   }
137
 
106
 
138
   init = (props) => {
107
   init = (props) => {
139
-    const tk = setTimeout(() => {
140
-      if (this.ecComponent && props.source && props.source.length) {
141
-        const options = createLineOptions(props.source)
142
-        this.ecComponent.init(initChart(options, chart => (this.chart = chart)))
108
+    if (!this.ecComponent) {
109
+      console.log('-==-------no ecComponent---------')
110
+      return
111
+    }
112
+
113
+    let options;
114
+    if (props.source && props.source.length) {
115
+      options = createLineOptions(props.source)
116
+    }
117
+    
118
+    this.ecComponent.init((canvas, width, height) => {
119
+      const chart = echarts.init(canvas, null, { width, height });
120
+      canvas.setChart(chart);
121
+
122
+      if (options) {
123
+        chart.setOption(options);
143
       }
124
       }
144
 
125
 
145
-      clearTimeout(tk)
146
-    }, 500)
126
+      this.chart = chart  
127
+      return chart;
128
+    })
129
+  }
130
+ 
131
+  resetData(props) {
132
+    // console.log('----------------->', props)
133
+    if (this.chart) {
134
+      const options = createLineOptions(props.source)
135
+      this.chart.setOption(options);
136
+    }
147
   }
137
   }
148
 
138
 
149
   render() {
139
   render() {
140
+    const ec = { lazyLoad: true }
141
+
150
     return (
142
     return (
151
       <View style="width:100vw;height:480rpx;position: relative;">
143
       <View style="width:100vw;height:480rpx;position: relative;">
152
         <View className="map-container">
144
         <View className="map-container">
153
-          <ec-canvas id={this.props.lid} ec={this.state.ec}></ec-canvas>
145
+          <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar"  ec={ec}></ec-canvas>
154
         </View>
146
         </View>
155
       </View>
147
       </View>
156
     )
148
     )

+ 7
- 10
src/useless/pages/person/customerAnalysis/analysis.js 查看文件

12
 import LineChart from "../../../components/charts/Line";
12
 import LineChart from "../../../components/charts/Line";
13
 const nothing = require('@/assets/person/nothing.png')
13
 const nothing = require('@/assets/person/nothing.png')
14
 
14
 
15
-const rand = n => Math.random().toString(36).substr(2, n)
16
-
17
 @connect(({ user, city }) => ({ user, city }))
15
 @connect(({ user, city }) => ({ user, city }))
18
 export default class analysis extends Taro.Component {
16
 export default class analysis extends Taro.Component {
19
 
17
 
20
   config = {
18
   config = {
21
-    navigationBarTitleText: '客户分析'
19
+    navigationBarTitleText: '客户分析',
22
   }
20
   }
23
 
21
 
24
   state = {
22
   state = {
29
     chartInfo: [],
27
     chartInfo: [],
30
     chartInfo2: [],
28
     chartInfo2: [],
31
     chartInfo3: [],
29
     chartInfo3: [],
32
-    lineIds: [ rand(7), rand(7), rand(7) ],
33
   }
30
   }
34
 
31
 
35
   componentWillUnmount() {
32
   componentWillUnmount() {
180
                 </View>
177
                 </View>
181
               </View>
178
               </View>
182
               {
179
               {
183
-                chartInfo.length &&
184
-                <LineChart source={chartInfo} lid={lineIds[0]} />
180
+                chartInfo.length && current == 0 &&
181
+                <LineChart source={chartInfo} />
185
               }
182
               }
186
               {
183
               {
187
                 !chartInfo.length &&
184
                 !chartInfo.length &&
219
                   }
216
                   }
220
                 </View>
217
                 </View>
221
               </View>
218
               </View>
222
-              {chartInfo2.length &&
223
-                <LineChart source={chartInfo2} lid={lineIds[1]}/>
219
+              {chartInfo2.length &&  current == 1 &&
220
+                <LineChart source={chartInfo2} />
224
               }
221
               }
225
               {
222
               {
226
                 !chartInfo2.length &&
223
                 !chartInfo2.length &&
259
                 </View>
256
                 </View>
260
               </View>
257
               </View>
261
               {
258
               {
262
-                chartInfo3.length &&
263
-                <LineChart source={chartInfo3} lid={lineIds[2]} />
259
+                chartInfo3.length && current == 2 &&
260
+                <LineChart source={chartInfo3} />
264
               }
261
               }
265
               {
262
               {
266
                 !chartInfo3.length &&
263
                 !chartInfo3.length &&