dimensionHelper.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 _util = require("zrender/lib/core/util");
  20. var each = _util.each;
  21. var createHashMap = _util.createHashMap;
  22. var assert = _util.assert;
  23. var _config = require("../../config");
  24. var __DEV__ = _config.__DEV__;
  25. /*
  26. * Licensed to the Apache Software Foundation (ASF) under one
  27. * or more contributor license agreements. See the NOTICE file
  28. * distributed with this work for additional information
  29. * regarding copyright ownership. The ASF licenses this file
  30. * to you under the Apache License, Version 2.0 (the
  31. * "License"); you may not use this file except in compliance
  32. * with the License. You may obtain a copy of the License at
  33. *
  34. * http://www.apache.org/licenses/LICENSE-2.0
  35. *
  36. * Unless required by applicable law or agreed to in writing,
  37. * software distributed under the License is distributed on an
  38. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  39. * KIND, either express or implied. See the License for the
  40. * specific language governing permissions and limitations
  41. * under the License.
  42. */
  43. var OTHER_DIMENSIONS = createHashMap(['tooltip', 'label', 'itemName', 'itemId', 'seriesName']);
  44. function summarizeDimensions(data) {
  45. var summary = {};
  46. var encode = summary.encode = {};
  47. var notExtraCoordDimMap = createHashMap();
  48. var defaultedLabel = [];
  49. var defaultedTooltip = []; // See the comment of `List.js#userOutput`.
  50. var userOutput = summary.userOutput = {
  51. dimensionNames: data.dimensions.slice(),
  52. encode: {}
  53. };
  54. each(data.dimensions, function (dimName) {
  55. var dimItem = data.getDimensionInfo(dimName);
  56. var coordDim = dimItem.coordDim;
  57. if (coordDim) {
  58. var coordDimIndex = dimItem.coordDimIndex;
  59. getOrCreateEncodeArr(encode, coordDim)[coordDimIndex] = dimName;
  60. if (!dimItem.isExtraCoord) {
  61. notExtraCoordDimMap.set(coordDim, 1); // Use the last coord dim (and label friendly) as default label,
  62. // because when dataset is used, it is hard to guess which dimension
  63. // can be value dimension. If both show x, y on label is not look good,
  64. // and conventionally y axis is focused more.
  65. if (mayLabelDimType(dimItem.type)) {
  66. defaultedLabel[0] = dimName;
  67. } // User output encode do not contain generated coords.
  68. // And it only has index. User can use index to retrieve value from the raw item array.
  69. getOrCreateEncodeArr(userOutput.encode, coordDim)[coordDimIndex] = dimItem.index;
  70. }
  71. if (dimItem.defaultTooltip) {
  72. defaultedTooltip.push(dimName);
  73. }
  74. }
  75. OTHER_DIMENSIONS.each(function (v, otherDim) {
  76. var encodeArr = getOrCreateEncodeArr(encode, otherDim);
  77. var dimIndex = dimItem.otherDims[otherDim];
  78. if (dimIndex != null && dimIndex !== false) {
  79. encodeArr[dimIndex] = dimItem.name;
  80. }
  81. });
  82. });
  83. var dataDimsOnCoord = [];
  84. var encodeFirstDimNotExtra = {};
  85. notExtraCoordDimMap.each(function (v, coordDim) {
  86. var dimArr = encode[coordDim]; // ??? FIXME extra coord should not be set in dataDimsOnCoord.
  87. // But should fix the case that radar axes: simplify the logic
  88. // of `completeDimension`, remove `extraPrefix`.
  89. encodeFirstDimNotExtra[coordDim] = dimArr[0]; // Not necessary to remove duplicate, because a data
  90. // dim canot on more than one coordDim.
  91. dataDimsOnCoord = dataDimsOnCoord.concat(dimArr);
  92. });
  93. summary.dataDimsOnCoord = dataDimsOnCoord;
  94. summary.encodeFirstDimNotExtra = encodeFirstDimNotExtra;
  95. var encodeLabel = encode.label; // FIXME `encode.label` is not recommanded, because formatter can not be set
  96. // in this way. Use label.formatter instead. May be remove this approach someday.
  97. if (encodeLabel && encodeLabel.length) {
  98. defaultedLabel = encodeLabel.slice();
  99. }
  100. var encodeTooltip = encode.tooltip;
  101. if (encodeTooltip && encodeTooltip.length) {
  102. defaultedTooltip = encodeTooltip.slice();
  103. } else if (!defaultedTooltip.length) {
  104. defaultedTooltip = defaultedLabel.slice();
  105. }
  106. encode.defaultedLabel = defaultedLabel;
  107. encode.defaultedTooltip = defaultedTooltip;
  108. return summary;
  109. }
  110. function getOrCreateEncodeArr(encode, dim) {
  111. if (!encode.hasOwnProperty(dim)) {
  112. encode[dim] = [];
  113. }
  114. return encode[dim];
  115. }
  116. function getDimensionTypeByAxis(axisType) {
  117. return axisType === 'category' ? 'ordinal' : axisType === 'time' ? 'time' : 'float';
  118. }
  119. function mayLabelDimType(dimType) {
  120. // In most cases, ordinal and time do not suitable for label.
  121. // Ordinal info can be displayed on axis. Time is too long.
  122. return !(dimType === 'ordinal' || dimType === 'time');
  123. } // function findTheLastDimMayLabel(data) {
  124. // // Get last value dim
  125. // var dimensions = data.dimensions.slice();
  126. // var valueType;
  127. // var valueDim;
  128. // while (dimensions.length && (
  129. // valueDim = dimensions.pop(),
  130. // valueType = data.getDimensionInfo(valueDim).type,
  131. // valueType === 'ordinal' || valueType === 'time'
  132. // )) {} // jshint ignore:line
  133. // return valueDim;
  134. // }
  135. exports.OTHER_DIMENSIONS = OTHER_DIMENSIONS;
  136. exports.summarizeDimensions = summarizeDimensions;
  137. exports.getDimensionTypeByAxis = getDimensionTypeByAxis;