MarkPointView.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 symbolRotate = itemModel.getShallow('symbolRotate');
  104. var isFnSymbol = zrUtil.isFunction(symbol);
  105. var isFnSymbolSize = zrUtil.isFunction(symbolSize);
  106. var isFnSymbolRotate = zrUtil.isFunction(symbolRotate);
  107. if (isFnSymbol || isFnSymbolSize || isFnSymbolRotate) {
  108. var rawIdx = mpModel.getRawValue(idx);
  109. var dataParams = mpModel.getDataParams(idx);
  110. if (isFnSymbol) {
  111. symbol = symbol(rawIdx, dataParams);
  112. }
  113. if (isFnSymbolSize) {
  114. // FIXME 这里不兼容 ECharts 2.x,2.x 貌似参数是整个数据?
  115. symbolSize = symbolSize(rawIdx, dataParams);
  116. }
  117. if (isFnSymbolRotate) {
  118. symbolRotate = symbolRotate(rawIdx, dataParams);
  119. }
  120. }
  121. mpData.setItemVisual(idx, {
  122. symbol: symbol,
  123. symbolSize: symbolSize,
  124. symbolRotate: symbolRotate,
  125. color: itemModel.get('itemStyle.color') || seriesData.getVisual('color')
  126. });
  127. }); // TODO Text are wrong
  128. symbolDraw.updateData(mpData);
  129. this.group.add(symbolDraw.group); // Set host model for tooltip
  130. // FIXME
  131. mpData.eachItemGraphicEl(function (el) {
  132. el.traverse(function (child) {
  133. child.dataModel = mpModel;
  134. });
  135. });
  136. symbolDraw.__keep = true;
  137. symbolDraw.group.silent = mpModel.get('silent') || seriesModel.get('silent');
  138. }
  139. });
  140. /**
  141. * @inner
  142. * @param {module:echarts/coord/*} [coordSys]
  143. * @param {module:echarts/model/Series} seriesModel
  144. * @param {module:echarts/model/Model} mpModel
  145. */
  146. function createList(coordSys, seriesModel, mpModel) {
  147. var coordDimsInfos;
  148. if (coordSys) {
  149. coordDimsInfos = zrUtil.map(coordSys && coordSys.dimensions, function (coordDim) {
  150. 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
  151. return zrUtil.defaults({
  152. name: coordDim
  153. }, info);
  154. });
  155. } else {
  156. coordDimsInfos = [{
  157. name: 'value',
  158. type: 'float'
  159. }];
  160. }
  161. var mpData = new List(coordDimsInfos, mpModel);
  162. var dataOpt = zrUtil.map(mpModel.get('data'), zrUtil.curry(markerHelper.dataTransform, seriesModel));
  163. if (coordSys) {
  164. dataOpt = zrUtil.filter(dataOpt, zrUtil.curry(markerHelper.dataFilter, coordSys));
  165. }
  166. mpData.initData(dataOpt, null, coordSys ? markerHelper.dimValueGetter : function (item) {
  167. return item.value;
  168. });
  169. return mpData;
  170. }
  171. module.exports = _default;