dataProvider.js 13 KB

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