index.js
2.72 KB
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
/* eslint-disable no-undef */
import React, { PureComponent } from 'react';
// import G2 from '@antv/g2/build/g2';
import { Chart } from '@antv/g2';
import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
import equal from '../equal';
class ColorBlock extends PureComponent {
componentDidMount() {
this.renderChart(this.props.data);
window.addEventListener('resize', this.resize);
}
componentWillReceiveProps(nextProps) {
if (!equal(this.props, nextProps)) {
this.renderChart(nextProps.data);
}
}
componentWillUnmount() {
window.removeEventListener('resize', this.resize);
if (this.chart) {
this.chart.destroy();
}
this.resize.cancel();
}
@Bind()
@Debounce(200)
resize() {
if (!this.node) {
return;
}
this.renderChart([]);
}
handleRef = (n) => {
this.node = n;
};
renderChart(data) {
if (this.node.offsetWidth === 0) {
return;
}
if (this.chart) {
this.chart.destroy();
}
this.node.innerHTML = '';
const { day, sMachines, height } = this.props;
const source = data.map((arr) => {
return {
xValue: arr[0],
yValue: arr[1],
value: arr[2],
};
});
const chart = new Chart({
container: this.node,
autoFit: true,
height,
});
chart.data(source);
chart.scale('xValue', {
type: 'cat',
values: day,
});
chart.scale('yValue', {
type: 'cat',
values: sMachines,
});
chart.scale('value', {
nice: true,
});
chart.axis('xValue', {
tickLine: null,
grid: {
alignTick: false,
line: {
style: {
lineWidth: 1,
lineDash: null,
stroke: '#f0f0f0',
},
},
},
});
chart.axis('yValue', {
title: null,
grid: {
alignTick: false,
line: {
style: {
lineWidth: 1,
lineDash: null,
stroke: '#f0f0f0',
},
},
},
});
chart.tooltip({
showMarkers: false,
});
chart
.polygon()
.position('xValue*yValue')
.color('value', '#f6ffed-#389e0d-#135200')
.label('value', {
offset: -2,
style: {
fill: '#fff',
shadowBlur: 2,
shadowColor: 'rgba(0, 0, 0, .45)',
},
})
.style({
lineWidth: 1,
stroke: '#fff',
});
chart.interaction('element-active');
chart.render();
this.chart = chart;
}
render() {
const { height } = this.props;
return (
<div style={{ height }}>
<div ref={this.handleRef} />
</div>
);
}
}
export default ColorBlock;