guide.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. "use strict";
  2. var Util = require('../util/common');
  3. var Guide = require('../component/guide/base');
  4. var Global = require('../global'); // register the default configuration for Guide
  5. Global.guide = Util.deepMix({
  6. line: {
  7. style: {
  8. stroke: '#a3a3a3',
  9. lineWidth: 1
  10. },
  11. top: true
  12. },
  13. text: {
  14. style: {
  15. fill: '#787878',
  16. textAlign: 'center',
  17. textBaseline: 'middle'
  18. },
  19. offsetX: 0,
  20. offsetY: 0,
  21. top: true
  22. },
  23. rect: {
  24. style: {
  25. fill: '#fafafa'
  26. },
  27. top: false
  28. },
  29. arc: {
  30. style: {
  31. stroke: '#a3a3a3'
  32. },
  33. top: true
  34. },
  35. html: {
  36. offsetX: 0,
  37. offsetY: 0,
  38. alignX: 'center',
  39. alignY: 'middle'
  40. },
  41. tag: {
  42. top: true,
  43. offsetX: 0,
  44. offsetY: 0,
  45. side: 4,
  46. background: {
  47. padding: 5,
  48. radius: 2,
  49. fill: '#1890FF'
  50. },
  51. textStyle: {
  52. fontSize: 12,
  53. fill: '#fff',
  54. textAlign: 'center',
  55. textBaseline: 'middle'
  56. }
  57. },
  58. point: {
  59. top: true,
  60. offsetX: 0,
  61. offsetY: 0,
  62. style: {
  63. fill: '#fff',
  64. r: 3,
  65. lineWidth: 2,
  66. stroke: '#1890ff'
  67. }
  68. }
  69. }, Global.guide || {});
  70. var GuideController = /*#__PURE__*/function () {
  71. function GuideController(cfg) {
  72. this.guides = [];
  73. this.xScale = null;
  74. this.yScales = null;
  75. this.guideShapes = [];
  76. Util.mix(this, cfg);
  77. }
  78. var _proto = GuideController.prototype;
  79. _proto._toString = function _toString(position) {
  80. if (Util.isFunction(position)) {
  81. position = position(this.xScale, this.yScales);
  82. }
  83. position = position.toString();
  84. return position;
  85. };
  86. _proto._getId = function _getId(shape, guide) {
  87. var id = guide.id;
  88. if (!id) {
  89. var type = guide.type;
  90. if (type === 'arc' || type === 'line' || type === 'rect') {
  91. id = this._toString(guide.start) + '-' + this._toString(guide.end);
  92. } else {
  93. id = this._toString(guide.position);
  94. }
  95. }
  96. return id;
  97. };
  98. _proto.paint = function paint(coord) {
  99. var self = this;
  100. var chart = self.chart,
  101. guides = self.guides,
  102. xScale = self.xScale,
  103. yScales = self.yScales;
  104. var guideShapes = [];
  105. Util.each(guides, function (guide, idx) {
  106. guide.xScale = xScale;
  107. guide.yScales = yScales;
  108. var container;
  109. if (guide.type === 'regionFilter') {
  110. // TODO: RegionFilter support animation
  111. guide.chart = chart;
  112. } else {
  113. container = guide.top ? self.frontPlot : self.backPlot;
  114. }
  115. guide.coord = coord;
  116. guide.container = container;
  117. guide.canvas = chart.get('canvas');
  118. var shape = guide.render(coord, container);
  119. if (shape) {
  120. var id = self._getId(shape, guide);
  121. [].concat(shape).forEach(function (s) {
  122. s._id = s.get('className') + '-' + id;
  123. s.set('index', idx);
  124. guideShapes.push(s);
  125. });
  126. }
  127. });
  128. self.guideShapes = guideShapes;
  129. };
  130. _proto.clear = function clear() {
  131. this.reset();
  132. this.guides = [];
  133. return this;
  134. };
  135. _proto.reset = function reset() {
  136. var guides = this.guides;
  137. Util.each(guides, function (guide) {
  138. guide.remove();
  139. });
  140. };
  141. _proto._createGuide = function _createGuide(type, cfg) {
  142. var ClassName = Util.upperFirst(type);
  143. var guide = new Guide[ClassName](Util.deepMix({}, Global.guide[type], cfg));
  144. this.guides.push(guide);
  145. return guide;
  146. };
  147. _proto.line = function line(cfg) {
  148. if (cfg === void 0) {
  149. cfg = {};
  150. }
  151. return this._createGuide('line', cfg);
  152. };
  153. _proto.text = function text(cfg) {
  154. if (cfg === void 0) {
  155. cfg = {};
  156. }
  157. return this._createGuide('text', cfg);
  158. };
  159. _proto.arc = function arc(cfg) {
  160. if (cfg === void 0) {
  161. cfg = {};
  162. }
  163. return this._createGuide('arc', cfg);
  164. };
  165. _proto.html = function html(cfg) {
  166. if (cfg === void 0) {
  167. cfg = {};
  168. }
  169. return this._createGuide('html', cfg);
  170. };
  171. _proto.rect = function rect(cfg) {
  172. if (cfg === void 0) {
  173. cfg = {};
  174. }
  175. return this._createGuide('rect', cfg);
  176. };
  177. _proto.tag = function tag(cfg) {
  178. if (cfg === void 0) {
  179. cfg = {};
  180. }
  181. return this._createGuide('tag', cfg);
  182. };
  183. _proto.point = function point(cfg) {
  184. if (cfg === void 0) {
  185. cfg = {};
  186. }
  187. return this._createGuide('point', cfg);
  188. };
  189. _proto.regionFilter = function regionFilter(cfg) {
  190. if (cfg === void 0) {
  191. cfg = {};
  192. }
  193. return this._createGuide('regionFilter', cfg);
  194. };
  195. return GuideController;
  196. }();
  197. module.exports = {
  198. init: function init(chart) {
  199. var guideController = new GuideController({
  200. frontPlot: chart.get('frontPlot').addGroup({
  201. zIndex: 20,
  202. className: 'guideContainer'
  203. }),
  204. backPlot: chart.get('backPlot').addGroup({
  205. className: 'guideContainer'
  206. })
  207. });
  208. chart.set('guideController', guideController);
  209. /**
  210. * 为图表添加 guide
  211. * @return {GuideController} 返回 guide 控制器
  212. */
  213. chart.guide = function () {
  214. return guideController;
  215. };
  216. },
  217. afterGeomDraw: function afterGeomDraw(chart) {
  218. var guideController = chart.get('guideController');
  219. if (!guideController.guides.length) {
  220. return;
  221. }
  222. var xScale = chart.getXScale();
  223. var yScales = chart.getYScales();
  224. var coord = chart.get('coord');
  225. guideController.xScale = xScale;
  226. guideController.yScales = yScales;
  227. guideController.chart = chart; // for regionFilter
  228. guideController.paint(coord);
  229. },
  230. clear: function clear(chart) {
  231. chart.get('guideController').clear();
  232. },
  233. repaint: function repaint(chart) {
  234. chart.get('guideController').reset();
  235. }
  236. };