dimensionHelper.js 6.6 KB

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