sourceHelper.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. import { makeInner, getDataItemValue, queryReferringComponents, SINGLE_REFERRING } from '../../util/model.js';
  41. import { createHashMap, each, isArray, isString, isObject, isTypedArray } from 'zrender/lib/core/util.js';
  42. import { SOURCE_FORMAT_ORIGINAL, SOURCE_FORMAT_ARRAY_ROWS, SOURCE_FORMAT_OBJECT_ROWS, SERIES_LAYOUT_BY_ROW, SOURCE_FORMAT_KEYED_COLUMNS } from '../../util/types.js'; // The result of `guessOrdinal`.
  43. export var BE_ORDINAL = {
  44. Must: 1,
  45. Might: 2,
  46. Not: 3 // Other cases
  47. };
  48. var innerGlobalModel = makeInner();
  49. /**
  50. * MUST be called before mergeOption of all series.
  51. */
  52. export function resetSourceDefaulter(ecModel) {
  53. // `datasetMap` is used to make default encode.
  54. innerGlobalModel(ecModel).datasetMap = createHashMap();
  55. }
  56. /**
  57. * [The strategy of the arrengment of data dimensions for dataset]:
  58. * "value way": all axes are non-category axes. So series one by one take
  59. * several (the number is coordSysDims.length) dimensions from dataset.
  60. * The result of data arrengment of data dimensions like:
  61. * | ser0_x | ser0_y | ser1_x | ser1_y | ser2_x | ser2_y |
  62. * "category way": at least one axis is category axis. So the the first data
  63. * dimension is always mapped to the first category axis and shared by
  64. * all of the series. The other data dimensions are taken by series like
  65. * "value way" does.
  66. * The result of data arrengment of data dimensions like:
  67. * | ser_shared_x | ser0_y | ser1_y | ser2_y |
  68. *
  69. * @return encode Never be `null/undefined`.
  70. */
  71. export function makeSeriesEncodeForAxisCoordSys(coordDimensions, seriesModel, source) {
  72. var encode = {};
  73. var datasetModel = querySeriesUpstreamDatasetModel(seriesModel); // Currently only make default when using dataset, util more reqirements occur.
  74. if (!datasetModel || !coordDimensions) {
  75. return encode;
  76. }
  77. var encodeItemName = [];
  78. var encodeSeriesName = [];
  79. var ecModel = seriesModel.ecModel;
  80. var datasetMap = innerGlobalModel(ecModel).datasetMap;
  81. var key = datasetModel.uid + '_' + source.seriesLayoutBy;
  82. var baseCategoryDimIndex;
  83. var categoryWayValueDimStart;
  84. coordDimensions = coordDimensions.slice();
  85. each(coordDimensions, function (coordDimInfoLoose, coordDimIdx) {
  86. var coordDimInfo = isObject(coordDimInfoLoose) ? coordDimInfoLoose : coordDimensions[coordDimIdx] = {
  87. name: coordDimInfoLoose
  88. };
  89. if (coordDimInfo.type === 'ordinal' && baseCategoryDimIndex == null) {
  90. baseCategoryDimIndex = coordDimIdx;
  91. categoryWayValueDimStart = getDataDimCountOnCoordDim(coordDimInfo);
  92. }
  93. encode[coordDimInfo.name] = [];
  94. });
  95. var datasetRecord = datasetMap.get(key) || datasetMap.set(key, {
  96. categoryWayDim: categoryWayValueDimStart,
  97. valueWayDim: 0
  98. }); // TODO
  99. // Auto detect first time axis and do arrangement.
  100. each(coordDimensions, function (coordDimInfo, coordDimIdx) {
  101. var coordDimName = coordDimInfo.name;
  102. var count = getDataDimCountOnCoordDim(coordDimInfo); // In value way.
  103. if (baseCategoryDimIndex == null) {
  104. var start = datasetRecord.valueWayDim;
  105. pushDim(encode[coordDimName], start, count);
  106. pushDim(encodeSeriesName, start, count);
  107. datasetRecord.valueWayDim += count; // ??? TODO give a better default series name rule?
  108. // especially when encode x y specified.
  109. // consider: when multiple series share one dimension
  110. // category axis, series name should better use
  111. // the other dimension name. On the other hand, use
  112. // both dimensions name.
  113. } // In category way, the first category axis.
  114. else if (baseCategoryDimIndex === coordDimIdx) {
  115. pushDim(encode[coordDimName], 0, count);
  116. pushDim(encodeItemName, 0, count);
  117. } // In category way, the other axis.
  118. else {
  119. var start = datasetRecord.categoryWayDim;
  120. pushDim(encode[coordDimName], start, count);
  121. pushDim(encodeSeriesName, start, count);
  122. datasetRecord.categoryWayDim += count;
  123. }
  124. });
  125. function pushDim(dimIdxArr, idxFrom, idxCount) {
  126. for (var i = 0; i < idxCount; i++) {
  127. dimIdxArr.push(idxFrom + i);
  128. }
  129. }
  130. function getDataDimCountOnCoordDim(coordDimInfo) {
  131. var dimsDef = coordDimInfo.dimsDef;
  132. return dimsDef ? dimsDef.length : 1;
  133. }
  134. encodeItemName.length && (encode.itemName = encodeItemName);
  135. encodeSeriesName.length && (encode.seriesName = encodeSeriesName);
  136. return encode;
  137. }
  138. /**
  139. * Work for data like [{name: ..., value: ...}, ...].
  140. *
  141. * @return encode Never be `null/undefined`.
  142. */
  143. export function makeSeriesEncodeForNameBased(seriesModel, source, dimCount) {
  144. var encode = {};
  145. var datasetModel = querySeriesUpstreamDatasetModel(seriesModel); // Currently only make default when using dataset, util more reqirements occur.
  146. if (!datasetModel) {
  147. return encode;
  148. }
  149. var sourceFormat = source.sourceFormat;
  150. var dimensionsDefine = source.dimensionsDefine;
  151. var potentialNameDimIndex;
  152. if (sourceFormat === SOURCE_FORMAT_OBJECT_ROWS || sourceFormat === SOURCE_FORMAT_KEYED_COLUMNS) {
  153. each(dimensionsDefine, function (dim, idx) {
  154. if ((isObject(dim) ? dim.name : dim) === 'name') {
  155. potentialNameDimIndex = idx;
  156. }
  157. });
  158. }
  159. var idxResult = function () {
  160. var idxRes0 = {};
  161. var idxRes1 = {};
  162. var guessRecords = []; // 5 is an experience value.
  163. for (var i = 0, len = Math.min(5, dimCount); i < len; i++) {
  164. var guessResult = doGuessOrdinal(source.data, sourceFormat, source.seriesLayoutBy, dimensionsDefine, source.startIndex, i);
  165. guessRecords.push(guessResult);
  166. var isPureNumber = guessResult === BE_ORDINAL.Not; // [Strategy of idxRes0]: find the first BE_ORDINAL.Not as the value dim,
  167. // and then find a name dim with the priority:
  168. // "BE_ORDINAL.Might|BE_ORDINAL.Must" > "other dim" > "the value dim itself".
  169. if (isPureNumber && idxRes0.v == null && i !== potentialNameDimIndex) {
  170. idxRes0.v = i;
  171. }
  172. if (idxRes0.n == null || idxRes0.n === idxRes0.v || !isPureNumber && guessRecords[idxRes0.n] === BE_ORDINAL.Not) {
  173. idxRes0.n = i;
  174. }
  175. if (fulfilled(idxRes0) && guessRecords[idxRes0.n] !== BE_ORDINAL.Not) {
  176. return idxRes0;
  177. } // [Strategy of idxRes1]: if idxRes0 not satisfied (that is, no BE_ORDINAL.Not),
  178. // find the first BE_ORDINAL.Might as the value dim,
  179. // and then find a name dim with the priority:
  180. // "other dim" > "the value dim itself".
  181. // That is for backward compat: number-like (e.g., `'3'`, `'55'`) can be
  182. // treated as number.
  183. if (!isPureNumber) {
  184. if (guessResult === BE_ORDINAL.Might && idxRes1.v == null && i !== potentialNameDimIndex) {
  185. idxRes1.v = i;
  186. }
  187. if (idxRes1.n == null || idxRes1.n === idxRes1.v) {
  188. idxRes1.n = i;
  189. }
  190. }
  191. }
  192. function fulfilled(idxResult) {
  193. return idxResult.v != null && idxResult.n != null;
  194. }
  195. return fulfilled(idxRes0) ? idxRes0 : fulfilled(idxRes1) ? idxRes1 : null;
  196. }();
  197. if (idxResult) {
  198. encode.value = [idxResult.v]; // `potentialNameDimIndex` has highest priority.
  199. var nameDimIndex = potentialNameDimIndex != null ? potentialNameDimIndex : idxResult.n; // By default, label uses itemName in charts.
  200. // So we don't set encodeLabel here.
  201. encode.itemName = [nameDimIndex];
  202. encode.seriesName = [nameDimIndex];
  203. }
  204. return encode;
  205. }
  206. /**
  207. * @return If return null/undefined, indicate that should not use datasetModel.
  208. */
  209. export function querySeriesUpstreamDatasetModel(seriesModel) {
  210. // Caution: consider the scenario:
  211. // A dataset is declared and a series is not expected to use the dataset,
  212. // and at the beginning `setOption({series: { noData })` (just prepare other
  213. // option but no data), then `setOption({series: {data: [...]}); In this case,
  214. // the user should set an empty array to avoid that dataset is used by default.
  215. var thisData = seriesModel.get('data', true);
  216. if (!thisData) {
  217. return queryReferringComponents(seriesModel.ecModel, 'dataset', {
  218. index: seriesModel.get('datasetIndex', true),
  219. id: seriesModel.get('datasetId', true)
  220. }, SINGLE_REFERRING).models[0];
  221. }
  222. }
  223. /**
  224. * @return Always return an array event empty.
  225. */
  226. export function queryDatasetUpstreamDatasetModels(datasetModel) {
  227. // Only these attributes declared, we by defualt reference to `datasetIndex: 0`.
  228. // Otherwise, no reference.
  229. if (!datasetModel.get('transform', true) && !datasetModel.get('fromTransformResult', true)) {
  230. return [];
  231. }
  232. return queryReferringComponents(datasetModel.ecModel, 'dataset', {
  233. index: datasetModel.get('fromDatasetIndex', true),
  234. id: datasetModel.get('fromDatasetId', true)
  235. }, SINGLE_REFERRING).models;
  236. }
  237. /**
  238. * The rule should not be complex, otherwise user might not
  239. * be able to known where the data is wrong.
  240. * The code is ugly, but how to make it neat?
  241. */
  242. export function guessOrdinal(source, dimIndex) {
  243. return doGuessOrdinal(source.data, source.sourceFormat, source.seriesLayoutBy, source.dimensionsDefine, source.startIndex, dimIndex);
  244. } // dimIndex may be overflow source data.
  245. // return {BE_ORDINAL}
  246. function doGuessOrdinal(data, sourceFormat, seriesLayoutBy, dimensionsDefine, startIndex, dimIndex) {
  247. var result; // Experience value.
  248. var maxLoop = 5;
  249. if (isTypedArray(data)) {
  250. return BE_ORDINAL.Not;
  251. } // When sourceType is 'objectRows' or 'keyedColumns', dimensionsDefine
  252. // always exists in source.
  253. var dimName;
  254. var dimType;
  255. if (dimensionsDefine) {
  256. var dimDefItem = dimensionsDefine[dimIndex];
  257. if (isObject(dimDefItem)) {
  258. dimName = dimDefItem.name;
  259. dimType = dimDefItem.type;
  260. } else if (isString(dimDefItem)) {
  261. dimName = dimDefItem;
  262. }
  263. }
  264. if (dimType != null) {
  265. return dimType === 'ordinal' ? BE_ORDINAL.Must : BE_ORDINAL.Not;
  266. }
  267. if (sourceFormat === SOURCE_FORMAT_ARRAY_ROWS) {
  268. var dataArrayRows = data;
  269. if (seriesLayoutBy === SERIES_LAYOUT_BY_ROW) {
  270. var sample = dataArrayRows[dimIndex];
  271. for (var i = 0; i < (sample || []).length && i < maxLoop; i++) {
  272. if ((result = detectValue(sample[startIndex + i])) != null) {
  273. return result;
  274. }
  275. }
  276. } else {
  277. for (var i = 0; i < dataArrayRows.length && i < maxLoop; i++) {
  278. var row = dataArrayRows[startIndex + i];
  279. if (row && (result = detectValue(row[dimIndex])) != null) {
  280. return result;
  281. }
  282. }
  283. }
  284. } else if (sourceFormat === SOURCE_FORMAT_OBJECT_ROWS) {
  285. var dataObjectRows = data;
  286. if (!dimName) {
  287. return BE_ORDINAL.Not;
  288. }
  289. for (var i = 0; i < dataObjectRows.length && i < maxLoop; i++) {
  290. var item = dataObjectRows[i];
  291. if (item && (result = detectValue(item[dimName])) != null) {
  292. return result;
  293. }
  294. }
  295. } else if (sourceFormat === SOURCE_FORMAT_KEYED_COLUMNS) {
  296. var dataKeyedColumns = data;
  297. if (!dimName) {
  298. return BE_ORDINAL.Not;
  299. }
  300. var sample = dataKeyedColumns[dimName];
  301. if (!sample || isTypedArray(sample)) {
  302. return BE_ORDINAL.Not;
  303. }
  304. for (var i = 0; i < sample.length && i < maxLoop; i++) {
  305. if ((result = detectValue(sample[i])) != null) {
  306. return result;
  307. }
  308. }
  309. } else if (sourceFormat === SOURCE_FORMAT_ORIGINAL) {
  310. var dataOriginal = data;
  311. for (var i = 0; i < dataOriginal.length && i < maxLoop; i++) {
  312. var item = dataOriginal[i];
  313. var val = getDataItemValue(item);
  314. if (!isArray(val)) {
  315. return BE_ORDINAL.Not;
  316. }
  317. if ((result = detectValue(val[dimIndex])) != null) {
  318. return result;
  319. }
  320. }
  321. }
  322. function detectValue(val) {
  323. var beStr = isString(val); // Consider usage convenience, '1', '2' will be treated as "number".
  324. // `isFinit('')` get `true`.
  325. if (val != null && isFinite(val) && val !== '') {
  326. return beStr ? BE_ORDINAL.Might : BE_ORDINAL.Not;
  327. } else if (beStr && val !== '-') {
  328. return BE_ORDINAL.Must;
  329. }
  330. }
  331. return BE_ORDINAL.Not;
  332. }