chart.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  3. var Base = require('../base');
  4. var Plot = require('./plot');
  5. var Util = require('../util/common');
  6. var Coord = require('../coord/index');
  7. var Geom = require('../geom/base');
  8. var ScaleController = require('./controller/scale');
  9. var AxisController = require('./controller/axis');
  10. var Global = require('../global');
  11. var _require = require('../graphic/index'),
  12. Canvas = _require.Canvas;
  13. var Helper = require('../util/helper');
  14. function isFullCircle(coord) {
  15. var startAngle = coord.startAngle;
  16. var endAngle = coord.endAngle;
  17. if (!Util.isNil(startAngle) && !Util.isNil(endAngle) && endAngle - startAngle < Math.PI * 2) {
  18. return false;
  19. }
  20. return true;
  21. }
  22. function compare(a, b) {
  23. return a - b;
  24. }
  25. function _isScaleExist(scales, compareScale) {
  26. var flag = false;
  27. Util.each(scales, function (scale) {
  28. var scaleValues = [].concat(scale.values);
  29. var compareScaleValues = [].concat(compareScale.values);
  30. if (scale.type === compareScale.type && scale.field === compareScale.field && scaleValues.sort(compare).toString() === compareScaleValues.sort(compare).toString()) {
  31. flag = true;
  32. return;
  33. }
  34. });
  35. return flag;
  36. }
  37. var Chart =
  38. /*#__PURE__*/
  39. function (_Base) {
  40. _inheritsLoose(Chart, _Base);
  41. Chart.initPlugins = function initPlugins() {
  42. return {
  43. _plugins: [],
  44. _cacheId: 0,
  45. register: function register(plugins) {
  46. var p = this._plugins;
  47. [].concat(plugins).forEach(function (plugin) {
  48. if (p.indexOf(plugin) === -1) {
  49. p.push(plugin);
  50. }
  51. });
  52. this._cacheId++;
  53. },
  54. unregister: function unregister(plugins) {
  55. var p = this._plugins;
  56. [].concat(plugins).forEach(function (plugin) {
  57. var idx = p.indexOf(plugin);
  58. if (idx !== -1) {
  59. p.splice(idx, 1);
  60. }
  61. });
  62. this._cacheId++;
  63. },
  64. clear: function clear() {
  65. this._plugins = [];
  66. this._cacheId++;
  67. },
  68. count: function count() {
  69. return this._plugins.length;
  70. },
  71. getAll: function getAll() {
  72. return this._plugins;
  73. },
  74. notify: function notify(chart, hook, args) {
  75. var descriptors = this.descriptors(chart);
  76. var ilen = descriptors.length;
  77. var i;
  78. var descriptor;
  79. var plugin;
  80. var params;
  81. var method;
  82. for (i = 0; i < ilen; ++i) {
  83. descriptor = descriptors[i];
  84. plugin = descriptor.plugin;
  85. method = plugin[hook];
  86. if (typeof method === 'function') {
  87. params = [chart].concat(args || []);
  88. if (method.apply(plugin, params) === false) {
  89. return false;
  90. }
  91. }
  92. }
  93. return true;
  94. },
  95. descriptors: function descriptors(chart) {
  96. var cache = chart._plugins || (chart._plugins = {});
  97. if (cache.id === this._cacheId) {
  98. return cache.descriptors;
  99. }
  100. var plugins = [];
  101. var descriptors = [];
  102. this._plugins.concat(chart && chart.get('plugins') || []).forEach(function (plugin) {
  103. var idx = plugins.indexOf(plugin);
  104. if (idx !== -1) {
  105. return;
  106. }
  107. plugins.push(plugin);
  108. descriptors.push({
  109. plugin: plugin
  110. });
  111. });
  112. cache.descriptors = descriptors;
  113. cache.id = this._cacheId;
  114. return descriptors;
  115. }
  116. };
  117. };
  118. var _proto = Chart.prototype;
  119. _proto.getDefaultCfg = function getDefaultCfg() {
  120. return {
  121. /**
  122. * the id of canvas
  123. * @type {String}
  124. */
  125. id: null,
  126. /**
  127. * padding
  128. * @type {Array|Number}
  129. */
  130. padding: Global.padding,
  131. /**
  132. * data
  133. * @type {Array}
  134. */
  135. data: null,
  136. /**
  137. * scales of chart
  138. * @type {Object}
  139. */
  140. scales: {},
  141. /**
  142. * @private
  143. * geometry instances
  144. * @type {Array}
  145. */
  146. geoms: null,
  147. /**
  148. * scale configuration
  149. * @type {Object}
  150. */
  151. colDefs: null,
  152. pixelRatio: Global.pixelRatio,
  153. /**
  154. * filter options
  155. * @type {Object}
  156. */
  157. filters: null,
  158. appendPadding: Global.appendPadding
  159. };
  160. };
  161. _proto._syncYScales = function _syncYScales() {
  162. var geoms = this.get('geoms');
  163. var syncScales = [];
  164. var min = [];
  165. var max = [];
  166. Util.each(geoms, function (geom) {
  167. var yScale = geom.getYScale();
  168. if (yScale.isLinear) {
  169. syncScales.push(yScale);
  170. min.push(yScale.min);
  171. max.push(yScale.max);
  172. }
  173. });
  174. min = Math.min.apply(null, min);
  175. max = Math.max.apply(null, max);
  176. Util.each(syncScales, function (scale) {
  177. scale.change({
  178. min: min
  179. });
  180. scale.change({
  181. max: max
  182. });
  183. });
  184. };
  185. _proto._getFieldsForLegend = function _getFieldsForLegend() {
  186. var fields = [];
  187. var geoms = this.get('geoms');
  188. Util.each(geoms, function (geom) {
  189. var attrOptions = geom.get('attrOptions');
  190. var attrCfg = attrOptions.color;
  191. if (attrCfg && attrCfg.field && Util.isString(attrCfg.field)) {
  192. var arr = attrCfg.field.split('*');
  193. Util.each(arr, function (item) {
  194. if (fields.indexOf(item) === -1) {
  195. fields.push(item);
  196. }
  197. });
  198. }
  199. });
  200. return fields;
  201. };
  202. _proto._createScale = function _createScale(field, data) {
  203. var scaleController = this.get('scaleController');
  204. return scaleController.createScale(field, data);
  205. };
  206. _proto._adjustScale = function _adjustScale() {
  207. var self = this;
  208. var coord = self.get('coord');
  209. var xScale = self.getXScale();
  210. var yScales = self.getYScales();
  211. var scales = [];
  212. xScale && scales.push(xScale);
  213. scales = scales.concat(yScales);
  214. var inFullCircle = coord.isPolar && isFullCircle(coord);
  215. var scaleController = self.get('scaleController');
  216. var colDefs = scaleController.defs;
  217. Util.each(scales, function (scale) {
  218. if ((scale.isCategory || scale.isIdentity) && scale.values && !(colDefs[scale.field] && colDefs[scale.field].range)) {
  219. var count = scale.values.length;
  220. var range;
  221. if (count === 1) {
  222. range = [0.5, 1];
  223. } else {
  224. var widthRatio = 1;
  225. var offset = 0;
  226. if (inFullCircle) {
  227. if (!coord.transposed) {
  228. range = [0, 1 - 1 / count];
  229. } else {
  230. widthRatio = Global.widthRatio.multiplePie;
  231. offset = 1 / count * widthRatio;
  232. range = [offset / 2, 1 - offset / 2];
  233. }
  234. } else {
  235. offset = 1 / count * 1 / 2;
  236. range = [offset, 1 - offset];
  237. }
  238. }
  239. scale.range = range;
  240. }
  241. });
  242. var geoms = this.get('geoms');
  243. for (var i = 0; i < geoms.length; i++) {
  244. var geom = geoms[i];
  245. if (geom.get('type') === 'interval') {
  246. var yScale = geom.getYScale();
  247. var field = yScale.field,
  248. min = yScale.min,
  249. max = yScale.max,
  250. type = yScale.type;
  251. if (!(colDefs[field] && colDefs[field].min) && type !== 'time') {
  252. if (min > 0) {
  253. yScale.change({
  254. min: 0
  255. });
  256. } else if (max <= 0) {
  257. yScale.change({
  258. max: 0
  259. });
  260. }
  261. }
  262. }
  263. }
  264. };
  265. _proto._removeGeoms = function _removeGeoms() {
  266. var geoms = this.get('geoms');
  267. while (geoms.length > 0) {
  268. var geom = geoms.shift();
  269. geom.destroy();
  270. }
  271. };
  272. _proto._clearGeoms = function _clearGeoms() {
  273. var geoms = this.get('geoms');
  274. for (var i = 0, length = geoms.length; i < length; i++) {
  275. var geom = geoms[i];
  276. geom.clear();
  277. }
  278. };
  279. _proto._clearInner = function _clearInner() {
  280. this.set('scales', {});
  281. this.set('legendItems', null);
  282. this._clearGeoms();
  283. Chart.plugins.notify(this, 'clearInner');
  284. this.get('axisController') && this.get('axisController').clear();
  285. };
  286. _proto._execFilter = function _execFilter(data) {
  287. var filters = this.get('filters');
  288. if (filters) {
  289. data = data.filter(function (obj) {
  290. var rst = true;
  291. Util.each(filters, function (fn, k) {
  292. if (fn) {
  293. rst = fn(obj[k], obj);
  294. if (!rst) {
  295. return false;
  296. }
  297. }
  298. });
  299. return rst;
  300. });
  301. }
  302. return data;
  303. };
  304. _proto._initGeoms = function _initGeoms(geoms) {
  305. var coord = this.get('coord');
  306. var data = this.get('filteredData');
  307. var colDefs = this.get('colDefs');
  308. for (var i = 0, length = geoms.length; i < length; i++) {
  309. var geom = geoms[i];
  310. geom.set('data', data);
  311. geom.set('coord', coord);
  312. geom.set('colDefs', colDefs);
  313. geom.init();
  314. }
  315. };
  316. _proto._initCoord = function _initCoord() {
  317. var plot = this.get('plotRange');
  318. var coordCfg = Util.mix({
  319. type: 'cartesian'
  320. }, this.get('coordCfg'), {
  321. plot: plot
  322. });
  323. var type = coordCfg.type;
  324. var C = Coord[Util.upperFirst(type)];
  325. var coord = new C(coordCfg);
  326. this.set('coord', coord);
  327. };
  328. _proto._initLayout = function _initLayout() {
  329. var padding = this.get('_padding');
  330. if (!padding) {
  331. padding = this.get('margin') || this.get('padding');
  332. padding = Util.parsePadding(padding);
  333. }
  334. var top = padding[0] === 'auto' ? 0 : padding[0];
  335. var right = padding[1] === 'auto' ? 0 : padding[1];
  336. var bottom = padding[2] === 'auto' ? 0 : padding[2];
  337. var left = padding[3] === 'auto' ? 0 : padding[3];
  338. var width = this.get('width');
  339. var height = this.get('height');
  340. var plot = new Plot({
  341. start: {
  342. x: left,
  343. y: top
  344. },
  345. end: {
  346. x: width - right,
  347. y: height - bottom
  348. }
  349. });
  350. this.set('plotRange', plot);
  351. this.set('plot', plot);
  352. };
  353. _proto._initCanvas = function _initCanvas() {
  354. var self = this;
  355. try {
  356. var canvas = new Canvas({
  357. el: self.get('el') || self.get('id'),
  358. context: self.get('context'),
  359. pixelRatio: self.get('pixelRatio'),
  360. width: self.get('width'),
  361. height: self.get('height'),
  362. fontFamily: Global.fontFamily
  363. });
  364. self.set('canvas', canvas);
  365. self.set('width', canvas.get('width'));
  366. self.set('height', canvas.get('height'));
  367. } catch (error) {
  368. throw error;
  369. }
  370. Chart.plugins.notify(self, 'afterCanvasInit');
  371. self._initLayout();
  372. };
  373. _proto._initLayers = function _initLayers() {
  374. var canvas = this.get('canvas');
  375. this.set('backPlot', canvas.addGroup());
  376. this.set('middlePlot', canvas.addGroup({
  377. zIndex: 10
  378. }));
  379. this.set('frontPlot', canvas.addGroup({
  380. zIndex: 20
  381. }));
  382. };
  383. _proto._init = function _init() {
  384. var self = this;
  385. self._initCanvas();
  386. self._initLayers();
  387. self.set('geoms', []);
  388. self.set('scaleController', new ScaleController());
  389. self.set('axisController', new AxisController({
  390. frontPlot: self.get('frontPlot').addGroup({
  391. className: 'axisContainer'
  392. }),
  393. backPlot: self.get('backPlot').addGroup({
  394. className: 'axisContainer'
  395. }),
  396. chart: self
  397. }));
  398. Chart.plugins.notify(self, 'init');
  399. };
  400. function Chart(cfg) {
  401. var _this;
  402. _this = _Base.call(this, cfg) || this;
  403. var self = _assertThisInitialized(_assertThisInitialized(_this));
  404. Util.each(Geom, function (geomConstructor, className) {
  405. var methodName = Util.lowerFirst(className);
  406. self[methodName] = function (cfg) {
  407. var geom = new geomConstructor(cfg);
  408. self.addGeom(geom);
  409. return geom;
  410. };
  411. });
  412. self._init();
  413. return _this;
  414. }
  415. /**
  416. * set data and some scale configuration
  417. * @chainable
  418. * @param {Array} data the dataset to visualize
  419. * @param {Object} colDefs the configuration for scales
  420. * @return {Chart} return the chart instance
  421. */
  422. _proto.source = function source(data, colDefs) {
  423. this.set('data', data);
  424. if (colDefs) {
  425. this.scale(colDefs);
  426. }
  427. return this;
  428. };
  429. _proto.scale = function scale(field, cfg) {
  430. var colDefs = this.get('colDefs') || {};
  431. if (Util.isObject(field)) {
  432. Util.mix(colDefs, field);
  433. } else {
  434. colDefs[field] = cfg;
  435. }
  436. this.set('colDefs', colDefs);
  437. var scaleController = this.get('scaleController');
  438. scaleController.defs = colDefs;
  439. return this;
  440. };
  441. /**
  442. * configure the axis
  443. * @chainable
  444. * @param {String|Boolean} field the field name of data
  445. * @param {Object} cfg configuration for axis
  446. * @return {Chart} return the chart instance
  447. */
  448. _proto.axis = function axis(field, cfg) {
  449. var axisController = this.get('axisController');
  450. if (!field) {
  451. axisController.axisCfg = null;
  452. } else {
  453. axisController.axisCfg = axisController.axisCfg || {};
  454. axisController.axisCfg[field] = cfg;
  455. }
  456. return this;
  457. };
  458. /**
  459. * configure the coordinate
  460. * @chainable
  461. * @param {String} type set the type of coodinate
  462. * @param {Object} cfg configuration for coordinate
  463. * @return {Chart} return the chart instance
  464. */
  465. _proto.coord = function coord(type, cfg) {
  466. var coordCfg;
  467. if (Util.isObject(type)) {
  468. coordCfg = type;
  469. } else {
  470. coordCfg = cfg || {};
  471. coordCfg.type = type || 'cartesian';
  472. }
  473. this.set('coordCfg', coordCfg);
  474. return this;
  475. };
  476. _proto.filter = function filter(field, condition) {
  477. var filters = this.get('filters') || {};
  478. filters[field] = condition;
  479. this.set('filters', filters);
  480. };
  481. /**
  482. * render the chart
  483. * @chainable
  484. * @return {Chart} return the chart instance
  485. */
  486. _proto.render = function render() {
  487. var canvas = this.get('canvas');
  488. var geoms = this.get('geoms');
  489. var data = this.get('data') || [];
  490. var filteredData = this._execFilter(data); // filter data
  491. this.set('filteredData', filteredData);
  492. this._initCoord(); // initialization coordinate instance
  493. Chart.plugins.notify(this, 'beforeGeomInit');
  494. this._initGeoms(geoms); // init all geometry instances
  495. this.get('syncY') && this._syncYScales();
  496. this._adjustScale(); // do some adjust for data
  497. Chart.plugins.notify(this, 'beforeGeomDraw');
  498. this._renderAxis();
  499. var middlePlot = this.get('middlePlot');
  500. if (this.get('limitInPlot') && !middlePlot.attr('clip')) {
  501. var coord = this.get('coord');
  502. var clip = Helper.getClip(coord);
  503. clip.set('canvas', middlePlot.get('canvas'));
  504. middlePlot.attr('clip', clip);
  505. }
  506. for (var i = 0, length = geoms.length; i < length; i++) {
  507. var geom = geoms[i];
  508. geom.paint();
  509. }
  510. Chart.plugins.notify(this, 'afterGeomDraw');
  511. canvas.sort();
  512. this.get('frontPlot').sort();
  513. Chart.plugins.notify(this, 'beforeCanvasDraw');
  514. canvas.draw();
  515. return this;
  516. };
  517. /**
  518. * clear the chart, include geometris and all the shapes
  519. * @chainable
  520. * @return {Chart} return the chart
  521. */
  522. _proto.clear = function clear() {
  523. Chart.plugins.notify(this, 'clear');
  524. this._removeGeoms();
  525. this._clearInner();
  526. this.set('filters', null);
  527. this.set('isUpdate', false);
  528. this.set('_padding', null);
  529. var canvas = this.get('canvas');
  530. canvas.draw();
  531. return this;
  532. };
  533. _proto.repaint = function repaint() {
  534. this.set('isUpdate', true);
  535. Chart.plugins.notify(this, 'repaint');
  536. this._clearInner();
  537. this.render();
  538. };
  539. _proto.changeData = function changeData(data) {
  540. this.set('data', data);
  541. Chart.plugins.notify(this, 'changeData');
  542. this.set('_padding', null);
  543. this.repaint();
  544. };
  545. _proto.changeSize = function changeSize(width, height) {
  546. if (width) {
  547. this.set('width', width);
  548. } else {
  549. width = this.get('width');
  550. }
  551. if (height) {
  552. this.set('height', height);
  553. } else {
  554. height = this.get('height');
  555. }
  556. var canvas = this.get('canvas');
  557. canvas.changeSize(width, height);
  558. this._initLayout();
  559. this.repaint();
  560. return this;
  561. };
  562. _proto.destroy = function destroy() {
  563. this.clear();
  564. var canvas = this.get('canvas');
  565. canvas.destroy();
  566. Chart.plugins.notify(this, 'afterCanvasDestroyed');
  567. if (this._interactions) {
  568. Util.each(this._interactions, function (interaction) {
  569. interaction.destroy();
  570. });
  571. }
  572. _Base.prototype.destroy.call(this);
  573. };
  574. /**
  575. * calculate dataset's position on canvas
  576. * @param {Object} record the dataset
  577. * @return {Object} return the position
  578. */
  579. _proto.getPosition = function getPosition(record) {
  580. var self = this;
  581. var coord = self.get('coord');
  582. var xScale = self.getXScale();
  583. var yScale = self.getYScales()[0];
  584. var xField = xScale.field;
  585. var x = xScale.scale(record[xField]);
  586. var yField = yScale.field;
  587. var y = yScale.scale(record[yField]);
  588. return coord.convertPoint({
  589. x: x,
  590. y: y
  591. });
  592. };
  593. /**
  594. * get the data item of the point
  595. * @param {Object} point canvas position
  596. * @return {Object} return the data item
  597. */
  598. _proto.getRecord = function getRecord(point) {
  599. var self = this;
  600. var coord = self.get('coord');
  601. var xScale = self.getXScale();
  602. var yScale = self.getYScales()[0];
  603. var invertPoint = coord.invertPoint(point);
  604. var record = {};
  605. record[xScale.field] = xScale.invert(invertPoint.x);
  606. record[yScale.field] = yScale.invert(invertPoint.y);
  607. return record;
  608. };
  609. /**
  610. * get the dataset of the point
  611. * @param {Object} point canvas position
  612. * @return {Array} return the dataset
  613. **/
  614. _proto.getSnapRecords = function getSnapRecords(point) {
  615. var geom = this.get('geoms')[0];
  616. var data = [];
  617. if (geom) {
  618. // need to judge
  619. data = geom.getSnapRecords(point);
  620. }
  621. return data;
  622. };
  623. /**
  624. * creat scale instances
  625. * @param {String} field field name of data
  626. * @return {Scale} return the scale
  627. */
  628. _proto.createScale = function createScale(field) {
  629. var data = this.get('data');
  630. var filteredData = this.get('filteredData');
  631. if (filteredData.length) {
  632. var legendFields = this._getFieldsForLegend();
  633. if (legendFields.indexOf(field) === -1) {
  634. data = filteredData;
  635. }
  636. }
  637. var scales = this.get('scales');
  638. if (!scales[field]) {
  639. scales[field] = this._createScale(field, data);
  640. }
  641. return scales[field];
  642. };
  643. /**
  644. * @protected
  645. * add geometry instance to geoms
  646. * @param {Geom} geom geometry instance
  647. */
  648. _proto.addGeom = function addGeom(geom) {
  649. var geoms = this.get('geoms');
  650. var middlePlot = this.get('middlePlot');
  651. geoms.push(geom);
  652. geom.set('chart', this);
  653. geom.set('container', middlePlot.addGroup());
  654. };
  655. /**
  656. * get the scale of x axis
  657. * @return {Scale} return the scale
  658. */
  659. _proto.getXScale = function getXScale() {
  660. var self = this;
  661. var geoms = self.get('geoms');
  662. var xScale = geoms[0].getXScale();
  663. return xScale;
  664. };
  665. /**
  666. * get the scale of y axis
  667. * @return {Array} return the scale
  668. */
  669. _proto.getYScales = function getYScales() {
  670. var geoms = this.get('geoms');
  671. var rst = [];
  672. Util.each(geoms, function (geom) {
  673. var yScale = geom.getYScale();
  674. if (rst.indexOf(yScale) === -1) {
  675. rst.push(yScale);
  676. }
  677. });
  678. return rst;
  679. };
  680. _proto.getLegendItems = function getLegendItems() {
  681. if (this.get('legendItems')) {
  682. return this.get('legendItems');
  683. }
  684. var legendItems = {};
  685. var scales = [];
  686. var geoms = this.get('geoms');
  687. Util.each(geoms, function (geom) {
  688. var colorAttr = geom.getAttr('color');
  689. if (colorAttr) {
  690. var scale = colorAttr.getScale('color');
  691. if (scale.type !== 'identity' && !_isScaleExist(scales, scale)) {
  692. scales.push(scale);
  693. var field = scale.field;
  694. var ticks = scale.getTicks();
  695. var items = [];
  696. Util.each(ticks, function (tick) {
  697. var text = tick.text;
  698. var name = text;
  699. var scaleValue = tick.value;
  700. var value = scale.invert(scaleValue);
  701. var color = colorAttr.mapping(value).join('') || Global.defaultColor;
  702. var marker = {
  703. fill: color,
  704. radius: 3,
  705. symbol: 'circle',
  706. stroke: '#fff'
  707. };
  708. items.push({
  709. name: name,
  710. // for display
  711. dataValue: value,
  712. // the origin value
  713. checked: true,
  714. marker: marker
  715. });
  716. });
  717. legendItems[field] = items;
  718. }
  719. }
  720. });
  721. this.set('legendItems', legendItems);
  722. return legendItems;
  723. }; // register the plugins
  724. _proto.registerPlugins = function registerPlugins(plugins) {
  725. var self = this;
  726. var chartPlugins = self.get('plugins') || [];
  727. if (!Util.isArray(chartPlugins)) {
  728. chartPlugins = [chartPlugins];
  729. }
  730. [].concat(plugins).forEach(function (plugin) {
  731. if (chartPlugins.indexOf(plugin) === -1) {
  732. plugin.init && plugin.init(self); // init
  733. chartPlugins.push(plugin);
  734. }
  735. });
  736. Chart.plugins._cacheId++;
  737. self.set('plugins', chartPlugins);
  738. };
  739. _proto._renderAxis = function _renderAxis() {
  740. var axisController = this.get('axisController');
  741. var xScale = this.getXScale();
  742. var yScales = this.getYScales();
  743. var coord = this.get('coord');
  744. Chart.plugins.notify(this, 'beforeRenderAxis');
  745. axisController.createAxis(coord, xScale, yScales);
  746. };
  747. _proto._isAutoPadding = function _isAutoPadding() {
  748. if (this.get('_padding')) {
  749. return false;
  750. }
  751. var padding = this.get('padding');
  752. if (Util.isArray(padding)) {
  753. return padding.indexOf('auto') !== -1;
  754. }
  755. return padding === 'auto';
  756. };
  757. _proto._updateLayout = function _updateLayout(padding) {
  758. var width = this.get('width');
  759. var height = this.get('height');
  760. var start = {
  761. x: padding[3],
  762. y: padding[0]
  763. };
  764. var end = {
  765. x: width - padding[1],
  766. y: height - padding[2]
  767. };
  768. var plot = this.get('plot');
  769. var coord = this.get('coord');
  770. plot.reset(start, end);
  771. coord.reset(plot);
  772. };
  773. return Chart;
  774. }(Base);
  775. Chart.plugins = Chart.initPlugins();
  776. module.exports = Chart;