sourceHelper.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. var _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var _model = require("../../util/model");
  22. var makeInner = _model.makeInner;
  23. var getDataItemValue = _model.getDataItemValue;
  24. var _util = require("zrender/lib/core/util");
  25. var createHashMap = _util.createHashMap;
  26. var each = _util.each;
  27. var map = _util.map;
  28. var isArray = _util.isArray;
  29. var isString = _util.isString;
  30. var isObject = _util.isObject;
  31. var isTypedArray = _util.isTypedArray;
  32. var isArrayLike = _util.isArrayLike;
  33. var extend = _util.extend;
  34. var assert = _util.assert;
  35. var Source = require("../Source");
  36. var _sourceType = require("./sourceType");
  37. var SOURCE_FORMAT_ORIGINAL = _sourceType.SOURCE_FORMAT_ORIGINAL;
  38. var SOURCE_FORMAT_ARRAY_ROWS = _sourceType.SOURCE_FORMAT_ARRAY_ROWS;
  39. var SOURCE_FORMAT_OBJECT_ROWS = _sourceType.SOURCE_FORMAT_OBJECT_ROWS;
  40. var SOURCE_FORMAT_KEYED_COLUMNS = _sourceType.SOURCE_FORMAT_KEYED_COLUMNS;
  41. var SOURCE_FORMAT_UNKNOWN = _sourceType.SOURCE_FORMAT_UNKNOWN;
  42. var SOURCE_FORMAT_TYPED_ARRAY = _sourceType.SOURCE_FORMAT_TYPED_ARRAY;
  43. var SERIES_LAYOUT_BY_ROW = _sourceType.SERIES_LAYOUT_BY_ROW;
  44. /*
  45. * Licensed to the Apache Software Foundation (ASF) under one
  46. * or more contributor license agreements. See the NOTICE file
  47. * distributed with this work for additional information
  48. * regarding copyright ownership. The ASF licenses this file
  49. * to you under the Apache License, Version 2.0 (the
  50. * "License"); you may not use this file except in compliance
  51. * with the License. You may obtain a copy of the License at
  52. *
  53. * http://www.apache.org/licenses/LICENSE-2.0
  54. *
  55. * Unless required by applicable law or agreed to in writing,
  56. * software distributed under the License is distributed on an
  57. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  58. * KIND, either express or implied. See the License for the
  59. * specific language governing permissions and limitations
  60. * under the License.
  61. */
  62. // The result of `guessOrdinal`.
  63. var BE_ORDINAL = {
  64. Must: 1,
  65. // Encounter string but not '-' and not number-like.
  66. Might: 2,
  67. // Encounter string but number-like.
  68. Not: 3 // Other cases
  69. };
  70. var inner = makeInner();
  71. /**
  72. * @see {module:echarts/data/Source}
  73. * @param {module:echarts/component/dataset/DatasetModel} datasetModel
  74. * @return {string} sourceFormat
  75. */
  76. function detectSourceFormat(datasetModel) {
  77. var data = datasetModel.option.source;
  78. var sourceFormat = SOURCE_FORMAT_UNKNOWN;
  79. if (isTypedArray(data)) {
  80. sourceFormat = SOURCE_FORMAT_TYPED_ARRAY;
  81. } else if (isArray(data)) {
  82. // FIXME Whether tolerate null in top level array?
  83. if (data.length === 0) {
  84. sourceFormat = SOURCE_FORMAT_ARRAY_ROWS;
  85. }
  86. for (var i = 0, len = data.length; i < len; i++) {
  87. var item = data[i];
  88. if (item == null) {
  89. continue;
  90. } else if (isArray(item)) {
  91. sourceFormat = SOURCE_FORMAT_ARRAY_ROWS;
  92. break;
  93. } else if (isObject(item)) {
  94. sourceFormat = SOURCE_FORMAT_OBJECT_ROWS;
  95. break;
  96. }
  97. }
  98. } else if (isObject(data)) {
  99. for (var key in data) {
  100. if (data.hasOwnProperty(key) && isArrayLike(data[key])) {
  101. sourceFormat = SOURCE_FORMAT_KEYED_COLUMNS;
  102. break;
  103. }
  104. }
  105. } else if (data != null) {
  106. throw new Error('Invalid data');
  107. }
  108. inner(datasetModel).sourceFormat = sourceFormat;
  109. }
  110. /**
  111. * [Scenarios]:
  112. * (1) Provide source data directly:
  113. * series: {
  114. * encode: {...},
  115. * dimensions: [...]
  116. * seriesLayoutBy: 'row',
  117. * data: [[...]]
  118. * }
  119. * (2) Refer to datasetModel.
  120. * series: [{
  121. * encode: {...}
  122. * // Ignore datasetIndex means `datasetIndex: 0`
  123. * // and the dimensions defination in dataset is used
  124. * }, {
  125. * encode: {...},
  126. * seriesLayoutBy: 'column',
  127. * datasetIndex: 1
  128. * }]
  129. *
  130. * Get data from series itself or datset.
  131. * @return {module:echarts/data/Source} source
  132. */
  133. function getSource(seriesModel) {
  134. return inner(seriesModel).source;
  135. }
  136. /**
  137. * MUST be called before mergeOption of all series.
  138. * @param {module:echarts/model/Global} ecModel
  139. */
  140. function resetSourceDefaulter(ecModel) {
  141. // `datasetMap` is used to make default encode.
  142. inner(ecModel).datasetMap = createHashMap();
  143. }
  144. /**
  145. * [Caution]:
  146. * MUST be called after series option merged and
  147. * before "series.getInitailData()" called.
  148. *
  149. * [The rule of making default encode]:
  150. * Category axis (if exists) alway map to the first dimension.
  151. * Each other axis occupies a subsequent dimension.
  152. *
  153. * [Why make default encode]:
  154. * Simplify the typing of encode in option, avoiding the case like that:
  155. * series: [{encode: {x: 0, y: 1}}, {encode: {x: 0, y: 2}}, {encode: {x: 0, y: 3}}],
  156. * where the "y" have to be manually typed as "1, 2, 3, ...".
  157. *
  158. * @param {module:echarts/model/Series} seriesModel
  159. */
  160. function prepareSource(seriesModel) {
  161. var seriesOption = seriesModel.option;
  162. var data = seriesOption.data;
  163. var sourceFormat = isTypedArray(data) ? SOURCE_FORMAT_TYPED_ARRAY : SOURCE_FORMAT_ORIGINAL;
  164. var fromDataset = false;
  165. var seriesLayoutBy = seriesOption.seriesLayoutBy;
  166. var sourceHeader = seriesOption.sourceHeader;
  167. var dimensionsDefine = seriesOption.dimensions;
  168. var datasetModel = getDatasetModel(seriesModel);
  169. if (datasetModel) {
  170. var datasetOption = datasetModel.option;
  171. data = datasetOption.source;
  172. sourceFormat = inner(datasetModel).sourceFormat;
  173. fromDataset = true; // These settings from series has higher priority.
  174. seriesLayoutBy = seriesLayoutBy || datasetOption.seriesLayoutBy;
  175. sourceHeader == null && (sourceHeader = datasetOption.sourceHeader);
  176. dimensionsDefine = dimensionsDefine || datasetOption.dimensions;
  177. }
  178. var completeResult = completeBySourceData(data, sourceFormat, seriesLayoutBy, sourceHeader, dimensionsDefine);
  179. inner(seriesModel).source = new Source({
  180. data: data,
  181. fromDataset: fromDataset,
  182. seriesLayoutBy: seriesLayoutBy,
  183. sourceFormat: sourceFormat,
  184. dimensionsDefine: completeResult.dimensionsDefine,
  185. startIndex: completeResult.startIndex,
  186. dimensionsDetectCount: completeResult.dimensionsDetectCount,
  187. // Note: dataset option does not have `encode`.
  188. encodeDefine: seriesOption.encode
  189. });
  190. } // return {startIndex, dimensionsDefine, dimensionsCount}
  191. function completeBySourceData(data, sourceFormat, seriesLayoutBy, sourceHeader, dimensionsDefine) {
  192. if (!data) {
  193. return {
  194. dimensionsDefine: normalizeDimensionsDefine(dimensionsDefine)
  195. };
  196. }
  197. var dimensionsDetectCount;
  198. var startIndex;
  199. if (sourceFormat === SOURCE_FORMAT_ARRAY_ROWS) {
  200. // Rule: Most of the first line are string: it is header.
  201. // Caution: consider a line with 5 string and 1 number,
  202. // it still can not be sure it is a head, because the
  203. // 5 string may be 5 values of category columns.
  204. if (sourceHeader === 'auto' || sourceHeader == null) {
  205. arrayRowsTravelFirst(function (val) {
  206. // '-' is regarded as null/undefined.
  207. if (val != null && val !== '-') {
  208. if (isString(val)) {
  209. startIndex == null && (startIndex = 1);
  210. } else {
  211. startIndex = 0;
  212. }
  213. } // 10 is an experience number, avoid long loop.
  214. }, seriesLayoutBy, data, 10);
  215. } else {
  216. startIndex = sourceHeader ? 1 : 0;
  217. }
  218. if (!dimensionsDefine && startIndex === 1) {
  219. dimensionsDefine = [];
  220. arrayRowsTravelFirst(function (val, index) {
  221. dimensionsDefine[index] = val != null ? val : '';
  222. }, seriesLayoutBy, data);
  223. }
  224. dimensionsDetectCount = dimensionsDefine ? dimensionsDefine.length : seriesLayoutBy === SERIES_LAYOUT_BY_ROW ? data.length : data[0] ? data[0].length : null;
  225. } else if (sourceFormat === SOURCE_FORMAT_OBJECT_ROWS) {
  226. if (!dimensionsDefine) {
  227. dimensionsDefine = objectRowsCollectDimensions(data);
  228. }
  229. } else if (sourceFormat === SOURCE_FORMAT_KEYED_COLUMNS) {
  230. if (!dimensionsDefine) {
  231. dimensionsDefine = [];
  232. each(data, function (colArr, key) {
  233. dimensionsDefine.push(key);
  234. });
  235. }
  236. } else if (sourceFormat === SOURCE_FORMAT_ORIGINAL) {
  237. var value0 = getDataItemValue(data[0]);
  238. dimensionsDetectCount = isArray(value0) && value0.length || 1;
  239. } else if (sourceFormat === SOURCE_FORMAT_TYPED_ARRAY) {}
  240. return {
  241. startIndex: startIndex,
  242. dimensionsDefine: normalizeDimensionsDefine(dimensionsDefine),
  243. dimensionsDetectCount: dimensionsDetectCount
  244. };
  245. } // Consider dimensions defined like ['A', 'price', 'B', 'price', 'C', 'price'],
  246. // which is reasonable. But dimension name is duplicated.
  247. // Returns undefined or an array contains only object without null/undefiend or string.
  248. function normalizeDimensionsDefine(dimensionsDefine) {
  249. if (!dimensionsDefine) {
  250. // The meaning of null/undefined is different from empty array.
  251. return;
  252. }
  253. var nameMap = createHashMap();
  254. return map(dimensionsDefine, function (item, index) {
  255. item = extend({}, isObject(item) ? item : {
  256. name: item
  257. }); // User can set null in dimensions.
  258. // We dont auto specify name, othewise a given name may
  259. // cause it be refered unexpectedly.
  260. if (item.name == null) {
  261. return item;
  262. } // Also consider number form like 2012.
  263. item.name += ''; // User may also specify displayName.
  264. // displayName will always exists except user not
  265. // specified or dim name is not specified or detected.
  266. // (A auto generated dim name will not be used as
  267. // displayName).
  268. if (item.displayName == null) {
  269. item.displayName = item.name;
  270. }
  271. var exist = nameMap.get(item.name);
  272. if (!exist) {
  273. nameMap.set(item.name, {
  274. count: 1
  275. });
  276. } else {
  277. item.name += '-' + exist.count++;
  278. }
  279. return item;
  280. });
  281. }
  282. function arrayRowsTravelFirst(cb, seriesLayoutBy, data, maxLoop) {
  283. maxLoop == null && (maxLoop = Infinity);
  284. if (seriesLayoutBy === SERIES_LAYOUT_BY_ROW) {
  285. for (var i = 0; i < data.length && i < maxLoop; i++) {
  286. cb(data[i] ? data[i][0] : null, i);
  287. }
  288. } else {
  289. var value0 = data[0] || [];
  290. for (var i = 0; i < value0.length && i < maxLoop; i++) {
  291. cb(value0[i], i);
  292. }
  293. }
  294. }
  295. function objectRowsCollectDimensions(data) {
  296. var firstIndex = 0;
  297. var obj;
  298. while (firstIndex < data.length && !(obj = data[firstIndex++])) {} // jshint ignore: line
  299. if (obj) {
  300. var dimensions = [];
  301. each(obj, function (value, key) {
  302. dimensions.push(key);
  303. });
  304. return dimensions;
  305. }
  306. }
  307. /**
  308. * [The strategy of the arrengment of data dimensions for dataset]:
  309. * "value way": all axes are non-category axes. So series one by one take
  310. * several (the number is coordSysDims.length) dimensions from dataset.
  311. * The result of data arrengment of data dimensions like:
  312. * | ser0_x | ser0_y | ser1_x | ser1_y | ser2_x | ser2_y |
  313. * "category way": at least one axis is category axis. So the the first data
  314. * dimension is always mapped to the first category axis and shared by
  315. * all of the series. The other data dimensions are taken by series like
  316. * "value way" does.
  317. * The result of data arrengment of data dimensions like:
  318. * | ser_shared_x | ser0_y | ser1_y | ser2_y |
  319. *
  320. * @param {Array.<Object|string>} coordDimensions [{name: <string>, type: <string>, dimsDef: <Array>}, ...]
  321. * @param {module:model/Series} seriesModel
  322. * @param {module:data/Source} source
  323. * @return {Object} encode Never be `null/undefined`.
  324. */
  325. function makeSeriesEncodeForAxisCoordSys(coordDimensions, seriesModel, source) {
  326. var encode = {};
  327. var datasetModel = getDatasetModel(seriesModel); // Currently only make default when using dataset, util more reqirements occur.
  328. if (!datasetModel || !coordDimensions) {
  329. return encode;
  330. }
  331. var encodeItemName = [];
  332. var encodeSeriesName = [];
  333. var ecModel = seriesModel.ecModel;
  334. var datasetMap = inner(ecModel).datasetMap;
  335. var key = datasetModel.uid + '_' + source.seriesLayoutBy;
  336. var baseCategoryDimIndex;
  337. var categoryWayValueDimStart;
  338. coordDimensions = coordDimensions.slice();
  339. each(coordDimensions, function (coordDimInfo, coordDimIdx) {
  340. !isObject(coordDimInfo) && (coordDimensions[coordDimIdx] = {
  341. name: coordDimInfo
  342. });
  343. if (coordDimInfo.type === 'ordinal' && baseCategoryDimIndex == null) {
  344. baseCategoryDimIndex = coordDimIdx;
  345. categoryWayValueDimStart = getDataDimCountOnCoordDim(coordDimensions[coordDimIdx]);
  346. }
  347. encode[coordDimInfo.name] = [];
  348. });
  349. var datasetRecord = datasetMap.get(key) || datasetMap.set(key, {
  350. categoryWayDim: categoryWayValueDimStart,
  351. valueWayDim: 0
  352. }); // TODO
  353. // Auto detect first time axis and do arrangement.
  354. each(coordDimensions, function (coordDimInfo, coordDimIdx) {
  355. var coordDimName = coordDimInfo.name;
  356. var count = getDataDimCountOnCoordDim(coordDimInfo); // In value way.
  357. if (baseCategoryDimIndex == null) {
  358. var start = datasetRecord.valueWayDim;
  359. pushDim(encode[coordDimName], start, count);
  360. pushDim(encodeSeriesName, start, count);
  361. datasetRecord.valueWayDim += count; // ??? TODO give a better default series name rule?
  362. // especially when encode x y specified.
  363. // consider: when mutiple series share one dimension
  364. // category axis, series name should better use
  365. // the other dimsion name. On the other hand, use
  366. // both dimensions name.
  367. } // In category way, the first category axis.
  368. else if (baseCategoryDimIndex === coordDimIdx) {
  369. pushDim(encode[coordDimName], 0, count);
  370. pushDim(encodeItemName, 0, count);
  371. } // In category way, the other axis.
  372. else {
  373. var start = datasetRecord.categoryWayDim;
  374. pushDim(encode[coordDimName], start, count);
  375. pushDim(encodeSeriesName, start, count);
  376. datasetRecord.categoryWayDim += count;
  377. }
  378. });
  379. function pushDim(dimIdxArr, idxFrom, idxCount) {
  380. for (var i = 0; i < idxCount; i++) {
  381. dimIdxArr.push(idxFrom + i);
  382. }
  383. }
  384. function getDataDimCountOnCoordDim(coordDimInfo) {
  385. var dimsDef = coordDimInfo.dimsDef;
  386. return dimsDef ? dimsDef.length : 1;
  387. }
  388. encodeItemName.length && (encode.itemName = encodeItemName);
  389. encodeSeriesName.length && (encode.seriesName = encodeSeriesName);
  390. return encode;
  391. }
  392. /**
  393. * Work for data like [{name: ..., value: ...}, ...].
  394. *
  395. * @param {module:model/Series} seriesModel
  396. * @param {module:data/Source} source
  397. * @return {Object} encode Never be `null/undefined`.
  398. */
  399. function makeSeriesEncodeForNameBased(seriesModel, source, dimCount) {
  400. var encode = {};
  401. var datasetModel = getDatasetModel(seriesModel); // Currently only make default when using dataset, util more reqirements occur.
  402. if (!datasetModel) {
  403. return encode;
  404. }
  405. var sourceFormat = source.sourceFormat;
  406. var dimensionsDefine = source.dimensionsDefine;
  407. var potentialNameDimIndex;
  408. if (sourceFormat === SOURCE_FORMAT_OBJECT_ROWS || sourceFormat === SOURCE_FORMAT_KEYED_COLUMNS) {
  409. each(dimensionsDefine, function (dim, idx) {
  410. if ((isObject(dim) ? dim.name : dim) === 'name') {
  411. potentialNameDimIndex = idx;
  412. }
  413. });
  414. } // idxResult: {v, n}.
  415. var idxResult = function () {
  416. var idxRes0 = {};
  417. var idxRes1 = {};
  418. var guessRecords = []; // 5 is an experience value.
  419. for (var i = 0, len = Math.min(5, dimCount); i < len; i++) {
  420. var guessResult = doGuessOrdinal(source.data, sourceFormat, source.seriesLayoutBy, dimensionsDefine, source.startIndex, i);
  421. guessRecords.push(guessResult);
  422. var isPureNumber = guessResult === BE_ORDINAL.Not; // [Strategy of idxRes0]: find the first BE_ORDINAL.Not as the value dim,
  423. // and then find a name dim with the priority:
  424. // "BE_ORDINAL.Might|BE_ORDINAL.Must" > "other dim" > "the value dim itself".
  425. if (isPureNumber && idxRes0.v == null && i !== potentialNameDimIndex) {
  426. idxRes0.v = i;
  427. }
  428. if (idxRes0.n == null || idxRes0.n === idxRes0.v || !isPureNumber && guessRecords[idxRes0.n] === BE_ORDINAL.Not) {
  429. idxRes0.n = i;
  430. }
  431. if (fulfilled(idxRes0) && guessRecords[idxRes0.n] !== BE_ORDINAL.Not) {
  432. return idxRes0;
  433. } // [Strategy of idxRes1]: if idxRes0 not satisfied (that is, no BE_ORDINAL.Not),
  434. // find the first BE_ORDINAL.Might as the value dim,
  435. // and then find a name dim with the priority:
  436. // "other dim" > "the value dim itself".
  437. // That is for backward compat: number-like (e.g., `'3'`, `'55'`) can be
  438. // treated as number.
  439. if (!isPureNumber) {
  440. if (guessResult === BE_ORDINAL.Might && idxRes1.v == null && i !== potentialNameDimIndex) {
  441. idxRes1.v = i;
  442. }
  443. if (idxRes1.n == null || idxRes1.n === idxRes1.v) {
  444. idxRes1.n = i;
  445. }
  446. }
  447. }
  448. function fulfilled(idxResult) {
  449. return idxResult.v != null && idxResult.n != null;
  450. }
  451. return fulfilled(idxRes0) ? idxRes0 : fulfilled(idxRes1) ? idxRes1 : null;
  452. }();
  453. if (idxResult) {
  454. encode.value = idxResult.v; // `potentialNameDimIndex` has highest priority.
  455. var nameDimIndex = potentialNameDimIndex != null ? potentialNameDimIndex : idxResult.n; // By default, label use itemName in charts.
  456. // So we dont set encodeLabel here.
  457. encode.itemName = [nameDimIndex];
  458. encode.seriesName = [nameDimIndex];
  459. }
  460. return encode;
  461. }
  462. /**
  463. * If return null/undefined, indicate that should not use datasetModel.
  464. */
  465. function getDatasetModel(seriesModel) {
  466. var option = seriesModel.option; // Caution: consider the scenario:
  467. // A dataset is declared and a series is not expected to use the dataset,
  468. // and at the beginning `setOption({series: { noData })` (just prepare other
  469. // option but no data), then `setOption({series: {data: [...]}); In this case,
  470. // the user should set an empty array to avoid that dataset is used by default.
  471. var thisData = option.data;
  472. if (!thisData) {
  473. return seriesModel.ecModel.getComponent('dataset', option.datasetIndex || 0);
  474. }
  475. }
  476. /**
  477. * The rule should not be complex, otherwise user might not
  478. * be able to known where the data is wrong.
  479. * The code is ugly, but how to make it neat?
  480. *
  481. * @param {module:echars/data/Source} source
  482. * @param {number} dimIndex
  483. * @return {BE_ORDINAL} guess result.
  484. */
  485. function guessOrdinal(source, dimIndex) {
  486. return doGuessOrdinal(source.data, source.sourceFormat, source.seriesLayoutBy, source.dimensionsDefine, source.startIndex, dimIndex);
  487. } // dimIndex may be overflow source data.
  488. // return {BE_ORDINAL}
  489. function doGuessOrdinal(data, sourceFormat, seriesLayoutBy, dimensionsDefine, startIndex, dimIndex) {
  490. var result; // Experience value.
  491. var maxLoop = 5;
  492. if (isTypedArray(data)) {
  493. return BE_ORDINAL.Not;
  494. } // When sourceType is 'objectRows' or 'keyedColumns', dimensionsDefine
  495. // always exists in source.
  496. var dimName;
  497. var dimType;
  498. if (dimensionsDefine) {
  499. var dimDefItem = dimensionsDefine[dimIndex];
  500. if (isObject(dimDefItem)) {
  501. dimName = dimDefItem.name;
  502. dimType = dimDefItem.type;
  503. } else if (isString(dimDefItem)) {
  504. dimName = dimDefItem;
  505. }
  506. }
  507. if (dimType != null) {
  508. return dimType === 'ordinal' ? BE_ORDINAL.Must : BE_ORDINAL.Not;
  509. }
  510. if (sourceFormat === SOURCE_FORMAT_ARRAY_ROWS) {
  511. if (seriesLayoutBy === SERIES_LAYOUT_BY_ROW) {
  512. var sample = data[dimIndex];
  513. for (var i = 0; i < (sample || []).length && i < maxLoop; i++) {
  514. if ((result = detectValue(sample[startIndex + i])) != null) {
  515. return result;
  516. }
  517. }
  518. } else {
  519. for (var i = 0; i < data.length && i < maxLoop; i++) {
  520. var row = data[startIndex + i];
  521. if (row && (result = detectValue(row[dimIndex])) != null) {
  522. return result;
  523. }
  524. }
  525. }
  526. } else if (sourceFormat === SOURCE_FORMAT_OBJECT_ROWS) {
  527. if (!dimName) {
  528. return BE_ORDINAL.Not;
  529. }
  530. for (var i = 0; i < data.length && i < maxLoop; i++) {
  531. var item = data[i];
  532. if (item && (result = detectValue(item[dimName])) != null) {
  533. return result;
  534. }
  535. }
  536. } else if (sourceFormat === SOURCE_FORMAT_KEYED_COLUMNS) {
  537. if (!dimName) {
  538. return BE_ORDINAL.Not;
  539. }
  540. var sample = data[dimName];
  541. if (!sample || isTypedArray(sample)) {
  542. return BE_ORDINAL.Not;
  543. }
  544. for (var i = 0; i < sample.length && i < maxLoop; i++) {
  545. if ((result = detectValue(sample[i])) != null) {
  546. return result;
  547. }
  548. }
  549. } else if (sourceFormat === SOURCE_FORMAT_ORIGINAL) {
  550. for (var i = 0; i < data.length && i < maxLoop; i++) {
  551. var item = data[i];
  552. var val = getDataItemValue(item);
  553. if (!isArray(val)) {
  554. return BE_ORDINAL.Not;
  555. }
  556. if ((result = detectValue(val[dimIndex])) != null) {
  557. return result;
  558. }
  559. }
  560. }
  561. function detectValue(val) {
  562. var beStr = isString(val); // Consider usage convenience, '1', '2' will be treated as "number".
  563. // `isFinit('')` get `true`.
  564. if (val != null && isFinite(val) && val !== '') {
  565. return beStr ? BE_ORDINAL.Might : BE_ORDINAL.Not;
  566. } else if (beStr && val !== '-') {
  567. return BE_ORDINAL.Must;
  568. }
  569. }
  570. return BE_ORDINAL.Not;
  571. }
  572. exports.BE_ORDINAL = BE_ORDINAL;
  573. exports.detectSourceFormat = detectSourceFormat;
  574. exports.getSource = getSource;
  575. exports.resetSourceDefaulter = resetSourceDefaulter;
  576. exports.prepareSource = prepareSource;
  577. exports.makeSeriesEncodeForAxisCoordSys = makeSeriesEncodeForAxisCoordSys;
  578. exports.makeSeriesEncodeForNameBased = makeSeriesEncodeForNameBased;
  579. exports.guessOrdinal = guessOrdinal;