register.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { each } from '../util/common';
  2. import Chart from '../chart/chart';
  3. Chart._Interactions = {};
  4. Chart.registerInteraction = function (type, constructor) {
  5. Chart._Interactions[type] = constructor;
  6. };
  7. Chart.getInteraction = function (type) {
  8. return Chart._Interactions[type];
  9. };
  10. Chart.prototype.interaction = function (type, cfg) {
  11. var interactions = this._interactions || {};
  12. if (interactions[type]) {
  13. // if reprated, destroy last
  14. interactions[type].destroy();
  15. }
  16. var Ctor = Chart.getInteraction(type);
  17. var interact = new Ctor(cfg, this);
  18. interactions[type] = interact;
  19. this._interactions = interactions;
  20. return this;
  21. };
  22. Chart.prototype.clearInteraction = function (type) {
  23. var interactions = this._interactions;
  24. if (!interactions) return;
  25. if (type) {
  26. interactions[type] && interactions[type].destroy();
  27. delete interactions[type];
  28. } else {
  29. each(interactions, function (interaction, key) {
  30. interaction.destroy();
  31. delete interactions[key];
  32. });
  33. }
  34. return this;
  35. };
  36. export default Chart;