markerHelper.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if (!item) {
  81. return;
  82. }
  83. var data = seriesModel.getData();
  84. var coordSys = seriesModel.coordinateSystem;
  85. var dims = coordSys && coordSys.dimensions; // 1. If not specify the position with pixel directly
  86. // 2. If `coord` is not a data array. Which uses `xAxis`,
  87. // `yAxis` to specify the coord on each dimension
  88. // parseFloat first because item.x and item.y can be percent string like '20%'
  89. if (!hasXAndY(item) && !isArray(item.coord) && isArray(dims)) {
  90. var axisInfo = getAxisInfo(item, data, coordSys, seriesModel); // Clone the option
  91. // Transform the properties xAxis, yAxis, radiusAxis, angleAxis, geoCoord to value
  92. item = clone(item);
  93. if (item.type && markerTypeCalculator[item.type] && axisInfo.baseAxis && axisInfo.valueAxis) {
  94. var otherCoordIndex = indexOf(dims, axisInfo.baseAxis.dim);
  95. var targetCoordIndex = indexOf(dims, axisInfo.valueAxis.dim);
  96. var coordInfo = markerTypeCalculator[item.type](data, axisInfo.baseDataDim, axisInfo.valueDataDim, otherCoordIndex, targetCoordIndex);
  97. item.coord = coordInfo[0]; // Force to use the value of calculated value.
  98. // let item use the value without stack.
  99. item.value = coordInfo[1];
  100. } else {
  101. // FIXME Only has one of xAxis and yAxis.
  102. item.coord = [item.xAxis != null ? item.xAxis : item.radiusAxis, item.yAxis != null ? item.yAxis : item.angleAxis];
  103. }
  104. } // x y is provided
  105. if (item.coord == null || !isArray(dims)) {
  106. item.coord = [];
  107. } else {
  108. // Each coord support max, min, average
  109. var coord = item.coord;
  110. for (var i = 0; i < 2; i++) {
  111. if (markerTypeCalculator[coord[i]]) {
  112. coord[i] = numCalculate(data, data.mapDimension(dims[i]), coord[i]);
  113. }
  114. }
  115. }
  116. return item;
  117. }
  118. export function getAxisInfo(item, data, coordSys, seriesModel) {
  119. var ret = {};
  120. if (item.valueIndex != null || item.valueDim != null) {
  121. ret.valueDataDim = item.valueIndex != null ? data.getDimension(item.valueIndex) : item.valueDim;
  122. ret.valueAxis = coordSys.getAxis(dataDimToCoordDim(seriesModel, ret.valueDataDim));
  123. ret.baseAxis = coordSys.getOtherAxis(ret.valueAxis);
  124. ret.baseDataDim = data.mapDimension(ret.baseAxis.dim);
  125. } else {
  126. ret.baseAxis = seriesModel.getBaseAxis();
  127. ret.valueAxis = coordSys.getOtherAxis(ret.baseAxis);
  128. ret.baseDataDim = data.mapDimension(ret.baseAxis.dim);
  129. ret.valueDataDim = data.mapDimension(ret.valueAxis.dim);
  130. }
  131. return ret;
  132. }
  133. function dataDimToCoordDim(seriesModel, dataDim) {
  134. var dimItem = seriesModel.getData().getDimensionInfo(dataDim);
  135. return dimItem && dimItem.coordDim;
  136. }
  137. /**
  138. * Filter data which is out of coordinateSystem range
  139. * [dataFilter description]
  140. */
  141. export function dataFilter( // Currently only polar and cartesian has containData.
  142. coordSys, item) {
  143. // Always return true if there is no coordSys
  144. return coordSys && coordSys.containData && item.coord && !hasXOrY(item) ? coordSys.containData(item.coord) : true;
  145. }
  146. export function zoneFilter( // Currently only polar and cartesian has containData.
  147. coordSys, item1, item2) {
  148. // Always return true if there is no coordSys
  149. return coordSys && coordSys.containZone && item1.coord && item2.coord && !hasXOrY(item1) && !hasXOrY(item2) ? coordSys.containZone(item1.coord, item2.coord) : true;
  150. }
  151. export function createMarkerDimValueGetter(inCoordSys, dims) {
  152. return inCoordSys ? function (item, dimName, dataIndex, dimIndex) {
  153. var rawVal = dimIndex < 2 // x, y, radius, angle
  154. ? item.coord && item.coord[dimIndex] : item.value;
  155. return parseDataValue(rawVal, dims[dimIndex]);
  156. } : function (item, dimName, dataIndex, dimIndex) {
  157. return parseDataValue(item.value, dims[dimIndex]);
  158. };
  159. }
  160. export function numCalculate(data, valueDataDim, type) {
  161. if (type === 'average') {
  162. var sum_1 = 0;
  163. var count_1 = 0;
  164. data.each(valueDataDim, function (val, idx) {
  165. if (!isNaN(val)) {
  166. sum_1 += val;
  167. count_1++;
  168. }
  169. });
  170. return sum_1 / count_1;
  171. } else if (type === 'median') {
  172. return data.getMedian(valueDataDim);
  173. } else {
  174. // max & min
  175. return data.getDataExtent(valueDataDim)[type === 'max' ? 1 : 0];
  176. }
  177. }