guide.js 5.3 KB

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