dataProvider.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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;
  41. // TODO
  42. // ??? refactor? check the outer usage of data provider.
  43. // merge with defaultDimValueGetter?
  44. import { isTypedArray, extend, assert, each, isObject, bind } 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. var providerMethods;
  49. var mountMethods;
  50. /**
  51. * If normal array used, mutable chunk size is supported.
  52. * If typed array used, chunk size must be fixed.
  53. */
  54. var DefaultDataProvider = /** @class */function () {
  55. function DefaultDataProvider(sourceParam, dimSize) {
  56. // let source: Source;
  57. var source = !isSourceInstance(sourceParam) ? createSourceFromSeriesDataOption(sourceParam) : sourceParam;
  58. // declare source is Source;
  59. this._source = source;
  60. var data = this._data = source.data;
  61. // Typed array. TODO IE10+?
  62. if (source.sourceFormat === SOURCE_FORMAT_TYPED_ARRAY) {
  63. if (process.env.NODE_ENV !== 'production') {
  64. if (dimSize == null) {
  65. throw new Error('Typed array data must specify dimension size');
  66. }
  67. }
  68. this._offset = 0;
  69. this._dimSize = dimSize;
  70. this._data = data;
  71. }
  72. mountMethods(this, data, source);
  73. }
  74. DefaultDataProvider.prototype.getSource = function () {
  75. return this._source;
  76. };
  77. DefaultDataProvider.prototype.count = function () {
  78. return 0;
  79. };
  80. DefaultDataProvider.prototype.getItem = function (idx, out) {
  81. return;
  82. };
  83. DefaultDataProvider.prototype.appendData = function (newData) {};
  84. DefaultDataProvider.prototype.clean = function () {};
  85. DefaultDataProvider.protoInitialize = function () {
  86. // PENDING: To avoid potential incompat (e.g., prototype
  87. // is visited somewhere), still init them on prototype.
  88. var proto = DefaultDataProvider.prototype;
  89. proto.pure = false;
  90. proto.persistent = true;
  91. }();
  92. DefaultDataProvider.internalField = function () {
  93. var _a;
  94. mountMethods = function (provider, data, source) {
  95. var sourceFormat = source.sourceFormat;
  96. var seriesLayoutBy = source.seriesLayoutBy;
  97. var startIndex = source.startIndex;
  98. var dimsDef = source.dimensionsDefine;
  99. var methods = providerMethods[getMethodMapKey(sourceFormat, seriesLayoutBy)];
  100. if (process.env.NODE_ENV !== 'production') {
  101. assert(methods, 'Invalide sourceFormat: ' + sourceFormat);
  102. }
  103. extend(provider, methods);
  104. if (sourceFormat === SOURCE_FORMAT_TYPED_ARRAY) {
  105. provider.getItem = getItemForTypedArray;
  106. provider.count = countForTypedArray;
  107. provider.fillStorage = fillStorageForTypedArray;
  108. } else {
  109. var rawItemGetter = getRawSourceItemGetter(sourceFormat, seriesLayoutBy);
  110. provider.getItem = bind(rawItemGetter, null, data, startIndex, dimsDef);
  111. var rawCounter = getRawSourceDataCounter(sourceFormat, seriesLayoutBy);
  112. provider.count = bind(rawCounter, null, data, startIndex, dimsDef);
  113. }
  114. };
  115. var getItemForTypedArray = function (idx, out) {
  116. idx = idx - this._offset;
  117. out = out || [];
  118. var data = this._data;
  119. var dimSize = this._dimSize;
  120. var offset = dimSize * idx;
  121. for (var i = 0; i < dimSize; i++) {
  122. out[i] = data[offset + i];
  123. }
  124. return out;
  125. };
  126. var fillStorageForTypedArray = function (start, end, storage, extent) {
  127. var data = this._data;
  128. var dimSize = this._dimSize;
  129. for (var dim = 0; dim < dimSize; dim++) {
  130. var dimExtent = extent[dim];
  131. var min = dimExtent[0] == null ? Infinity : dimExtent[0];
  132. var max = dimExtent[1] == null ? -Infinity : dimExtent[1];
  133. var count = end - start;
  134. var arr = storage[dim];
  135. for (var i = 0; i < count; i++) {
  136. // appendData with TypedArray will always do replace in provider.
  137. var val = data[i * dimSize + dim];
  138. arr[start + i] = val;
  139. val < min && (min = val);
  140. val > max && (max = val);
  141. }
  142. dimExtent[0] = min;
  143. dimExtent[1] = max;
  144. }
  145. };
  146. var countForTypedArray = function () {
  147. return this._data ? this._data.length / this._dimSize : 0;
  148. };
  149. providerMethods = (_a = {}, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_COLUMN] = {
  150. pure: true,
  151. appendData: appendDataSimply
  152. }, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_ROW] = {
  153. pure: true,
  154. appendData: function () {
  155. throw new Error('Do not support appendData when set seriesLayoutBy: "row".');
  156. }
  157. }, _a[SOURCE_FORMAT_OBJECT_ROWS] = {
  158. pure: true,
  159. appendData: appendDataSimply
  160. }, _a[SOURCE_FORMAT_KEYED_COLUMNS] = {
  161. pure: true,
  162. appendData: function (newData) {
  163. var data = this._data;
  164. each(newData, function (newCol, key) {
  165. var oldCol = data[key] || (data[key] = []);
  166. for (var i = 0; i < (newCol || []).length; i++) {
  167. oldCol.push(newCol[i]);
  168. }
  169. });
  170. }
  171. }, _a[SOURCE_FORMAT_ORIGINAL] = {
  172. appendData: appendDataSimply
  173. }, _a[SOURCE_FORMAT_TYPED_ARRAY] = {
  174. persistent: false,
  175. pure: true,
  176. appendData: function (newData) {
  177. if (process.env.NODE_ENV !== 'production') {
  178. assert(isTypedArray(newData), 'Added data must be TypedArray if data in initialization is TypedArray');
  179. }
  180. this._data = newData;
  181. },
  182. // Clean self if data is already used.
  183. clean: function () {
  184. // PENDING
  185. this._offset += this.count();
  186. this._data = null;
  187. }
  188. }, _a);
  189. function appendDataSimply(newData) {
  190. for (var i = 0; i < newData.length; i++) {
  191. this._data.push(newData[i]);
  192. }
  193. }
  194. }();
  195. return DefaultDataProvider;
  196. }();
  197. export { DefaultDataProvider };
  198. var getItemSimply = function (rawData, startIndex, dimsDef, idx) {
  199. return rawData[idx];
  200. };
  201. var rawSourceItemGetterMap = (_a = {}, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_COLUMN] = function (rawData, startIndex, dimsDef, idx) {
  202. return rawData[idx + startIndex];
  203. }, _a[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_ROW] = function (rawData, startIndex, dimsDef, idx, out) {
  204. idx += startIndex;
  205. var item = out || [];
  206. var data = rawData;
  207. for (var i = 0; i < data.length; i++) {
  208. var row = data[i];
  209. item[i] = row ? row[idx] : null;
  210. }
  211. return item;
  212. }, _a[SOURCE_FORMAT_OBJECT_ROWS] = getItemSimply, _a[SOURCE_FORMAT_KEYED_COLUMNS] = function (rawData, startIndex, dimsDef, idx, out) {
  213. var item = out || [];
  214. for (var i = 0; i < dimsDef.length; i++) {
  215. var dimName = dimsDef[i].name;
  216. if (process.env.NODE_ENV !== 'production') {
  217. if (dimName == null) {
  218. throw new Error();
  219. }
  220. }
  221. var col = rawData[dimName];
  222. item[i] = col ? col[idx] : null;
  223. }
  224. return item;
  225. }, _a[SOURCE_FORMAT_ORIGINAL] = getItemSimply, _a);
  226. export function getRawSourceItemGetter(sourceFormat, seriesLayoutBy) {
  227. var method = rawSourceItemGetterMap[getMethodMapKey(sourceFormat, seriesLayoutBy)];
  228. if (process.env.NODE_ENV !== 'production') {
  229. assert(method, 'Do not support get item on "' + sourceFormat + '", "' + seriesLayoutBy + '".');
  230. }
  231. return method;
  232. }
  233. var countSimply = function (rawData, startIndex, dimsDef) {
  234. return rawData.length;
  235. };
  236. var rawSourceDataCounterMap = (_b = {}, _b[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_COLUMN] = function (rawData, startIndex, dimsDef) {
  237. return Math.max(0, rawData.length - startIndex);
  238. }, _b[SOURCE_FORMAT_ARRAY_ROWS + '_' + SERIES_LAYOUT_BY_ROW] = function (rawData, startIndex, dimsDef) {
  239. var row = rawData[0];
  240. return row ? Math.max(0, row.length - startIndex) : 0;
  241. }, _b[SOURCE_FORMAT_OBJECT_ROWS] = countSimply, _b[SOURCE_FORMAT_KEYED_COLUMNS] = function (rawData, startIndex, dimsDef) {
  242. var dimName = dimsDef[0].name;
  243. if (process.env.NODE_ENV !== 'production') {
  244. if (dimName == null) {
  245. throw new Error();
  246. }
  247. }
  248. var col = rawData[dimName];
  249. return col ? col.length : 0;
  250. }, _b[SOURCE_FORMAT_ORIGINAL] = countSimply, _b);
  251. export function getRawSourceDataCounter(sourceFormat, seriesLayoutBy) {
  252. var method = rawSourceDataCounterMap[getMethodMapKey(sourceFormat, seriesLayoutBy)];
  253. if (process.env.NODE_ENV !== 'production') {
  254. assert(method, 'Do not support count on "' + sourceFormat + '", "' + seriesLayoutBy + '".');
  255. }
  256. return method;
  257. }
  258. var getRawValueSimply = function (dataItem, dimIndex, property) {
  259. return dataItem[dimIndex];
  260. };
  261. var rawSourceValueGetterMap = (_c = {}, _c[SOURCE_FORMAT_ARRAY_ROWS] = getRawValueSimply, _c[SOURCE_FORMAT_OBJECT_ROWS] = function (dataItem, dimIndex, property) {
  262. return dataItem[property];
  263. }, _c[SOURCE_FORMAT_KEYED_COLUMNS] = getRawValueSimply, _c[SOURCE_FORMAT_ORIGINAL] = function (dataItem, dimIndex, property) {
  264. // FIXME: In some case (markpoint in geo (geo-map.html)),
  265. // dataItem is {coord: [...]}
  266. var value = getDataItemValue(dataItem);
  267. return !(value instanceof Array) ? value : value[dimIndex];
  268. }, _c[SOURCE_FORMAT_TYPED_ARRAY] = getRawValueSimply, _c);
  269. export function getRawSourceValueGetter(sourceFormat) {
  270. var method = rawSourceValueGetterMap[sourceFormat];
  271. if (process.env.NODE_ENV !== 'production') {
  272. assert(method, 'Do not support get value on "' + sourceFormat + '".');
  273. }
  274. return method;
  275. }
  276. function getMethodMapKey(sourceFormat, seriesLayoutBy) {
  277. return sourceFormat === SOURCE_FORMAT_ARRAY_ROWS ? sourceFormat + '_' + seriesLayoutBy : sourceFormat;
  278. }
  279. // ??? FIXME can these logic be more neat: getRawValue, getRawDataItem,
  280. // Consider persistent.
  281. // Caution: why use raw value to display on label or tooltip?
  282. // A reason is to avoid format. For example time value we do not know
  283. // how to format is expected. More over, if stack is used, calculated
  284. // value may be 0.91000000001, which have brings trouble to display.
  285. // TODO: consider how to treat null/undefined/NaN when display?
  286. export function retrieveRawValue(data, dataIndex,
  287. // If dimIndex is null/undefined, return OptionDataItem.
  288. // Otherwise, return OptionDataValue.
  289. dim) {
  290. if (!data) {
  291. return;
  292. }
  293. // Consider data may be not persistent.
  294. var dataItem = data.getRawDataItem(dataIndex);
  295. if (dataItem == null) {
  296. return;
  297. }
  298. var store = data.getStore();
  299. var sourceFormat = store.getSource().sourceFormat;
  300. if (dim != null) {
  301. var dimIndex = data.getDimensionIndex(dim);
  302. var property = store.getDimensionProperty(dimIndex);
  303. return getRawSourceValueGetter(sourceFormat)(dataItem, dimIndex, property);
  304. } else {
  305. var result = dataItem;
  306. if (sourceFormat === SOURCE_FORMAT_ORIGINAL) {
  307. result = getDataItemValue(dataItem);
  308. }
  309. return result;
  310. }
  311. }
  312. /**
  313. * Compatible with some cases (in pie, map) like:
  314. * data: [{name: 'xx', value: 5, selected: true}, ...]
  315. * where only sourceFormat is 'original' and 'objectRows' supported.
  316. *
  317. * // TODO
  318. * Supported detail options in data item when using 'arrayRows'.
  319. *
  320. * @param data
  321. * @param dataIndex
  322. * @param attr like 'selected'
  323. */
  324. export function retrieveRawAttr(data, dataIndex, attr) {
  325. if (!data) {
  326. return;
  327. }
  328. var sourceFormat = data.getStore().getSource().sourceFormat;
  329. if (sourceFormat !== SOURCE_FORMAT_ORIGINAL && sourceFormat !== SOURCE_FORMAT_OBJECT_ROWS) {
  330. return;
  331. }
  332. var dataItem = data.getRawDataItem(dataIndex);
  333. if (sourceFormat === SOURCE_FORMAT_ORIGINAL && !isObject(dataItem)) {
  334. dataItem = null;
  335. }
  336. if (dataItem) {
  337. return dataItem[attr];
  338. }
  339. }