-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathanychart-react.js
286 lines (235 loc) · 8.63 KB
/
anychart-react.js
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
require('anychart');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* AnyChart React plugin.
*/
var AnyChart = function (_React$Component) {
_inherits(AnyChart, _React$Component);
function AnyChart(props) {
_classCallCheck(this, AnyChart);
/**
* Instance (stage or chart).
* @type {Object}
*/
var _this = _possibleConstructorReturn(this, (AnyChart.__proto__ || Object.getPrototypeOf(AnyChart)).call(this, props));
_this.instance = null;
/**
* Whether instance is stage.
* @type {boolean}
*/
_this.isStage = false;
/**
* Should we dispose instance or not.
* @type {boolean}
*/
_this.disposeInstance = false;
/**
* Properties of AnyChart which expected as array of [entity_index, json].
* E.g. <AnyChart yAxis={[1, {orientation: 'right'}]} />
* @type {Array.<string>}
*/
_this.multipleEntities = ['xAxis', 'yAxis', 'lineMarker', 'rangeMarker', 'textMarker', 'grid', 'minorGrid'];
/**
* Container for chart/stage.
* @type {string}
*/
_this.containerId = props.id || 'ac-chart-container';
return _this;
}
/**
* Remove instance (dispose it if necessary).
*/
_createClass(AnyChart, [{
key: 'removeInstance',
value: function removeInstance() {
if (this.instance) {
if (this.disposeInstance) this.instance.dispose();else {
if (this.isStage) this.instance.remove();else this.instance.container().getStage().remove();
}
}
}
/**
* Checker for array.
* @param {*} value Value to check.
* @return {boolean}
*/
}, {
key: 'isArray',
value: function isArray(value) {
return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) == 'object' && value instanceof Array;
}
/**
* Applies props.
* @param {Object} props Properties.
*/
}, {
key: 'applyProps',
value: function applyProps(props) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = Object.keys(props)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
var value = props[key];
if ((key == 'width' || key == 'height') && !this.isStage) this.instance.container().getStage()[key](value);
if (this.instance[key]) {
if (~this.multipleEntities.indexOf(key)) {
var _instance;
if (!this.isArray(value)) value = [value];
(_instance = this.instance)[key].apply(_instance, _toConsumableArray(value));
} else this.instance[key](value);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
/**
* Create instance to render chart or use instance property.
* @param {Object} props Properties.
*/
}, {
key: 'createInstance',
value: function createInstance(props) {
if (props.instance) {
this.removeInstance();
this.instance = props.instance;
this.isStage = typeof this.instance.draw !== 'function';
delete props.instance;
this.disposeInstance = false;
} else if (props.type) {
this.removeInstance();
this.disposeInstance = true;
this.instance = anychart[props.type](props.data);
this.isStage = false;
delete props.type;
delete props.data;
}
if (this.instance) this.instance.container(this.containerId);
delete props.id;
}
/**
* Draws chart.
* @param {Object} props Properties.
*/
}, {
key: 'drawInstance',
value: function drawInstance(props) {
if (!this.instance) return;
if (this.isStage) {
this.instance.suspend();
var charts = props.charts;
delete props.charts;
this.applyProps(props);
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = charts[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var chart = _step2.value;
chart.container(this.instance).draw();
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
this.instance.resume();
} else {
this.applyProps(props);
this.instance.draw();
}
}
/**
* Method that
* @param {Object} prevProps
*/
}, {
key: 'createAndDraw',
value: function createAndDraw(prevProps) {
var props = Object.assign(prevProps, this.props);
this.createInstance(props);
this.drawInstance(props);
}
/**
* Render container for future chart drawing.
*/
}, {
key: 'render',
value: function render() {
return _react2.default.createElement('div', { id: this.containerId });
}
/**
* Component has rendered.
*/
}, {
key: 'componentDidMount',
value: function componentDidMount() {
this.createAndDraw({});
}
}, {
key: 'componentWillUpdate',
value: function componentWillUpdate(nextProps, nextState) {
this.containerId = nextProps.id || this.containerId;
}
/**
* Component has re-rendered.
* @param {Object} prevProps Previous properties.
* @param {Object} prevState Previous state.
*/
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps, prevState) {
var props = Object.assign({}, prevProps);
delete props.type;
delete props.instance;
this.createAndDraw(props);
}
/**
* Unmount react component.
*/
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.removeInstance();
}
}]);
return AnyChart;
}(_react2.default.Component);
/**
* Default export.
*/
exports.default = AnyChart;