legend.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. var Util = require('../util/common');
  2. var List = require('../component/list');
  3. var Global = require('../global');
  4. var LEGEND_GAP = 12;
  5. var MARKER_SIZE = 3;
  6. var DEFAULT_CFG = {
  7. itemMarginBottom: 12,
  8. itemGap: 10,
  9. showTitle: false,
  10. titleStyle: {
  11. fontSize: 12,
  12. fill: '#808080',
  13. textAlign: 'start',
  14. textBaseline: 'top'
  15. },
  16. nameStyle: {
  17. fill: '#808080',
  18. fontSize: 12,
  19. textAlign: 'start',
  20. textBaseline: 'middle'
  21. },
  22. valueStyle: {
  23. fill: '#000000',
  24. fontSize: 12,
  25. textAlign: 'start',
  26. textBaseline: 'middle'
  27. },
  28. unCheckStyle: {
  29. fill: '#bfbfbf'
  30. },
  31. itemWidth: 'auto',
  32. wordSpace: 6,
  33. selectedMode: 'multiple' // 'multiple' or 'single'
  34. }; // Register the default configuration for Legend
  35. Global.legend = Util.deepMix({
  36. common: DEFAULT_CFG,
  37. // common legend configuration
  38. right: Util.mix({
  39. position: 'right',
  40. layout: 'vertical'
  41. }, DEFAULT_CFG),
  42. left: Util.mix({
  43. position: 'left',
  44. layout: 'vertical'
  45. }, DEFAULT_CFG),
  46. top: Util.mix({
  47. position: 'top',
  48. layout: 'horizontal'
  49. }, DEFAULT_CFG),
  50. bottom: Util.mix({
  51. position: 'bottom',
  52. layout: 'horizontal'
  53. }, DEFAULT_CFG)
  54. }, Global.legend || {});
  55. function getPaddingByPos(pos, appendPadding) {
  56. var padding = 0;
  57. appendPadding = Util.parsePadding(appendPadding);
  58. switch (pos) {
  59. case 'top':
  60. padding = appendPadding[0];
  61. break;
  62. case 'right':
  63. padding = appendPadding[1];
  64. break;
  65. case 'bottom':
  66. padding = appendPadding[2];
  67. break;
  68. case 'left':
  69. padding = appendPadding[3];
  70. break;
  71. default:
  72. break;
  73. }
  74. return padding;
  75. }
  76. var LegendController =
  77. /*#__PURE__*/
  78. function () {
  79. function LegendController(cfg) {
  80. this.legendCfg = {};
  81. this.enable = true;
  82. this.position = 'top';
  83. Util.mix(this, cfg);
  84. var chart = this.chart;
  85. this.canvasDom = chart.get('canvas').get('el');
  86. this.clear();
  87. }
  88. var _proto = LegendController.prototype;
  89. _proto.addLegend = function addLegend(scale, items, filterVals) {
  90. var self = this;
  91. var legendCfg = self.legendCfg;
  92. var field = scale.field;
  93. var fieldCfg = legendCfg[field];
  94. if (fieldCfg === false) {
  95. return null;
  96. }
  97. if (fieldCfg && fieldCfg.custom) {
  98. self.addCustomLegend(field);
  99. } else {
  100. var position = legendCfg.position || self.position;
  101. if (fieldCfg && fieldCfg.position) {
  102. position = fieldCfg.position;
  103. }
  104. if (scale.isCategory) {
  105. self._addCategoryLegend(scale, items, position, filterVals);
  106. }
  107. }
  108. };
  109. _proto.addCustomLegend = function addCustomLegend(field) {
  110. var self = this;
  111. var legendCfg = self.legendCfg;
  112. if (field && legendCfg[field]) {
  113. legendCfg = legendCfg[field];
  114. }
  115. var position = legendCfg.position || self.position;
  116. var legends = self.legends;
  117. legends[position] = legends[position] || [];
  118. var items = legendCfg.items;
  119. if (!items) {
  120. return null;
  121. }
  122. var container = self.container;
  123. Util.each(items, function (item) {
  124. if (!Util.isPlainObject(item.marker)) {
  125. item.marker = {
  126. symbol: item.marker || 'circle',
  127. fill: item.fill,
  128. radius: MARKER_SIZE
  129. };
  130. } else {
  131. item.marker.radius = item.marker.radius || MARKER_SIZE;
  132. }
  133. item.checked = Util.isNil(item.checked) ? true : item.checked;
  134. item.name = item.name || item.value;
  135. });
  136. var legend = new List(Util.deepMix({}, Global.legend[position], legendCfg, {
  137. maxLength: self._getMaxLength(position),
  138. items: items,
  139. parent: container
  140. }));
  141. legends[position].push(legend);
  142. };
  143. _proto.clear = function clear() {
  144. var legends = this.legends;
  145. Util.each(legends, function (legendItems) {
  146. Util.each(legendItems, function (legend) {
  147. legend.clear();
  148. });
  149. });
  150. this.legends = {};
  151. this.unBindEvents();
  152. };
  153. _proto._isFiltered = function _isFiltered(scale, values, value) {
  154. var rst = false;
  155. Util.each(values, function (val) {
  156. rst = rst || scale.getText(val) === scale.getText(value);
  157. if (rst) {
  158. return false;
  159. }
  160. });
  161. return rst;
  162. };
  163. _proto._getMaxLength = function _getMaxLength(position) {
  164. var chart = this.chart;
  165. var appendPadding = Util.parsePadding(chart.get('appendPadding'));
  166. return position === 'right' || position === 'left' ? chart.get('height') - (appendPadding[0] + appendPadding[2]) : chart.get('width') - (appendPadding[1] + appendPadding[3]);
  167. };
  168. _proto._addCategoryLegend = function _addCategoryLegend(scale, items, position, filterVals) {
  169. var self = this;
  170. var legendCfg = self.legendCfg,
  171. legends = self.legends,
  172. container = self.container,
  173. chart = self.chart;
  174. var field = scale.field;
  175. legends[position] = legends[position] || [];
  176. var symbol = 'circle';
  177. if (legendCfg[field] && legendCfg[field].marker) {
  178. symbol = legendCfg[field].marker;
  179. } else if (legendCfg.marker) {
  180. symbol = legendCfg.marker;
  181. }
  182. Util.each(items, function (item) {
  183. if (Util.isPlainObject(symbol)) {
  184. Util.mix(item.marker, symbol);
  185. } else {
  186. item.marker.symbol = symbol;
  187. }
  188. if (filterVals) {
  189. item.checked = self._isFiltered(scale, filterVals, item.dataValue);
  190. }
  191. });
  192. var legendItems = chart.get('legendItems');
  193. legendItems[field] = items;
  194. var lastCfg = Util.deepMix({}, Global.legend[position], legendCfg[field] || legendCfg, {
  195. maxLength: self._getMaxLength(position),
  196. items: items,
  197. field: field,
  198. filterVals: filterVals,
  199. parent: container
  200. });
  201. if (lastCfg.showTitle) {
  202. Util.deepMix(lastCfg, {
  203. title: scale.alias || scale.field
  204. });
  205. }
  206. var legend = new List(lastCfg);
  207. legends[position].push(legend);
  208. return legend;
  209. };
  210. _proto._alignLegend = function _alignLegend(legend, pre, position) {
  211. var self = this;
  212. var _self$plotRange = self.plotRange,
  213. tl = _self$plotRange.tl,
  214. bl = _self$plotRange.bl;
  215. var chart = self.chart;
  216. var offsetX = legend.offsetX || 0;
  217. var offsetY = legend.offsetY || 0;
  218. var chartWidth = chart.get('width');
  219. var chartHeight = chart.get('height');
  220. var appendPadding = Util.parsePadding(chart.get('appendPadding'));
  221. var legendHeight = legend.getHeight();
  222. var legendWidth = legend.getWidth();
  223. var x = 0;
  224. var y = 0;
  225. if (position === 'left' || position === 'right') {
  226. var verticalAlign = legend.verticalAlign || 'middle';
  227. var height = Math.abs(tl.y - bl.y);
  228. x = position === 'left' ? appendPadding[3] : chartWidth - legendWidth - appendPadding[1];
  229. y = (height - legendHeight) / 2 + tl.y;
  230. if (verticalAlign === 'top') {
  231. y = tl.y;
  232. } else if (verticalAlign === 'bottom') {
  233. y = bl.y - legendHeight;
  234. }
  235. if (pre) {
  236. y = pre.get('y') - legendHeight - LEGEND_GAP;
  237. }
  238. } else {
  239. var align = legend.align || 'left';
  240. x = appendPadding[3];
  241. if (align === 'center') {
  242. x = chartWidth / 2 - legendWidth / 2;
  243. } else if (align === 'right') {
  244. x = chartWidth - (legendWidth + appendPadding[1]);
  245. }
  246. y = position === 'top' ? appendPadding[0] + Math.abs(legend.container.getBBox().minY) : chartHeight - legendHeight;
  247. if (pre) {
  248. var preWidth = pre.getWidth();
  249. x = pre.x + preWidth + LEGEND_GAP;
  250. }
  251. }
  252. if (position === 'bottom' && offsetY > 0) {
  253. offsetY = 0;
  254. }
  255. if (position === 'right' && offsetX > 0) {
  256. offsetX = 0;
  257. }
  258. legend.moveTo(x + offsetX, y + offsetY);
  259. };
  260. _proto.alignLegends = function alignLegends() {
  261. var self = this;
  262. var legends = self.legends;
  263. Util.each(legends, function (legendItems, position) {
  264. Util.each(legendItems, function (legend, index) {
  265. var pre = legendItems[index - 1];
  266. self._alignLegend(legend, pre, position);
  267. });
  268. });
  269. return self;
  270. };
  271. _proto.handleEvent = function handleEvent(ev) {
  272. var self = this;
  273. function findItem(x, y) {
  274. var result = null;
  275. var legends = self.legends;
  276. Util.each(legends, function (legendItems) {
  277. Util.each(legendItems, function (legend) {
  278. var itemsGroup = legend.itemsGroup,
  279. legendHitBoxes = legend.legendHitBoxes;
  280. var children = itemsGroup.get('children');
  281. if (children.length) {
  282. var legendPosX = legend.x;
  283. var legendPosY = legend.y;
  284. Util.each(legendHitBoxes, function (box, index) {
  285. if (x >= box.x + legendPosX && x <= box.x + box.width + legendPosX && y >= box.y + legendPosY && y <= box.height + box.y + legendPosY) {
  286. // inbox
  287. result = {
  288. clickedItem: children[index],
  289. clickedLegend: legend
  290. };
  291. return false;
  292. }
  293. });
  294. }
  295. });
  296. });
  297. return result;
  298. }
  299. var chart = self.chart;
  300. var _Util$createEvent = Util.createEvent(ev, chart),
  301. x = _Util$createEvent.x,
  302. y = _Util$createEvent.y;
  303. var clicked = findItem(x, y);
  304. if (clicked && clicked.clickedLegend.clickable !== false) {
  305. var clickedItem = clicked.clickedItem,
  306. clickedLegend = clicked.clickedLegend;
  307. if (clickedLegend.onClick) {
  308. ev.clickedItem = clickedItem;
  309. clickedLegend.onClick(ev);
  310. } else if (!clickedLegend.custom) {
  311. var checked = clickedItem.get('checked');
  312. var value = clickedItem.get('dataValue');
  313. var filterVals = clickedLegend.filterVals,
  314. field = clickedLegend.field,
  315. selectedMode = clickedLegend.selectedMode;
  316. var isSingeSelected = selectedMode === 'single';
  317. if (isSingeSelected) {
  318. chart.filter(field, function (val) {
  319. return val === value;
  320. });
  321. } else {
  322. if (!checked) {
  323. filterVals.push(value);
  324. } else {
  325. Util.Array.remove(filterVals, value);
  326. }
  327. chart.filter(field, function (val) {
  328. return filterVals.indexOf(val) !== -1;
  329. });
  330. }
  331. chart.repaint();
  332. }
  333. }
  334. };
  335. _proto.bindEvents = function bindEvents() {
  336. var legendCfg = this.legendCfg;
  337. var triggerOn = legendCfg.triggerOn || 'touchstart';
  338. var method = Util.wrapBehavior(this, 'handleEvent');
  339. Util.addEventListener(this.canvasDom, triggerOn, method);
  340. };
  341. _proto.unBindEvents = function unBindEvents() {
  342. var legendCfg = this.legendCfg;
  343. var triggerOn = legendCfg.triggerOn || 'touchstart';
  344. var method = Util.getWrapBehavior(this, 'handleEvent');
  345. Util.removeEventListener(this.canvasDom, triggerOn, method);
  346. };
  347. return LegendController;
  348. }();
  349. module.exports = {
  350. init: function init(chart) {
  351. var legendController = new LegendController({
  352. container: chart.get('backPlot'),
  353. plotRange: chart.get('plotRange'),
  354. chart: chart
  355. });
  356. chart.set('legendController', legendController);
  357. chart.legend = function (field, cfg) {
  358. var legendCfg = legendController.legendCfg;
  359. legendController.enable = true;
  360. if (Util.isBoolean(field)) {
  361. legendController.enable = field;
  362. legendCfg = cfg || {};
  363. } else if (Util.isObject(field)) {
  364. legendCfg = field;
  365. } else {
  366. legendCfg[field] = cfg;
  367. }
  368. legendController.legendCfg = legendCfg;
  369. return this;
  370. };
  371. },
  372. beforeGeomDraw: function beforeGeomDraw(chart) {
  373. var legendController = chart.get('legendController');
  374. if (!legendController.enable) return null; // legend is not displayed
  375. var legendCfg = legendController.legendCfg;
  376. if (legendCfg && legendCfg.custom) {
  377. legendController.addCustomLegend();
  378. } else {
  379. var legendItems = chart.getLegendItems();
  380. var scales = chart.get('scales');
  381. var filters = chart.get('filters');
  382. Util.each(legendItems, function (items, field) {
  383. var scale = scales[field];
  384. var values = scale.values;
  385. var filterVals;
  386. if (filters && filters[field]) {
  387. filterVals = values.filter(filters[field]);
  388. } else {
  389. filterVals = values.slice(0);
  390. }
  391. legendController.addLegend(scale, items, filterVals);
  392. });
  393. }
  394. if (legendCfg && legendCfg.clickable !== false) {
  395. legendController.bindEvents();
  396. }
  397. var legends = legendController.legends;
  398. var legendRange = {
  399. top: 0,
  400. right: 0,
  401. bottom: 0,
  402. left: 0
  403. };
  404. Util.each(legends, function (legendItems, position) {
  405. var padding = 0;
  406. Util.each(legendItems, function (legend) {
  407. var width = legend.getWidth();
  408. var height = legend.getHeight();
  409. if (position === 'top' || position === 'bottom') {
  410. padding = Math.max(padding, height);
  411. if (legend.offsetY > 0) {
  412. padding += legend.offsetY;
  413. }
  414. } else {
  415. padding = Math.max(padding, width);
  416. if (legend.offsetX > 0) {
  417. padding += legend.offsetX;
  418. }
  419. }
  420. });
  421. legendRange[position] = padding + getPaddingByPos(position, chart.get('appendPadding'));
  422. });
  423. chart.set('legendRange', legendRange);
  424. },
  425. afterGeomDraw: function afterGeomDraw(chart) {
  426. var legendController = chart.get('legendController');
  427. legendController.alignLegends();
  428. },
  429. clearInner: function clearInner(chart) {
  430. var legendController = chart.get('legendController');
  431. legendController.clear();
  432. chart.set('legendRange', null);
  433. }
  434. };