dataProvider.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. var _a, _b, _c, _d;
  41. // TODO
  42. // ??? refactor? check the outer usage of data provider.
  43. // merge with defaultDimValueGetter?
  44. import { isTypedArray, extend, assert, each, isObject, bind, isArray } from 'zrender/lib/core/util.js';
  45. import { getDataItemValue } from '../../util/model.js';
  46. import { createSourceFromSeriesDataOption, isSourceInstance } from '../Source.js';
  47. import { SOURCE_FORMAT_ORIGINAL, SOURCE_FORMAT_OBJECT_ROWS, SOURCE_FORMAT_KEYED_COLUMNS, SOURCE_FORMAT_TYPED_ARRAY, SOURCE_FORMAT_ARRAY_ROWS, SERIES_LAYOUT_BY_COLUMN, SERIES_LAYOUT_BY_ROW } from '../../util/types.js';
  48. import { error } from '../../util/log.js';
  49. var providerMethods;
  50. var mountMethods;
  51. /**
  52. * If normal array used, mutable chunk size is supported.
  53. * If typed array used, chunk size must be fixed.
  54. */
  55. var DefaultDataProvider = /** @class */function () {
  56. function DefaultDataProvider(sourceParam, dimSize) {
  57. // let source: Source;
  58. var source = !isSourceInstance(sourceParam) ? createSourceFromSeriesDataOption(sourceParam) : sourceParam;
  59. // declare source is Source;
  60. this._source = source;
  61. var data = this._data = source.data;
  62. var sourceFormat = source.sourceFormat;
  63. var seriesLayoutBy = source.seriesLayoutBy;
  64. // Typed array. TODO IE10+?
  65. if (sourceFormat === SOURCE_FORMAT_TYPED_ARRAY) {
  66. if (process.env.NODE_ENV !== 'production') {
  67. if (dimSize == null) {
  68. throw new Error('Typed array data must specify dimension size');
  69. }
  70. }
  71. this._offset = 0;
  72. this._dimSize = dimSize;
  73. this._data = data;
  74. }
  75. if (process.env.NODE_ENV !== 'production') {
  76. var validator = rawSourceDataValidatorMap[getMethodMapKey(sourceFormat, seriesLayoutBy)];
  77. validator && validator(data, source.dimensionsDefine);
  78. }
  79. mountMethods(this, data, source);
  80. }
  81. DefaultDataProvider.prototype.getSource = function () {
  82. return this._source;
  83. };
  84. DefaultDataProvider.prototype.count = function () {
  85. return 0;
  86. };
  87. DefaultDataProvider.prototype.getItem = function (idx, out) {
  88. return;
  89. };
  90. DefaultDataProvider.prototype.appendData = function (newData) {};
  91. DefaultDataProvider.prototype.clean = function () {};
  92. DefaultDataProvider.protoInitialize = function () {
  93. // PENDING: To avoid potential incompat (e.g., prototype
  94. // is visited somewhere), still init them on prototype.
  95. var proto = DefaultDataProvider.prototype;
  96. proto.pure = false;
  97. proto.persistent = true;
  98. }();
  99. DefaultDataProvider.internalField = function () {
  100. var _a;
  101. mountMethods = function (provider, data, source) {
  102. var sourceFormat = source.sourceFormat;
  103. var seriesLayoutBy = source.seriesLayoutBy;
  104. var startIndex = source.startIndex;
  105. var dimsDef = source.dimensionsDefine;
  106. var methods = providerMethods[getMethodMapKey(sourceFormat, seriesLayoutBy)];
  107. if (process.env.NODE_ENV !== 'production') {
  108. assert(methods, 'Invalide sourceFormat: ' + sourceFormat);
  109. }
  110. extend(provider, methods);
  111. if (sourceFormat === SOURCE_FORMAT_TYPED_ARRAY) {
  112. provider.getItem = getItemForTypedArray;
  113. provider.count = countForTypedArray;
  114. provider.fillStorage = fillStorageForTypedArray;
  115. } else {
  116. var rawItemGetter = getRawSourceItemGetter(sourceFormat, seriesLayoutBy);
  117. provider.getItem = bind(rawItemGetter, null, data, startIndex, dimsDef);
  118. var rawCounter = getRawSourceDataCounter(sourceFormat, seriesLayoutBy);
  119. provider.count = bind(rawCounter, null, data, startIndex, dimsDef);
  120. }
  121. };
  122. var getItemForTypedArray = function (idx, out) {
  123. idx = idx - this._offset;
  124. out = out || [];
  125. var data = this._data;
  126. var dimSize = this._dimSize;
  127. var offset = dimSize * idx;
  128. for (var i = 0; i < dimSize; i++) {
  129. out[i] = data[offset + i];
  130. }
  131. return out;
  132. };
  133. var fillStorageForTypedArray = function (start, end, storage, extent) {
  134. var data = this._data;
  135. var dimSize = this._dimSize;
  136. for (var dim = 0; dim < dimSize; dim++) {
  137. var dimExtent = extent[dim];
  138. var min = dimExtent[0] == null ? Infinity : dimExtent[0];
  139. var max = dimExtent[1] == null ? -Infinity : dimExtent[1];
  140. var count = end - start;
  141. var arr = storage[dim];
  142. for (var i = 0; i < count; i++) {
  143. // appendData with TypedArray will always do replace in provider.
  144. var val = data[i * dimSize + dim];
  145. arr[start + i] = val;
  146. val < min && (min = val);
  147. val > max && (max = val);
  148. }
  149. dimExtent[0] = min;
  150. dimExtent[1] = max;
  151. }
  152. };
  153. var countForTypedArray = function () {
  154. return this._data ? this._data.length / this._dimSize : 0;
  155. };
  156. providerMethods = (_a = {}, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_COLUMN] = {
  157. pure: true,
  158. appendData: appendDataSimply
  159. }, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_ROW] = {
  160. pure: true,
  161. appendData: function () {
  162. throw new Error('Do not support appendData when set seriesLayoutBy: "row".');
  163. }
  164. }, _a[SOURCE_FORMAT_OBJECT_ROWS] = {
  165. pure: true,
  166. appendData: appendDataSimply
  167. }, _a[SOURCE_FORMAT_KEYED_COLUMNS] = {
  168. pure: true,
  169. appendData: function (newData) {
  170. var data = this._data;
  171. each(newData, function (newCol, key) {
  172. var oldCol = data[key] || (data[key] = []);
  173. for (var i = 0; i < (newCol || []).length; i++) {
  174. oldCol.push(newCol[i]);
  175. }
  176. });
  177. }
  178. }, _a[SOURCE_FORMAT_ORIGINAL] = {
  179. appendData: appendDataSimply
  180. }, _a[SOURCE_FORMAT_TYPED_ARRAY] = {
  181. persistent: false,
  182. pure: true,
  183. appendData: function (newData) {
  184. if (process.env.NODE_ENV !== 'production') {
  185. assert(isTypedArray(newData), 'Added data must be TypedArray if data in initialization is TypedArray');
  186. }
  187. this._data = newData;
  188. },
  189. // Clean self if data is already used.
  190. clean: function () {
  191. // PENDING
  192. this._offset += this.count();
  193. this._data = null;
  194. }
  195. }, _a);
  196. function appendDataSimply(newData) {
  197. for (var i = 0; i < newData.length; i++) {
  198. this._data.push(newData[i]);
  199. }
  200. }
  201. }();
  202. return DefaultDataProvider;
  203. }();
  204. export { DefaultDataProvider };
  205. var validateSimply = function (rawData) {
  206. if (!isArray(rawData)) {
  207. error('series.data or dataset.source must be an array.');
  208. }
  209. };
  210. /**
  211. * Only run in dev mode - hint users for debug.
  212. */
  213. var rawSourceDataValidatorMap = (_a = {}, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_COLUMN] = validateSimply, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_ROW] = validateSimply, _a[SOURCE_FORMAT_OBJECT_ROWS] = validateSimply, _a[SOURCE_FORMAT_KEYED_COLUMNS] = function (rawData, dimsDef) {
  214. for (var i = 0; i < dimsDef.length; i++) {
  215. var dimName = dimsDef[i].name;
  216. if (dimName == null) {
  217. error('dimension name must not be null/undefined.');
  218. }
  219. }
  220. }, _a[SOURCE_FORMAT_ORIGINAL] = validateSimply, _a);
  221. var getItemSimply = function (rawData, startIndex, dimsDef, idx) {
  222. return rawData[idx];
  223. };
  224. var rawSourceItemGetterMap = (_b = {}, _b[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_COLUMN] = function (rawData, startIndex, dimsDef, idx) {
  225. return rawData[idx + startIndex];
  226. }, _b[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_ROW] = function (rawData, startIndex, dimsDef, idx, out) {
  227. idx += startIndex;
  228. var item = out || [];
  229. var data = rawData;
  230. for (var i = 0; i < data.length; i++) {
  231. var row = data[i];
  232. item[i] = row ? row[idx] : null;
  233. }
  234. return item;
  235. }, _b[SOURCE_FORMAT_OBJECT_ROWS] = getItemSimply, _b[SOURCE_FORMAT_KEYED_COLUMNS] = function (rawData, startIndex, dimsDef, idx, out) {
  236. var item = out || [];
  237. for (var i = 0; i < dimsDef.length; i++) {
  238. var dimName = dimsDef[i].name;
  239. var col = dimName != null ? rawData[dimName] : null;
  240. item[i] = col ? col[idx] : null;
  241. }
  242. return item;
  243. }, _b[SOURCE_FORMAT_ORIGINAL] = getItemSimply, _b);
  244. export function getRawSourceItemGetter(sourceFormat, seriesLayoutBy) {
  245. var method = rawSourceItemGetterMap[getMethodMapKey(sourceFormat, seriesLayoutBy)];
  246. if (process.env.NODE_ENV !== 'production') {
  247. assert(method, 'Do not support get item on "' + sourceFormat + '", "' + seriesLayoutBy + '".');
  248. }
  249. return method;
  250. }
  251. var countSimply = function (rawData, startIndex, dimsDef) {
  252. return rawData.length;
  253. };
  254. var rawSourceDataCounterMap = (_c = {}, _c[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_COLUMN] = function (rawData, startIndex, dimsDef) {
  255. return Math.max(0, rawData.length - startIndex);
  256. }, _c[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_ROW] = function (rawData, startIndex, dimsDef) {
  257. var row = rawData[0];
  258. return row ? Math.max(0, row.length - startIndex) : 0;
  259. }, _c[SOURCE_FORMAT_OBJECT_ROWS] = countSimply, _c[SOURCE_FORMAT_KEYED_COLUMNS] = function (rawData, startIndex, dimsDef) {
  260. var dimName = dimsDef[0].name;
  261. var col = dimName != null ? rawData[dimName] : null;
  262. return col ? col.length : 0;
  263. }, _c[SOURCE_FORMAT_ORIGINAL] = countSimply, _c);
  264. export function getRawSourceDataCounter(sourceFormat, seriesLayoutBy) {
  265. var method = rawSourceDataCounterMap[getMethodMapKey(sourceFormat, seriesLayoutBy)];
  266. if (process.env.NODE_ENV !== 'production') {
  267. assert(method, 'Do not support count on "' + sourceFormat + '", "' + seriesLayoutBy + '".');
  268. }
  269. return method;
  270. }
  271. var getRawValueSimply = function (dataItem, dimIndex, property) {
  272. return dataItem[dimIndex];
  273. };
  274. var rawSourceValueGetterMap = (_d = {}, _d[SOURCE_FORMAT_ARRAY_ROWS] = getRawValueSimply, _d[SOURCE_FORMAT_OBJECT_ROWS] = function (dataItem, dimIndex, property) {
  275. return dataItem[property];
  276. }, _d[SOURCE_FORMAT_KEYED_COLUMNS] = getRawValueSimply, _d[SOURCE_FORMAT_ORIGINAL] = function (dataItem, dimIndex, property) {
  277. // FIXME: In some case (markpoint in geo (geo-map.html)),
  278. // dataItem is {coord: [...]}
  279. var value = getDataItemValue(dataItem);
  280. return !(value instanceof Array) ? value : value[dimIndex];
  281. }, _d[SOURCE_FORMAT_TYPED_ARRAY] = getRawValueSimply, _d);
  282. export function getRawSourceValueGetter(sourceFormat) {
  283. var method = rawSourceValueGetterMap[sourceFormat];
  284. if (process.env.NODE_ENV !== 'production') {
  285. assert(method, 'Do not support get value on "' + sourceFormat + '".');
  286. }
  287. return method;
  288. }
  289. function getMethodMapKey(sourceFormat, seriesLayoutBy) {
  290. return sourceFormat === SOURCE_FORMAT_ARRAY_ROWS ? sourceFormat + '_' + seriesLayoutBy : sourceFormat;
  291. }
  292. // ??? FIXME can these logic be more neat: getRawValue, getRawDataItem,
  293. // Consider persistent.
  294. // Caution: why use raw value to display on label or tooltip?
  295. // A reason is to avoid format. For example time value we do not know
  296. // how to format is expected. More over, if stack is used, calculated
  297. // value may be 0.91000000001, which have brings trouble to display.
  298. // TODO: consider how to treat null/undefined/NaN when display?
  299. export function retrieveRawValue(data, dataIndex,
  300. // If dimIndex is null/undefined, return OptionDataItem.
  301. // Otherwise, return OptionDataValue.
  302. dim) {
  303. if (!data) {
  304. return;
  305. }
  306. // Consider data may be not persistent.
  307. var dataItem = data.getRawDataItem(dataIndex);
  308. if (dataItem == null) {
  309. return;
  310. }
  311. var store = data.getStore();
  312. var sourceFormat = store.getSource().sourceFormat;
  313. if (dim != null) {
  314. var dimIndex = data.getDimensionIndex(dim);
  315. var property = store.getDimensionProperty(dimIndex);
  316. return getRawSourceValueGetter(sourceFormat)(dataItem, dimIndex, property);
  317. } else {
  318. var result = dataItem;
  319. if (sourceFormat === SOURCE_FORMAT_ORIGINAL) {
  320. result = getDataItemValue(dataItem);
  321. }
  322. return result;
  323. }
  324. }
  325. /**
  326. * Compatible with some cases (in pie, map) like:
  327. * data: [{name: 'xx', value: 5, selected: true}, ...]
  328. * where only sourceFormat is 'original' and 'objectRows' supported.
  329. *
  330. * // TODO
  331. * Supported detail options in data item when using 'arrayRows'.
  332. *
  333. * @param data
  334. * @param dataIndex
  335. * @param attr like 'selected'
  336. */
  337. export function retrieveRawAttr(data, dataIndex, attr) {
  338. if (!data) {
  339. return;
  340. }
  341. var sourceFormat = data.getStore().getSource().sourceFormat;
  342. if (sourceFormat !== SOURCE_FORMAT_ORIGINAL && sourceFormat !== SOURCE_FORMAT_OBJECT_ROWS) {
  343. return;
  344. }
  345. var dataItem = data.getRawDataItem(dataIndex);
  346. if (sourceFormat === SOURCE_FORMAT_ORIGINAL && !isObject(dataItem)) {
  347. dataItem = null;
  348. }
  349. if (dataItem) {
  350. return dataItem[attr];
  351. }
  352. }