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