markerHelper.js 7.6 KB

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