createListFromArray.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 zrUtil = require("zrender/lib/core/util");
  20. var List = require("../../data/List");
  21. var createDimensions = require("../../data/helper/createDimensions");
  22. var _sourceType = require("../../data/helper/sourceType");
  23. var SOURCE_FORMAT_ORIGINAL = _sourceType.SOURCE_FORMAT_ORIGINAL;
  24. var _dimensionHelper = require("../../data/helper/dimensionHelper");
  25. var getDimensionTypeByAxis = _dimensionHelper.getDimensionTypeByAxis;
  26. var _model = require("../../util/model");
  27. var getDataItemValue = _model.getDataItemValue;
  28. var CoordinateSystem = require("../../CoordinateSystem");
  29. var _referHelper = require("../../model/referHelper");
  30. var getCoordSysInfoBySeries = _referHelper.getCoordSysInfoBySeries;
  31. var Source = require("../../data/Source");
  32. var _dataStackHelper = require("../../data/helper/dataStackHelper");
  33. var enableDataStack = _dataStackHelper.enableDataStack;
  34. var _sourceHelper = require("../../data/helper/sourceHelper");
  35. var makeSeriesEncodeForAxisCoordSys = _sourceHelper.makeSeriesEncodeForAxisCoordSys;
  36. /*
  37. * Licensed to the Apache Software Foundation (ASF) under one
  38. * or more contributor license agreements. See the NOTICE file
  39. * distributed with this work for additional information
  40. * regarding copyright ownership. The ASF licenses this file
  41. * to you under the Apache License, Version 2.0 (the
  42. * "License"); you may not use this file except in compliance
  43. * with the License. You may obtain a copy of the License at
  44. *
  45. * http://www.apache.org/licenses/LICENSE-2.0
  46. *
  47. * Unless required by applicable law or agreed to in writing,
  48. * software distributed under the License is distributed on an
  49. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  50. * KIND, either express or implied. See the License for the
  51. * specific language governing permissions and limitations
  52. * under the License.
  53. */
  54. /**
  55. * @param {module:echarts/data/Source|Array} source Or raw data.
  56. * @param {module:echarts/model/Series} seriesModel
  57. * @param {Object} [opt]
  58. * @param {string} [opt.generateCoord]
  59. * @param {boolean} [opt.useEncodeDefaulter]
  60. */
  61. function createListFromArray(source, seriesModel, opt) {
  62. opt = opt || {};
  63. if (!Source.isInstance(source)) {
  64. source = Source.seriesDataToSource(source);
  65. }
  66. var coordSysName = seriesModel.get('coordinateSystem');
  67. var registeredCoordSys = CoordinateSystem.get(coordSysName);
  68. var coordSysInfo = getCoordSysInfoBySeries(seriesModel);
  69. var coordSysDimDefs;
  70. if (coordSysInfo) {
  71. coordSysDimDefs = zrUtil.map(coordSysInfo.coordSysDims, function (dim) {
  72. var dimInfo = {
  73. name: dim
  74. };
  75. var axisModel = coordSysInfo.axisMap.get(dim);
  76. if (axisModel) {
  77. var axisType = axisModel.get('type');
  78. dimInfo.type = getDimensionTypeByAxis(axisType); // dimInfo.stackable = isStackable(axisType);
  79. }
  80. return dimInfo;
  81. });
  82. }
  83. if (!coordSysDimDefs) {
  84. // Get dimensions from registered coordinate system
  85. coordSysDimDefs = registeredCoordSys && (registeredCoordSys.getDimensionsInfo ? registeredCoordSys.getDimensionsInfo() : registeredCoordSys.dimensions.slice()) || ['x', 'y'];
  86. }
  87. var dimInfoList = createDimensions(source, {
  88. coordDimensions: coordSysDimDefs,
  89. generateCoord: opt.generateCoord,
  90. encodeDefaulter: opt.useEncodeDefaulter ? zrUtil.curry(makeSeriesEncodeForAxisCoordSys, coordSysDimDefs, seriesModel) : null
  91. });
  92. var firstCategoryDimIndex;
  93. var hasNameEncode;
  94. coordSysInfo && zrUtil.each(dimInfoList, function (dimInfo, dimIndex) {
  95. var coordDim = dimInfo.coordDim;
  96. var categoryAxisModel = coordSysInfo.categoryAxisMap.get(coordDim);
  97. if (categoryAxisModel) {
  98. if (firstCategoryDimIndex == null) {
  99. firstCategoryDimIndex = dimIndex;
  100. }
  101. dimInfo.ordinalMeta = categoryAxisModel.getOrdinalMeta();
  102. }
  103. if (dimInfo.otherDims.itemName != null) {
  104. hasNameEncode = true;
  105. }
  106. });
  107. if (!hasNameEncode && firstCategoryDimIndex != null) {
  108. dimInfoList[firstCategoryDimIndex].otherDims.itemName = 0;
  109. }
  110. var stackCalculationInfo = enableDataStack(seriesModel, dimInfoList);
  111. var list = new List(dimInfoList, seriesModel);
  112. list.setCalculationInfo(stackCalculationInfo);
  113. var dimValueGetter = firstCategoryDimIndex != null && isNeedCompleteOrdinalData(source) ? function (itemOpt, dimName, dataIndex, dimIndex) {
  114. // Use dataIndex as ordinal value in categoryAxis
  115. return dimIndex === firstCategoryDimIndex ? dataIndex : this.defaultDimValueGetter(itemOpt, dimName, dataIndex, dimIndex);
  116. } : null;
  117. list.hasItemOption = false;
  118. list.initData(source, null, dimValueGetter);
  119. return list;
  120. }
  121. function isNeedCompleteOrdinalData(source) {
  122. if (source.sourceFormat === SOURCE_FORMAT_ORIGINAL) {
  123. var sampleItem = firstDataNotNull(source.data || []);
  124. return sampleItem != null && !zrUtil.isArray(getDataItemValue(sampleItem));
  125. }
  126. }
  127. function firstDataNotNull(data) {
  128. var i = 0;
  129. while (i < data.length && data[i] == null) {
  130. i++;
  131. }
  132. return data[i];
  133. }
  134. var _default = createListFromArray;
  135. module.exports = _default;