dataProvider.js
11.1 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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var _config = require("../../config");
var __DEV__ = _config.__DEV__;
var _util = require("zrender/lib/core/util");
var isTypedArray = _util.isTypedArray;
var extend = _util.extend;
var assert = _util.assert;
var each = _util.each;
var isObject = _util.isObject;
var _model = require("../../util/model");
var getDataItemValue = _model.getDataItemValue;
var isDataItemOption = _model.isDataItemOption;
var _number = require("../../util/number");
var parseDate = _number.parseDate;
var Source = require("../Source");
var _sourceType = require("./sourceType");
var SOURCE_FORMAT_TYPED_ARRAY = _sourceType.SOURCE_FORMAT_TYPED_ARRAY;
var SOURCE_FORMAT_ARRAY_ROWS = _sourceType.SOURCE_FORMAT_ARRAY_ROWS;
var SOURCE_FORMAT_ORIGINAL = _sourceType.SOURCE_FORMAT_ORIGINAL;
var SOURCE_FORMAT_OBJECT_ROWS = _sourceType.SOURCE_FORMAT_OBJECT_ROWS;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// TODO
// ??? refactor? check the outer usage of data provider.
// merge with defaultDimValueGetter?
/**
* If normal array used, mutable chunk size is supported.
* If typed array used, chunk size must be fixed.
*/
function DefaultDataProvider(source, dimSize) {
if (!Source.isInstance(source)) {
source = Source.seriesDataToSource(source);
}
this._source = source;
var data = this._data = source.data;
var sourceFormat = source.sourceFormat; // Typed array. TODO IE10+?
if (sourceFormat === SOURCE_FORMAT_TYPED_ARRAY) {
this._offset = 0;
this._dimSize = dimSize;
this._data = data;
}
var methods = providerMethods[sourceFormat === SOURCE_FORMAT_ARRAY_ROWS ? sourceFormat + '_' + source.seriesLayoutBy : sourceFormat];
extend(this, methods);
}
var providerProto = DefaultDataProvider.prototype; // If data is pure without style configuration
providerProto.pure = false; // If data is persistent and will not be released after use.
providerProto.persistent = true; // ???! FIXME legacy data provider do not has method getSource
providerProto.getSource = function () {
return this._source;
};
var providerMethods = {
'arrayRows_column': {
pure: true,
count: function () {
return Math.max(0, this._data.length - this._source.startIndex);
},
getItem: function (idx) {
return this._data[idx + this._source.startIndex];
},
appendData: appendDataSimply
},
'arrayRows_row': {
pure: true,
count: function () {
var row = this._data[0];
return row ? Math.max(0, row.length - this._source.startIndex) : 0;
},
getItem: function (idx) {
idx += this._source.startIndex;
var item = [];
var data = this._data;
for (var i = 0; i < data.length; i++) {
var row = data[i];
item.push(row ? row[idx] : null);
}
return item;
},
appendData: function () {
throw new Error('Do not support appendData when set seriesLayoutBy: "row".');
}
},
'objectRows': {
pure: true,
count: countSimply,
getItem: getItemSimply,
appendData: appendDataSimply
},
'keyedColumns': {
pure: true,
count: function () {
var dimName = this._source.dimensionsDefine[0].name;
var col = this._data[dimName];
return col ? col.length : 0;
},
getItem: function (idx) {
var item = [];
var dims = this._source.dimensionsDefine;
for (var i = 0; i < dims.length; i++) {
var col = this._data[dims[i].name];
item.push(col ? col[idx] : null);
}
return item;
},
appendData: function (newData) {
var data = this._data;
each(newData, function (newCol, key) {
var oldCol = data[key] || (data[key] = []);
for (var i = 0; i < (newCol || []).length; i++) {
oldCol.push(newCol[i]);
}
});
}
},
'original': {
count: countSimply,
getItem: getItemSimply,
appendData: appendDataSimply
},
'typedArray': {
persistent: false,
pure: true,
count: function () {
return this._data ? this._data.length / this._dimSize : 0;
},
getItem: function (idx, out) {
idx = idx - this._offset;
out = out || [];
var offset = this._dimSize * idx;
for (var i = 0; i < this._dimSize; i++) {
out[i] = this._data[offset + i];
}
return out;
},
appendData: function (newData) {
this._data = newData;
},
// Clean self if data is already used.
clean: function () {
// PENDING
this._offset += this.count();
this._data = null;
}
}
};
function countSimply() {
return this._data.length;
}
function getItemSimply(idx) {
return this._data[idx];
}
function appendDataSimply(newData) {
for (var i = 0; i < newData.length; i++) {
this._data.push(newData[i]);
}
}
var rawValueGetters = {
arrayRows: getRawValueSimply,
objectRows: function (dataItem, dataIndex, dimIndex, dimName) {
return dimIndex != null ? dataItem[dimName] : dataItem;
},
keyedColumns: getRawValueSimply,
original: function (dataItem, dataIndex, dimIndex, dimName) {
// FIXME
// In some case (markpoint in geo (geo-map.html)), dataItem
// is {coord: [...]}
var value = getDataItemValue(dataItem);
return dimIndex == null || !(value instanceof Array) ? value : value[dimIndex];
},
typedArray: getRawValueSimply
};
function getRawValueSimply(dataItem, dataIndex, dimIndex, dimName) {
return dimIndex != null ? dataItem[dimIndex] : dataItem;
}
var defaultDimValueGetters = {
arrayRows: getDimValueSimply,
objectRows: function (dataItem, dimName, dataIndex, dimIndex) {
return converDataValue(dataItem[dimName], this._dimensionInfos[dimName]);
},
keyedColumns: getDimValueSimply,
original: function (dataItem, dimName, dataIndex, dimIndex) {
// Performance sensitive, do not use modelUtil.getDataItemValue.
// If dataItem is an plain object with no value field, the var `value`
// will be assigned with the object, but it will be tread correctly
// in the `convertDataValue`.
var value = dataItem && (dataItem.value == null ? dataItem : dataItem.value); // If any dataItem is like { value: 10 }
if (!this._rawData.pure && isDataItemOption(dataItem)) {
this.hasItemOption = true;
}
return converDataValue(value instanceof Array ? value[dimIndex] // If value is a single number or something else not array.
: value, this._dimensionInfos[dimName]);
},
typedArray: function (dataItem, dimName, dataIndex, dimIndex) {
return dataItem[dimIndex];
}
};
function getDimValueSimply(dataItem, dimName, dataIndex, dimIndex) {
return converDataValue(dataItem[dimIndex], this._dimensionInfos[dimName]);
}
/**
* This helper method convert value in data.
* @param {string|number|Date} value
* @param {Object|string} [dimInfo] If string (like 'x'), dimType defaults 'number'.
* If "dimInfo.ordinalParseAndSave", ordinal value can be parsed.
*/
function converDataValue(value, dimInfo) {
// Performance sensitive.
var dimType = dimInfo && dimInfo.type;
if (dimType === 'ordinal') {
// If given value is a category string
var ordinalMeta = dimInfo && dimInfo.ordinalMeta;
return ordinalMeta ? ordinalMeta.parseAndCollect(value) : value;
}
if (dimType === 'time' // spead up when using timestamp
&& typeof value !== 'number' && value != null && value !== '-') {
value = +parseDate(value);
} // dimType defaults 'number'.
// If dimType is not ordinal and value is null or undefined or NaN or '-',
// parse to NaN.
return value == null || value === '' ? NaN // If string (like '-'), using '+' parse to NaN
// If object, also parse to NaN
: +value;
} // ??? FIXME can these logic be more neat: getRawValue, getRawDataItem,
// Consider persistent.
// Caution: why use raw value to display on label or tooltip?
// A reason is to avoid format. For example time value we do not know
// how to format is expected. More over, if stack is used, calculated
// value may be 0.91000000001, which have brings trouble to display.
// TODO: consider how to treat null/undefined/NaN when display?
/**
* @param {module:echarts/data/List} data
* @param {number} dataIndex
* @param {string|number} [dim] dimName or dimIndex
* @return {Array.<number>|string|number} can be null/undefined.
*/
function retrieveRawValue(data, dataIndex, dim) {
if (!data) {
return;
} // Consider data may be not persistent.
var dataItem = data.getRawDataItem(dataIndex);
if (dataItem == null) {
return;
}
var sourceFormat = data.getProvider().getSource().sourceFormat;
var dimName;
var dimIndex;
var dimInfo = data.getDimensionInfo(dim);
if (dimInfo) {
dimName = dimInfo.name;
dimIndex = dimInfo.index;
}
return rawValueGetters[sourceFormat](dataItem, dataIndex, dimIndex, dimName);
}
/**
* Compatible with some cases (in pie, map) like:
* data: [{name: 'xx', value: 5, selected: true}, ...]
* where only sourceFormat is 'original' and 'objectRows' supported.
*
* ??? TODO
* Supported detail options in data item when using 'arrayRows'.
*
* @param {module:echarts/data/List} data
* @param {number} dataIndex
* @param {string} attr like 'selected'
*/
function retrieveRawAttr(data, dataIndex, attr) {
if (!data) {
return;
}
var sourceFormat = data.getProvider().getSource().sourceFormat;
if (sourceFormat !== SOURCE_FORMAT_ORIGINAL && sourceFormat !== SOURCE_FORMAT_OBJECT_ROWS) {
return;
}
var dataItem = data.getRawDataItem(dataIndex);
if (sourceFormat === SOURCE_FORMAT_ORIGINAL && !isObject(dataItem)) {
dataItem = null;
}
if (dataItem) {
return dataItem[attr];
}
}
exports.DefaultDataProvider = DefaultDataProvider;
exports.defaultDimValueGetters = defaultDimValueGetters;
exports.retrieveRawValue = retrieveRawValue;
exports.retrieveRawAttr = retrieveRawAttr;