dimensionHelper.js 6.6 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. /**
  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 { each, createHashMap, assert, map } from 'zrender/lib/core/util.js';
  41. import { VISUAL_DIMENSIONS } from '../../util/types.js';
  42. var DimensionUserOuput = /** @class */function () {
  43. function DimensionUserOuput(encode, dimRequest) {
  44. this._encode = encode;
  45. this._schema = dimRequest;
  46. }
  47. DimensionUserOuput.prototype.get = function () {
  48. return {
  49. // Do not generate full dimension name until fist used.
  50. fullDimensions: this._getFullDimensionNames(),
  51. encode: this._encode
  52. };
  53. };
  54. /**
  55. * Get all data store dimension names.
  56. * Theoretically a series data store is defined both by series and used dataset (if any).
  57. * If some dimensions are omitted for performance reason in `this.dimensions`,
  58. * the dimension name may not be auto-generated if user does not specify a dimension name.
  59. * In this case, the dimension name is `null`/`undefined`.
  60. */
  61. DimensionUserOuput.prototype._getFullDimensionNames = function () {
  62. if (!this._cachedDimNames) {
  63. this._cachedDimNames = this._schema ? this._schema.makeOutputDimensionNames() : [];
  64. }
  65. return this._cachedDimNames;
  66. };
  67. return DimensionUserOuput;
  68. }();
  69. ;
  70. export function summarizeDimensions(data, schema) {
  71. var summary = {};
  72. var encode = summary.encode = {};
  73. var notExtraCoordDimMap = createHashMap();
  74. var defaultedLabel = [];
  75. var defaultedTooltip = [];
  76. var userOutputEncode = {};
  77. each(data.dimensions, function (dimName) {
  78. var dimItem = data.getDimensionInfo(dimName);
  79. var coordDim = dimItem.coordDim;
  80. if (coordDim) {
  81. if (process.env.NODE_ENV !== 'production') {
  82. assert(VISUAL_DIMENSIONS.get(coordDim) == null);
  83. }
  84. var coordDimIndex = dimItem.coordDimIndex;
  85. getOrCreateEncodeArr(encode, coordDim)[coordDimIndex] = dimName;
  86. if (!dimItem.isExtraCoord) {
  87. notExtraCoordDimMap.set(coordDim, 1);
  88. // Use the last coord dim (and label friendly) as default label,
  89. // because when dataset is used, it is hard to guess which dimension
  90. // can be value dimension. If both show x, y on label is not look good,
  91. // and conventionally y axis is focused more.
  92. if (mayLabelDimType(dimItem.type)) {
  93. defaultedLabel[0] = dimName;
  94. }
  95. // User output encode do not contain generated coords.
  96. // And it only has index. User can use index to retrieve value from the raw item array.
  97. getOrCreateEncodeArr(userOutputEncode, coordDim)[coordDimIndex] = data.getDimensionIndex(dimItem.name);
  98. }
  99. if (dimItem.defaultTooltip) {
  100. defaultedTooltip.push(dimName);
  101. }
  102. }
  103. VISUAL_DIMENSIONS.each(function (v, otherDim) {
  104. var encodeArr = getOrCreateEncodeArr(encode, otherDim);
  105. var dimIndex = dimItem.otherDims[otherDim];
  106. if (dimIndex != null && dimIndex !== false) {
  107. encodeArr[dimIndex] = dimItem.name;
  108. }
  109. });
  110. });
  111. var dataDimsOnCoord = [];
  112. var encodeFirstDimNotExtra = {};
  113. notExtraCoordDimMap.each(function (v, coordDim) {
  114. var dimArr = encode[coordDim];
  115. encodeFirstDimNotExtra[coordDim] = dimArr[0];
  116. // Not necessary to remove duplicate, because a data
  117. // dim canot on more than one coordDim.
  118. dataDimsOnCoord = dataDimsOnCoord.concat(dimArr);
  119. });
  120. summary.dataDimsOnCoord = dataDimsOnCoord;
  121. summary.dataDimIndicesOnCoord = map(dataDimsOnCoord, function (dimName) {
  122. return data.getDimensionInfo(dimName).storeDimIndex;
  123. });
  124. summary.encodeFirstDimNotExtra = encodeFirstDimNotExtra;
  125. var encodeLabel = encode.label;
  126. // FIXME `encode.label` is not recommended, because formatter cannot be set
  127. // in this way. Use label.formatter instead. Maybe remove this approach someday.
  128. if (encodeLabel && encodeLabel.length) {
  129. defaultedLabel = encodeLabel.slice();
  130. }
  131. var encodeTooltip = encode.tooltip;
  132. if (encodeTooltip && encodeTooltip.length) {
  133. defaultedTooltip = encodeTooltip.slice();
  134. } else if (!defaultedTooltip.length) {
  135. defaultedTooltip = defaultedLabel.slice();
  136. }
  137. encode.defaultedLabel = defaultedLabel;
  138. encode.defaultedTooltip = defaultedTooltip;
  139. summary.userOutput = new DimensionUserOuput(userOutputEncode, schema);
  140. return summary;
  141. }
  142. function getOrCreateEncodeArr(encode, dim) {
  143. if (!encode.hasOwnProperty(dim)) {
  144. encode[dim] = [];
  145. }
  146. return encode[dim];
  147. }
  148. // FIXME:TS should be type `AxisType`
  149. export function getDimensionTypeByAxis(axisType) {
  150. return axisType === 'category' ? 'ordinal' : axisType === 'time' ? 'time' : 'float';
  151. }
  152. function mayLabelDimType(dimType) {
  153. // In most cases, ordinal and time do not suitable for label.
  154. // Ordinal info can be displayed on axis. Time is too long.
  155. return !(dimType === 'ordinal' || dimType === 'time');
  156. }
  157. // function findTheLastDimMayLabel(data) {
  158. // // Get last value dim
  159. // let dimensions = data.dimensions.slice();
  160. // let valueType;
  161. // let valueDim;
  162. // while (dimensions.length && (
  163. // valueDim = dimensions.pop(),
  164. // valueType = data.getDimensionInfo(valueDim).type,
  165. // valueType === 'ordinal' || valueType === 'time'
  166. // )) {} // jshint ignore:line
  167. // return valueDim;
  168. // }