chart.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __rest = (this && this.__rest) || function (s, e) {
  9. var t = {};
  10. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  11. t[p] = s[p];
  12. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  13. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  14. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  15. t[p[i]] = s[p[i]];
  16. }
  17. return t;
  18. };
  19. var __importDefault = (this && this.__importDefault) || function (mod) {
  20. return (mod && mod.__esModule) ? mod : { "default": mod };
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. exports.Chart = exports.props = exports.G2_CHART_KEY = void 0;
  24. const g_1 = require("@antv/g");
  25. const g_canvas_1 = require("@antv/g-canvas");
  26. const g_plugin_dragndrop_1 = require("@antv/g-plugin-dragndrop");
  27. const util_1 = require("@antv/util");
  28. const event_emitter_1 = __importDefault(require("@antv/event-emitter"));
  29. const runtime_1 = require("../runtime");
  30. const event_1 = require("../utils/event");
  31. const props_1 = require("./props");
  32. const mark_1 = require("./mark");
  33. const composition_1 = require("./composition");
  34. const library_1 = require("./library");
  35. const utils_1 = require("./utils");
  36. exports.G2_CHART_KEY = 'G2_CHART_KEY';
  37. exports.props = [
  38. { name: 'data', type: 'value' },
  39. { name: 'width', type: 'value' },
  40. { name: 'height', type: 'value' },
  41. { name: 'coordinate', type: 'value' },
  42. { name: 'interaction', type: 'object' },
  43. { name: 'theme', type: 'object' },
  44. { name: 'title', type: 'value' },
  45. { name: 'transform', type: 'array' },
  46. { name: 'scale', type: 'object' },
  47. { name: 'axis', type: 'object' },
  48. { name: 'legend', type: 'object' },
  49. { name: 'style', type: 'object' },
  50. { name: 'labelTransform', type: 'array' },
  51. ...(0, props_1.nodeProps)(mark_1.mark),
  52. ...(0, props_1.containerProps)(composition_1.composition),
  53. ];
  54. let Chart = class Chart extends composition_1.View {
  55. constructor(options) {
  56. const _a = options || {}, { container, canvas, renderer, plugins } = _a, rest = __rest(_a, ["container", "canvas", "renderer", "plugins"]);
  57. super(rest, 'view');
  58. // Identifies whether bindAutoFit.
  59. this._hasBindAutoFit = false;
  60. this._rendering = false;
  61. this._trailing = false;
  62. this._trailingResolve = null;
  63. this._trailingReject = null;
  64. this._previousDefinedType = null;
  65. this._onResize = (0, util_1.debounce)(() => {
  66. this.forceFit();
  67. }, 300);
  68. this._renderer = renderer || new g_canvas_1.Renderer();
  69. this._plugins = plugins || [];
  70. this._container = (0, utils_1.normalizeContainer)(container);
  71. this._emitter = new event_emitter_1.default();
  72. this._context = { library: library_1.library, emitter: this._emitter, canvas };
  73. }
  74. render() {
  75. if (this._rendering)
  76. return this._addToTrailing();
  77. if (!this._context.canvas)
  78. this._createCanvas();
  79. this._bindAutoFit();
  80. this._rendering = true;
  81. const finished = new Promise((resolve, reject) => (0, runtime_1.render)(this._computedOptions(), this._context, this._createResolve(resolve), this._createReject(reject)));
  82. const [finished1, resolve, reject] = (0, utils_1.createEmptyPromise)();
  83. finished
  84. .then(resolve)
  85. .catch(reject)
  86. .then(() => this._renderTrailing());
  87. return finished1;
  88. }
  89. /**
  90. * @overload
  91. * @param {G2ViewTree} [options]
  92. * @returns {Chart|G2ViewTree}
  93. */
  94. options(options) {
  95. if (arguments.length === 0)
  96. return (0, utils_1.optionsOf)(this);
  97. const { type } = options;
  98. if (type)
  99. this._previousDefinedType = type;
  100. (0, utils_1.updateRoot)(this, options, this._previousDefinedType);
  101. return this;
  102. }
  103. getContainer() {
  104. return this._container;
  105. }
  106. getContext() {
  107. return this._context;
  108. }
  109. on(event, callback, once) {
  110. this._emitter.on(event, callback, once);
  111. return this;
  112. }
  113. once(event, callback) {
  114. this._emitter.once(event, callback);
  115. return this;
  116. }
  117. emit(event, ...args) {
  118. this._emitter.emit(event, ...args);
  119. return this;
  120. }
  121. off(event, callback) {
  122. this._emitter.off(event, callback);
  123. return this;
  124. }
  125. clear() {
  126. const options = this.options();
  127. this.emit(event_1.ChartEvent.BEFORE_CLEAR);
  128. this._reset();
  129. (0, runtime_1.destroy)(options, this._context, false);
  130. this.emit(event_1.ChartEvent.AFTER_CLEAR);
  131. }
  132. destroy() {
  133. const options = this.options();
  134. this.emit(event_1.ChartEvent.BEFORE_DESTROY);
  135. this._unbindAutoFit();
  136. this._reset();
  137. (0, runtime_1.destroy)(options, this._context, true);
  138. (0, utils_1.removeContainer)(this._container);
  139. this.emit(event_1.ChartEvent.AFTER_DESTROY);
  140. }
  141. forceFit() {
  142. // Don't fit if size do not change.
  143. this.options['autoFit'] = true;
  144. const { width, height } = (0, utils_1.sizeOf)(this.options(), this._container);
  145. if (width === this._width && height === this._height) {
  146. return Promise.resolve(this);
  147. }
  148. // Don't call changeSize to prevent update width and height of options.
  149. this.emit(event_1.ChartEvent.BEFORE_CHANGE_SIZE);
  150. const finished = this.render();
  151. finished.then(() => {
  152. this.emit(event_1.ChartEvent.AFTER_CHANGE_SIZE);
  153. });
  154. return finished;
  155. }
  156. changeSize(width, height) {
  157. if (width === this._width && height === this._height) {
  158. return Promise.resolve(this);
  159. }
  160. this.emit(event_1.ChartEvent.BEFORE_CHANGE_SIZE);
  161. this.width(width);
  162. this.height(height);
  163. const finished = this.render();
  164. finished.then(() => {
  165. this.emit(event_1.ChartEvent.AFTER_CHANGE_SIZE);
  166. });
  167. return finished;
  168. }
  169. _reset() {
  170. this.type = 'view';
  171. this.value = {};
  172. this.children = [];
  173. }
  174. _renderTrailing() {
  175. if (!this._trailing)
  176. return;
  177. this._trailing = false;
  178. this.render()
  179. .then(() => {
  180. const trailingResolve = this._trailingResolve.bind(this);
  181. this._trailingResolve = null;
  182. trailingResolve(this);
  183. })
  184. .catch((error) => {
  185. const trailingReject = this._trailingReject.bind(this);
  186. this._trailingReject = null;
  187. trailingReject(error);
  188. });
  189. }
  190. _createResolve(resolve) {
  191. return () => {
  192. this._rendering = false;
  193. resolve(this);
  194. };
  195. }
  196. _createReject(reject) {
  197. return (error) => {
  198. this._rendering = false;
  199. reject(error);
  200. };
  201. }
  202. // Update actual size and key.
  203. _computedOptions() {
  204. const options = this.options();
  205. const { key = exports.G2_CHART_KEY } = options;
  206. const { width, height } = (0, utils_1.sizeOf)(options, this._container);
  207. this._width = width;
  208. this._height = height;
  209. this._key = key;
  210. return Object.assign(Object.assign({ key: this._key }, options), { width, height });
  211. }
  212. // Create canvas if it does not exist.
  213. // DragAndDropPlugin is for interaction.
  214. // It is OK to register more than one time, G will handle this.
  215. _createCanvas() {
  216. const { width, height } = (0, utils_1.sizeOf)(this.options(), this._container);
  217. this._plugins.push(new g_plugin_dragndrop_1.Plugin());
  218. this._plugins.forEach((d) => this._renderer.registerPlugin(d));
  219. this._context.canvas = new g_1.Canvas({
  220. container: this._container,
  221. width,
  222. height,
  223. renderer: this._renderer,
  224. });
  225. }
  226. _addToTrailing() {
  227. var _a;
  228. // Resolve previous promise, and give up this task.
  229. (_a = this._trailingResolve) === null || _a === void 0 ? void 0 : _a.call(this, this);
  230. // Create new task.
  231. this._trailing = true;
  232. const promise = new Promise((resolve, reject) => {
  233. this._trailingResolve = resolve;
  234. this._trailingReject = reject;
  235. });
  236. return promise;
  237. }
  238. _bindAutoFit() {
  239. const options = this.options();
  240. const { autoFit } = options;
  241. if (this._hasBindAutoFit) {
  242. // If it was bind before, unbind it now.
  243. if (!autoFit)
  244. this._unbindAutoFit();
  245. return;
  246. }
  247. if (autoFit) {
  248. this._hasBindAutoFit = true;
  249. window.addEventListener('resize', this._onResize);
  250. }
  251. }
  252. _unbindAutoFit() {
  253. if (this._hasBindAutoFit) {
  254. this._hasBindAutoFit = false;
  255. window.removeEventListener('resize', this._onResize);
  256. }
  257. }
  258. };
  259. Chart = __decorate([
  260. (0, props_1.defineProps)(exports.props)
  261. ], Chart);
  262. exports.Chart = Chart;
  263. //# sourceMappingURL=chart.js.map