MarkPointView.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. import * as zrUtil from 'zrender/src/core/util';
  20. import SymbolDraw from '../../chart/helper/SymbolDraw';
  21. import * as numberUtil from '../../util/number';
  22. import List from '../../data/List';
  23. import * as markerHelper from './markerHelper';
  24. import MarkerView from './MarkerView';
  25. function updateMarkerLayout(mpData, seriesModel, api) {
  26. var coordSys = seriesModel.coordinateSystem;
  27. mpData.each(function (idx) {
  28. var itemModel = mpData.getItemModel(idx);
  29. var point;
  30. var xPx = numberUtil.parsePercent(itemModel.get('x'), api.getWidth());
  31. var yPx = numberUtil.parsePercent(itemModel.get('y'), api.getHeight());
  32. if (!isNaN(xPx) && !isNaN(yPx)) {
  33. point = [xPx, yPx];
  34. }
  35. // Chart like bar may have there own marker positioning logic
  36. else if (seriesModel.getMarkerPosition) {
  37. // Use the getMarkerPoisition
  38. point = seriesModel.getMarkerPosition(
  39. mpData.getValues(mpData.dimensions, idx)
  40. );
  41. }
  42. else if (coordSys) {
  43. var x = mpData.get(coordSys.dimensions[0], idx);
  44. var y = mpData.get(coordSys.dimensions[1], idx);
  45. point = coordSys.dataToPoint([x, y]);
  46. }
  47. // Use x, y if has any
  48. if (!isNaN(xPx)) {
  49. point[0] = xPx;
  50. }
  51. if (!isNaN(yPx)) {
  52. point[1] = yPx;
  53. }
  54. mpData.setItemLayout(idx, point);
  55. });
  56. }
  57. export default MarkerView.extend({
  58. type: 'markPoint',
  59. // updateLayout: function (markPointModel, ecModel, api) {
  60. // ecModel.eachSeries(function (seriesModel) {
  61. // var mpModel = seriesModel.markPointModel;
  62. // if (mpModel) {
  63. // updateMarkerLayout(mpModel.getData(), seriesModel, api);
  64. // this.markerGroupMap.get(seriesModel.id).updateLayout(mpModel);
  65. // }
  66. // }, this);
  67. // },
  68. updateTransform: function (markPointModel, ecModel, api) {
  69. ecModel.eachSeries(function (seriesModel) {
  70. var mpModel = seriesModel.markPointModel;
  71. if (mpModel) {
  72. updateMarkerLayout(mpModel.getData(), seriesModel, api);
  73. this.markerGroupMap.get(seriesModel.id).updateLayout(mpModel);
  74. }
  75. }, this);
  76. },
  77. renderSeries: function (seriesModel, mpModel, ecModel, api) {
  78. var coordSys = seriesModel.coordinateSystem;
  79. var seriesId = seriesModel.id;
  80. var seriesData = seriesModel.getData();
  81. var symbolDrawMap = this.markerGroupMap;
  82. var symbolDraw = symbolDrawMap.get(seriesId)
  83. || symbolDrawMap.set(seriesId, new SymbolDraw());
  84. var mpData = createList(coordSys, seriesModel, mpModel);
  85. // FIXME
  86. mpModel.setData(mpData);
  87. updateMarkerLayout(mpModel.getData(), seriesModel, api);
  88. mpData.each(function (idx) {
  89. var itemModel = mpData.getItemModel(idx);
  90. var symbol = itemModel.getShallow('symbol');
  91. var symbolSize = itemModel.getShallow('symbolSize');
  92. var symbolRotate = itemModel.getShallow('symbolRotate');
  93. var isFnSymbol = zrUtil.isFunction(symbol);
  94. var isFnSymbolSize = zrUtil.isFunction(symbolSize);
  95. var isFnSymbolRotate = zrUtil.isFunction(symbolRotate);
  96. if (isFnSymbol || isFnSymbolSize || isFnSymbolRotate) {
  97. var rawIdx = mpModel.getRawValue(idx);
  98. var dataParams = mpModel.getDataParams(idx);
  99. if (isFnSymbol) {
  100. symbol = symbol(rawIdx, dataParams);
  101. }
  102. if (isFnSymbolSize) {
  103. // FIXME 这里不兼容 ECharts 2.x,2.x 貌似参数是整个数据?
  104. symbolSize = symbolSize(rawIdx, dataParams);
  105. }
  106. if (isFnSymbolRotate) {
  107. symbolRotate = symbolRotate(rawIdx, dataParams);
  108. }
  109. }
  110. mpData.setItemVisual(idx, {
  111. symbol: symbol,
  112. symbolSize: symbolSize,
  113. symbolRotate: symbolRotate,
  114. color: itemModel.get('itemStyle.color')
  115. || seriesData.getVisual('color')
  116. });
  117. });
  118. // TODO Text are wrong
  119. symbolDraw.updateData(mpData);
  120. this.group.add(symbolDraw.group);
  121. // Set host model for tooltip
  122. // FIXME
  123. mpData.eachItemGraphicEl(function (el) {
  124. el.traverse(function (child) {
  125. child.dataModel = mpModel;
  126. });
  127. });
  128. symbolDraw.__keep = true;
  129. symbolDraw.group.silent = mpModel.get('silent') || seriesModel.get('silent');
  130. }
  131. });
  132. /**
  133. * @inner
  134. * @param {module:echarts/coord/*} [coordSys]
  135. * @param {module:echarts/model/Series} seriesModel
  136. * @param {module:echarts/model/Model} mpModel
  137. */
  138. function createList(coordSys, seriesModel, mpModel) {
  139. var coordDimsInfos;
  140. if (coordSys) {
  141. coordDimsInfos = zrUtil.map(coordSys && coordSys.dimensions, function (coordDim) {
  142. var info = seriesModel.getData().getDimensionInfo(
  143. seriesModel.getData().mapDimension(coordDim)
  144. ) || {};
  145. // In map series data don't have lng and lat dimension. Fallback to same with coordSys
  146. return zrUtil.defaults({name: coordDim}, info);
  147. });
  148. }
  149. else {
  150. coordDimsInfos = [{
  151. name: 'value',
  152. type: 'float'
  153. }];
  154. }
  155. var mpData = new List(coordDimsInfos, mpModel);
  156. var dataOpt = zrUtil.map(mpModel.get('data'), zrUtil.curry(
  157. markerHelper.dataTransform, seriesModel
  158. ));
  159. if (coordSys) {
  160. dataOpt = zrUtil.filter(
  161. dataOpt, zrUtil.curry(markerHelper.dataFilter, coordSys)
  162. );
  163. }
  164. mpData.initData(dataOpt, null,
  165. coordSys ? markerHelper.dimValueGetter : function (item) {
  166. return item.value;
  167. }
  168. );
  169. return mpData;
  170. }