sourceHelper.js 13 KB

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