chart.js 25 KB

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