markerHelper.js 7.5 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 * as numberUtil from '../../util/number.js';
  41. import { isDimensionStacked } from '../../data/helper/dataStackHelper.js';
  42. import { indexOf, curry, clone, isArray } from 'zrender/lib/core/util.js';
  43. import { parseDataValue } from '../../data/helper/dataValueHelper.js';
  44. function hasXOrY(item) {
  45. return !(isNaN(parseFloat(item.x)) && isNaN(parseFloat(item.y)));
  46. }
  47. function hasXAndY(item) {
  48. return !isNaN(parseFloat(item.x)) && !isNaN(parseFloat(item.y));
  49. }
  50. function markerTypeCalculatorWithExtent(markerType, data, otherDataDim, targetDataDim, otherCoordIndex, targetCoordIndex) {
  51. var coordArr = [];
  52. var stacked = isDimensionStacked(data, targetDataDim
  53. /*, otherDataDim*/
  54. );
  55. var calcDataDim = stacked ? data.getCalculationInfo('stackResultDimension') : targetDataDim;
  56. var value = numCalculate(data, calcDataDim, markerType);
  57. var dataIndex = data.indicesOfNearest(calcDataDim, value)[0];
  58. coordArr[otherCoordIndex] = data.get(otherDataDim, dataIndex);
  59. coordArr[targetCoordIndex] = data.get(calcDataDim, dataIndex);
  60. var coordArrValue = data.get(targetDataDim, dataIndex); // Make it simple, do not visit all stacked value to count precision.
  61. var precision = numberUtil.getPrecision(data.get(targetDataDim, dataIndex));
  62. precision = Math.min(precision, 20);
  63. if (precision >= 0) {
  64. coordArr[targetCoordIndex] = +coordArr[targetCoordIndex].toFixed(precision);
  65. }
  66. return [coordArr, coordArrValue];
  67. } // TODO Specified percent
  68. var markerTypeCalculator = {
  69. min: curry(markerTypeCalculatorWithExtent, 'min'),
  70. max: curry(markerTypeCalculatorWithExtent, 'max'),
  71. average: curry(markerTypeCalculatorWithExtent, 'average'),
  72. median: curry(markerTypeCalculatorWithExtent, 'median')
  73. };
  74. /**
  75. * Transform markPoint data item to format used in List by do the following
  76. * 1. Calculate statistic like `max`, `min`, `average`
  77. * 2. Convert `item.xAxis`, `item.yAxis` to `item.coord` array
  78. */
  79. export function dataTransform(seriesModel, item) {
  80. var data = seriesModel.getData();
  81. var coordSys = seriesModel.coordinateSystem; // 1. If not specify the position with pixel directly
  82. // 2. If `coord` is not a data array. Which uses `xAxis`,
  83. // `yAxis` to specify the coord on each dimension
  84. // parseFloat first because item.x and item.y can be percent string like '20%'
  85. if (item && !hasXAndY(item) && !isArray(item.coord) && coordSys) {
  86. var dims = coordSys.dimensions;
  87. var axisInfo = getAxisInfo(item, data, coordSys, seriesModel); // Clone the option
  88. // Transform the properties xAxis, yAxis, radiusAxis, angleAxis, geoCoord to value
  89. item = clone(item);
  90. if (item.type && markerTypeCalculator[item.type] && axisInfo.baseAxis && axisInfo.valueAxis) {
  91. var otherCoordIndex = indexOf(dims, axisInfo.baseAxis.dim);
  92. var targetCoordIndex = indexOf(dims, axisInfo.valueAxis.dim);
  93. var coordInfo = markerTypeCalculator[item.type](data, axisInfo.baseDataDim, axisInfo.valueDataDim, otherCoordIndex, targetCoordIndex);
  94. item.coord = coordInfo[0]; // Force to use the value of calculated value.
  95. // let item use the value without stack.
  96. item.value = coordInfo[1];
  97. } else {
  98. // FIXME Only has one of xAxis and yAxis.
  99. var coord = [item.xAxis != null ? item.xAxis : item.radiusAxis, item.yAxis != null ? item.yAxis : item.angleAxis]; // Each coord support max, min, average
  100. for (var i = 0; i < 2; i++) {
  101. if (markerTypeCalculator[coord[i]]) {
  102. coord[i] = numCalculate(data, data.mapDimension(dims[i]), coord[i]);
  103. }
  104. }
  105. item.coord = coord;
  106. }
  107. }
  108. return item;
  109. }
  110. export function getAxisInfo(item, data, coordSys, seriesModel) {
  111. var ret = {};
  112. if (item.valueIndex != null || item.valueDim != null) {
  113. ret.valueDataDim = item.valueIndex != null ? data.getDimension(item.valueIndex) : item.valueDim;
  114. ret.valueAxis = coordSys.getAxis(dataDimToCoordDim(seriesModel, ret.valueDataDim));
  115. ret.baseAxis = coordSys.getOtherAxis(ret.valueAxis);
  116. ret.baseDataDim = data.mapDimension(ret.baseAxis.dim);
  117. } else {
  118. ret.baseAxis = seriesModel.getBaseAxis();
  119. ret.valueAxis = coordSys.getOtherAxis(ret.baseAxis);
  120. ret.baseDataDim = data.mapDimension(ret.baseAxis.dim);
  121. ret.valueDataDim = data.mapDimension(ret.valueAxis.dim);
  122. }
  123. return ret;
  124. }
  125. function dataDimToCoordDim(seriesModel, dataDim) {
  126. var dimItem = seriesModel.getData().getDimensionInfo(dataDim);
  127. return dimItem && dimItem.coordDim;
  128. }
  129. /**
  130. * Filter data which is out of coordinateSystem range
  131. * [dataFilter description]
  132. */
  133. export function dataFilter( // Currently only polar and cartesian has containData.
  134. coordSys, item) {
  135. // Always return true if there is no coordSys
  136. return coordSys && coordSys.containData && item.coord && !hasXOrY(item) ? coordSys.containData(item.coord) : true;
  137. }
  138. export function zoneFilter( // Currently only polar and cartesian has containData.
  139. coordSys, item1, item2) {
  140. // Always return true if there is no coordSys
  141. return coordSys && coordSys.containZone && item1.coord && item2.coord && !hasXOrY(item1) && !hasXOrY(item2) ? coordSys.containZone(item1.coord, item2.coord) : true;
  142. }
  143. export function createMarkerDimValueGetter(inCoordSys, dims) {
  144. return inCoordSys ? function (item, dimName, dataIndex, dimIndex) {
  145. var rawVal = dimIndex < 2 // x, y, radius, angle
  146. ? item.coord && item.coord[dimIndex] : item.value;
  147. return parseDataValue(rawVal, dims[dimIndex]);
  148. } : function (item, dimName, dataIndex, dimIndex) {
  149. return parseDataValue(item.value, dims[dimIndex]);
  150. };
  151. }
  152. export function numCalculate(data, valueDataDim, type) {
  153. if (type === 'average') {
  154. var sum_1 = 0;
  155. var count_1 = 0;
  156. data.each(valueDataDim, function (val, idx) {
  157. if (!isNaN(val)) {
  158. sum_1 += val;
  159. count_1++;
  160. }
  161. });
  162. return sum_1 / count_1;
  163. } else if (type === 'median') {
  164. return data.getMedian(valueDataDim);
  165. } else {
  166. // max & min
  167. return data.getDataExtent(valueDataDim)[type === 'max' ? 1 : 0];
  168. }
  169. }