king
2020-05-18 763a67d39dcb0e5ae49816abcdb9cb7cbc2bd9e0
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Chart } from '@antv/g2'
import DataSet from '@antv/data-set'
 
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import ChartCompileForm from './chartcompile'
import './index.scss'
 
class LineChart extends Component {
  static propTpyes = {
    plot: PropTypes.object,
    config: PropTypes.object,
    plotchange: PropTypes.func
  }
 
  state = {
    dict: (!localStorage.getItem('lang') || localStorage.getItem('lang') === 'zh-CN') ? zhCN : enUS,
    visible: true
  }
 
  componentDidMount () {
    this.viewrender()
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (!is(fromJS(this.props.plot), fromJS(nextProps.plot))) {
      this.setState({}, () => {
        this.viewrender()
      })
    }
  }
 
  getdata = (X_axis, Y_axis) => {
    const { plot } = this.props
    let data = []
    let xdata = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    let point = 7
 
    if (plot.chartType === 'pie') {
      xdata = ['事例一', '事例二', '事例三', '事例四', '事例五']
      point = 5
    }
 
    for (let i = 0; i < point; i++) {
      let item = {}
 
      item[X_axis] = xdata[i]
 
      if (typeof(Y_axis) === 'string') {
        item[Y_axis] = Math.floor(Math.random() * 5 * (i + 1)) + i
      } else {
        Y_axis.forEach(y => {
          item[y] = Math.floor(Math.random() * 5 * (i + 1)) + i
        })
      }
 
      data.push(item)
    }
 
    return data
  }
 
  viewrender = () => {
    const { plot } = this.props
 
    if (plot.chartType === 'line') {
      this.linerender()
    } else if (plot.chartType === 'bar') {
      this.barrender()
    } else if (plot.chartType === 'pie') {
      this.pierender()
    }
  }
 
  linerender = () => {
    const { plot, config } = this.props
 
    let transfield = {}
    config.columns.forEach(col => {
      if (col.field) {
        transfield[col.field] = col.label
      }
    })
    // const colors = ['#f49d37', '#f03838', '#35d1d1', '#5be56b', '#4e7af0', '#ebcc21']
    let X_axis = plot.Xaxis || 'x'
    let Y_axis = plot.Yaxis || ['y']
 
    let data = this.getdata(X_axis, Y_axis)
 
    const ds = new DataSet()
    const dv = ds.createView().source(data)
 
    dv.transform({
      type: 'fold',
      fields: [...Y_axis],
      key: 'key', // key字段
      value: 'value', // value字段
      // retains: [], // 保留字段集,默认为除 fields 以外的所有字段
    })
 
    if (plot.Xaxis) {
      dv.transform({
        type: 'map',
        callback(row) {
          row.key = transfield[row.key]
          return row
        },
      })
    }
    
    const chart = new Chart({
      container: plot.uuid,
      autoFit: true,
      height: plot.height || 400
    })
 
    chart.data(dv.rows)
 
    if (plot.coordinate !== 'polar') {
      chart.scale(X_axis, {
        range: [0, 1]
      })
    }
    chart.scale('value', {
      nice: true
    })
 
    if (!plot.legend || plot.legend === 'hidden') {
      chart.legend(false)
    } else {
      chart.legend({
        position: plot.legend
      })
    }
 
    if (plot.tooltip !== 'true') {
      chart.tooltip(false)
    } else {
      chart.tooltip({
        shared: true
      })
    }
 
    if (plot.transpose === 'true') {
      chart.coordinate().transpose()
    }
 
    if (plot.coordinate === 'polar') {
      chart.coordinate('polar', {
        innerRadius: 0.1,
        radius: 0.8
      })
    }
 
    let _chart = chart
      .line()
      .position(`${X_axis}*value`)
      .color('key')
      .shape(plot.shape || 'smooth')
 
    if (plot.label === 'true') {
      _chart.label('value')
    }
 
    if (plot.point === 'true') {
      chart
        .point()
        .position(`${X_axis}*value`)
        .color('key')
        .size(3)
        .shape('circle')
    }
    
    // chart.interaction('element-active') 
    // chart.removeInteraction('legend-filter') // 自定义图例,移除默认的分类图例筛选交互
    chart.render()
  }
 
  barrender = () => {
    const { plot, config } = this.props
 
    let transfield = {}
    config.columns.forEach(col => {
      if (col.field) {
        transfield[col.field] = col.label
      }
    })
    let X_axis = plot.Xaxis || 'x'
    let Y_axis = plot.Yaxis || ['y']
    if (!plot.Yaxis && plot.modelId !== 'bar1') {
      Y_axis = ['bar1', 'bar2']
    }
 
    let data = this.getdata(X_axis, Y_axis)
 
    const ds = new DataSet()
    const dv = ds.createView().source(data)
 
    dv.transform({
      type: 'fold',
      fields: [...Y_axis],
      key: 'key',
      value: 'value'
    })
 
    if (plot.Xaxis) {
      dv.transform({
        type: 'map',
        callback(row) {
          row.key = transfield[row.key]
          return row
        },
      })
    }
    
    const chart = new Chart({
      container: plot.uuid,
      autoFit: true,
      height: plot.height || 400
    })
 
    chart.data(dv.rows)
 
    chart.scale('value', {
      nice: true
    })
 
    if (!plot.legend || plot.legend === 'hidden') {
      chart.legend(false)
    } else {
      chart.legend({
        position: plot.legend
      })
    }
 
    if (plot.tooltip !== 'true') {
      chart.tooltip(false)
    } else {
      chart.tooltip({
        shared: true
      })
    }
 
    if (plot.transpose === 'true') {
      chart.coordinate().transpose()
    }
 
    if (plot.coordinate === 'polar') {
      chart.coordinate('polar', {
        innerRadius: 0.1,
        radius: 0.8
      })
    }
 
    if (plot.adjust !== 'stack') {
      chart
        .interval()
        .position(`${X_axis}*value`)
        .color('key')
        .adjust([
          {
            type: 'dodge',
            marginRatio: 0
          }
        ])
        .shape(plot.shape || 'rect')
    } else if (plot.adjust === 'stack') {
      chart
        .interval()
        .position(`${X_axis}*value`)
        .color('key')
        .adjust('stack')
        .shape(plot.shape || 'rect')
    }
 
    chart.render()
  }
 
  pierender = () => {
    const { plot, config } = this.props
 
    let transfield = {}
    config.columns.forEach(col => {
      if (col.field) {
        transfield[col.field] = col.label
      }
    })
    let X_axis = plot.Xaxis || 'x'
    let Y_axis = plot.Yaxis || 'y'
 
    let data = this.getdata(X_axis, Y_axis)
 
    const ds = new DataSet()
    const dv = ds.createView().source(data)
 
    if (plot.pieshow !== 'value') {
      dv.transform({
        type: 'percent',
        field: Y_axis,
        dimension: X_axis,
        as: 'percent'
      })
    }
    
    const chart = new Chart({
      container: plot.uuid,
      autoFit: true,
      height: plot.height || 400
    })
 
    chart.data(dv.rows)
 
    if (plot.pieshow !== 'value') {
      chart.scale('percent', {
        formatter: (val) => {
          val = val * 100 + '%'
          return val
        }
      })
    }
 
    chart.coordinate('theta', {
      innerRadius: plot.shape === 'ring' ? 0.6 : 0,
      radius: 0.75,
    })
 
    if (!plot.legend || plot.legend === 'hidden') {
      chart.legend(false)
    } else {
      chart.legend({
        position: plot.legend
      })
    }
 
    if (plot.tooltip !== 'true') {
      chart.tooltip(false)
    } else {
      chart.tooltip({
        showTitle: false,
        showMarkers: false
      })
    }
 
    if (plot.pieshow !== 'value') {
      let _chart = chart
        .interval()
        .adjust('stack')
        .position('percent')
        .color(X_axis)
        .tooltip(X_axis + '*percent', (item, percent) => {
          percent = (percent * 100).toFixed(2) + '%'
          return {
            name: item,
            value: percent
          }
        })
 
      if (plot.label === 'true') {
        let setting = {
          content: (data) => {
            return `${data[X_axis]}: ${(data.percent * 100).toFixed(2)}%`
          }
        }
 
        if (plot.labelLayout === 'overlap') {
          setting.type = 'pie'
          setting.layout = {
            type: 'overlap'
          }
          setting.offset = 0
          // setting.style = {
          //   textAlign: 'center',
          //   fontSize: 12,
          //   fill: '#535353'
          // }
        }
 
        _chart.label('percent', setting)
      }
      
    } else {
      let _chart = chart
        .interval()
        .adjust('stack')
        .position(Y_axis)
        .color(X_axis)
        .tooltip(X_axis + '*' + Y_axis, (item, value) => {
          return {
            name: item,
            value: value
          }
        })
 
      if (plot.label === 'true') {
        let setting = {
          content: (data) => {
            return `${data[X_axis]}: ${data[Y_axis]}`
          }
        }
 
        if (plot.labelLayout === 'overlap') {
          setting.type = 'pie'
          setting.layout = {
            type: 'overlap'
          }
          setting.offset = 0
        }
 
        _chart.label(Y_axis, setting)
      }
    }
    
    chart.render()
  }
 
  plotChange = (values) => {
    const { plot, config } = this.props
    let _plot = {...plot, ...values}
    let _charts = fromJS(config.charts).toJS()
 
    _charts = _charts.map(item => {
      if (item.uuid === _plot.uuid) {
        if (!is(fromJS(item), fromJS(_plot))) {
          let _element = document.getElementById(_plot.uuid)
          if (_element) {
            _element.innerHTML = ''
          }
        }
        return _plot
      }
      return item
    })
 
    this.props.plotchange({...config, charts: _charts})
  }
 
  render() {
    const { plot } = this.props
 
    return (
      <div className="line-chart-edit-box" style={{minHeight: plot.height ? plot.height + 50 : 450}}>
        <p className="chart-title">{plot.title}</p>
        <div className="canvas" id={plot.uuid}></div>
        <ChartCompileForm
          plot={plot}
          type={plot.chartType}
          config={this.props.config}
          dict={this.state.dict}
          plotchange={this.plotChange}
        />
      </div>
    )
  }
}
 
export default LineChart