guide.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const Util = require('../util/common');
  2. const Guide = require('../component/guide/base');
  3. const Global = require('../global');
  4. // 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. class GuideController {
  71. constructor(cfg) {
  72. this.guides = [];
  73. this.xScale = null;
  74. this.yScales = null;
  75. this.guideShapes = [];
  76. Util.mix(this, cfg);
  77. }
  78. _toString(position) {
  79. if (Util.isFunction(position)) {
  80. position = position(this.xScale, this.yScales);
  81. }
  82. position = position.toString();
  83. return position;
  84. }
  85. _getId(shape, guide) {
  86. let id = guide.id;
  87. if (!id) {
  88. const type = guide.type;
  89. if (type === 'arc' || type === 'line' || type === 'rect') {
  90. id = this._toString(guide.start) + '-' + this._toString(guide.end);
  91. } else {
  92. id = this._toString(guide.position);
  93. }
  94. }
  95. return id;
  96. }
  97. paint(coord) {
  98. const self = this;
  99. const { chart, guides, xScale, yScales } = self;
  100. const guideShapes = [];
  101. Util.each(guides, function(guide, idx) {
  102. guide.xScale = xScale;
  103. guide.yScales = yScales;
  104. let container;
  105. if (guide.type === 'regionFilter') { // TODO: RegionFilter support animation
  106. guide.chart = chart;
  107. } else {
  108. container = guide.top ? self.frontPlot : self.backPlot;
  109. }
  110. guide.coord = coord;
  111. guide.container = container;
  112. guide.canvas = chart.get('canvas');
  113. const shape = guide.render(coord, container);
  114. if (shape) {
  115. const id = self._getId(shape, guide);
  116. [].concat(shape).forEach(s => {
  117. s._id = s.get('className') + '-' + id;
  118. s.set('index', idx);
  119. guideShapes.push(s);
  120. });
  121. }
  122. });
  123. self.guideShapes = guideShapes;
  124. }
  125. clear() {
  126. this.reset();
  127. this.guides = [];
  128. return this;
  129. }
  130. reset() {
  131. const guides = this.guides;
  132. Util.each(guides, guide => {
  133. guide.remove();
  134. });
  135. }
  136. _createGuide(type, cfg) {
  137. const ClassName = Util.upperFirst(type);
  138. const guide = new Guide[ClassName](Util.deepMix({}, Global.guide[type], cfg));
  139. this.guides.push(guide);
  140. return guide;
  141. }
  142. line(cfg = {}) {
  143. return this._createGuide('line', cfg);
  144. }
  145. text(cfg = {}) {
  146. return this._createGuide('text', cfg);
  147. }
  148. arc(cfg = {}) {
  149. return this._createGuide('arc', cfg);
  150. }
  151. html(cfg = {}) {
  152. return this._createGuide('html', cfg);
  153. }
  154. rect(cfg = {}) {
  155. return this._createGuide('rect', cfg);
  156. }
  157. tag(cfg = {}) {
  158. return this._createGuide('tag', cfg);
  159. }
  160. point(cfg = {}) {
  161. return this._createGuide('point', cfg);
  162. }
  163. regionFilter(cfg = {}) {
  164. return this._createGuide('regionFilter', cfg);
  165. }
  166. }
  167. module.exports = {
  168. init(chart) {
  169. const guideController = new GuideController({
  170. frontPlot: chart.get('frontPlot').addGroup({
  171. zIndex: 20,
  172. className: 'guideContainer'
  173. }),
  174. backPlot: chart.get('backPlot').addGroup({
  175. className: 'guideContainer'
  176. })
  177. });
  178. chart.set('guideController', guideController);
  179. /**
  180. * 为图表添加 guide
  181. * @return {GuideController} 返回 guide 控制器
  182. */
  183. chart.guide = function() {
  184. return guideController;
  185. };
  186. },
  187. afterGeomDraw(chart) {
  188. const guideController = chart.get('guideController');
  189. if (!guideController.guides.length) {
  190. return;
  191. }
  192. const xScale = chart.getXScale();
  193. const yScales = chart.getYScales();
  194. const coord = chart.get('coord');
  195. guideController.xScale = xScale;
  196. guideController.yScales = yScales;
  197. guideController.chart = chart; // for regionFilter
  198. guideController.paint(coord);
  199. },
  200. clear(chart) {
  201. chart.get('guideController').clear();
  202. },
  203. repaint(chart) {
  204. chart.get('guideController').reset();
  205. }
  206. };