list.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. "use strict";
  2. var Util = require('../util/common');
  3. var _require = require('../graphic/index'),
  4. Group = _require.Group;
  5. var Marker = require('./marker');
  6. var MARKER_RADIUS = 3;
  7. var List = /*#__PURE__*/function () {
  8. var _proto = List.prototype;
  9. _proto.getDefaultCfg = function getDefaultCfg() {
  10. return {
  11. showTitle: false,
  12. /**
  13. * title string
  14. * @type {?String}
  15. */
  16. title: null,
  17. /**
  18. * items array
  19. * @type {?Array}
  20. */
  21. items: null,
  22. /**
  23. * offset between title and items
  24. * @type {Number}
  25. */
  26. titleGap: 12,
  27. /**
  28. * offset between each item
  29. * @type {Number}
  30. */
  31. itemGap: 10,
  32. /**
  33. * the offset between each item in vertical direaction
  34. * @type {Number}
  35. */
  36. itemMarginBottom: 12,
  37. /**
  38. * the formatter for item text
  39. * @type {[type]}
  40. */
  41. itemFormatter: null,
  42. itemWidth: null,
  43. /**
  44. * offset between marker and text
  45. * @type {Number}
  46. */
  47. wordSpace: 6,
  48. x: 0,
  49. y: 0,
  50. layout: 'horizontal',
  51. /**
  52. * the join string of `name` and `value`
  53. * @type {String}
  54. */
  55. joinString: ': '
  56. };
  57. };
  58. function List(cfg) {
  59. Util.deepMix(this, this.getDefaultCfg(), cfg);
  60. this._init();
  61. this._renderTitle();
  62. this._renderItems();
  63. }
  64. _proto._init = function _init() {
  65. var container = new Group({
  66. zIndex: this.zIndex || 0
  67. });
  68. this.container = container;
  69. var wrapper = container.addGroup();
  70. this.wrapper = wrapper;
  71. var itemsGroup = wrapper.addGroup({
  72. className: 'itemsGroup'
  73. });
  74. this.itemsGroup = itemsGroup;
  75. if (this.parent) {
  76. this.parent.add(container);
  77. }
  78. };
  79. _proto._renderTitle = function _renderTitle(title) {
  80. title = title || this.title;
  81. var titleShape = this.titleShape;
  82. var titleHeight = 0;
  83. if (this.showTitle && title) {
  84. if (titleShape && !titleShape.get('destroyed')) {
  85. titleShape.attr('text', title);
  86. } else {
  87. var wrapper = this.wrapper,
  88. titleStyle = this.titleStyle;
  89. titleShape = wrapper.addShape('text', {
  90. className: 'title',
  91. attrs: Util.mix({
  92. x: 0,
  93. y: 0,
  94. text: title
  95. }, titleStyle)
  96. });
  97. this.titleShape = titleShape;
  98. }
  99. titleHeight = titleShape.getBBox().height + this.titleGap;
  100. }
  101. this._titleHeight = titleHeight;
  102. };
  103. _proto._renderItems = function _renderItems(items) {
  104. var self = this;
  105. items = items || self.items;
  106. if (!items) {
  107. return;
  108. }
  109. if (self.reversed) {
  110. items.reverse();
  111. }
  112. Util.each(items, function (item, index) {
  113. self._addItem(item, index);
  114. });
  115. if (items.length > 1) {
  116. this._adjustItems();
  117. }
  118. this._renderBackground();
  119. };
  120. _proto._renderBackground = function _renderBackground() {
  121. var background = this.background;
  122. if (background) {
  123. var container = this.container;
  124. var wrapper = this.wrapper;
  125. var _wrapper$getBBox = wrapper.getBBox(),
  126. minX = _wrapper$getBBox.minX,
  127. minY = _wrapper$getBBox.minY,
  128. width = _wrapper$getBBox.width,
  129. height = _wrapper$getBBox.height;
  130. var padding = background.padding || [0, 0, 0, 0];
  131. padding = Util.parsePadding(padding);
  132. var attrs = Util.mix({
  133. x: minX - padding[3],
  134. y: minY - padding[0],
  135. width: width + padding[1] + padding[3],
  136. height: height + padding[0] + padding[2]
  137. }, background);
  138. var backShape = this.backShape;
  139. if (backShape) {
  140. backShape.attr(attrs);
  141. } else {
  142. backShape = container.addShape('Rect', {
  143. zIndex: -1,
  144. attrs: attrs
  145. });
  146. }
  147. this.backShape = backShape;
  148. container.sort();
  149. }
  150. };
  151. _proto._addItem = function _addItem(item) {
  152. var itemsGroup = this.itemsGroup;
  153. var itemGroup = itemsGroup.addGroup({
  154. name: item.name,
  155. value: item.value,
  156. dataValue: item.dataValue,
  157. checked: item.checked
  158. });
  159. var unCheckStyle = this.unCheckStyle,
  160. unCheckColor = this.unCheckColor,
  161. nameStyle = this.nameStyle,
  162. valueStyle = this.valueStyle,
  163. wordSpace = this.wordSpace;
  164. var marker = item.marker,
  165. value = item.value;
  166. var startX = 0;
  167. if (unCheckColor) {
  168. unCheckStyle.fill = unCheckColor;
  169. }
  170. if (marker) {
  171. var radius = marker.radius || MARKER_RADIUS;
  172. var markerAttrs = Util.mix({
  173. x: radius,
  174. y: this._titleHeight
  175. }, marker);
  176. if (item.checked === false) {
  177. Util.mix(markerAttrs, unCheckStyle);
  178. }
  179. var markerShape = new Marker({
  180. className: 'item-marker',
  181. attrs: markerAttrs
  182. });
  183. itemGroup.add(markerShape);
  184. startX += markerShape.getBBox().width + wordSpace;
  185. }
  186. var nameText;
  187. var name = item.name;
  188. if (name) {
  189. var joinString = this.joinString || '';
  190. name = value ? name + joinString : name;
  191. nameText = itemGroup.addShape('text', {
  192. className: 'name',
  193. attrs: Util.mix({
  194. x: startX,
  195. y: this._titleHeight,
  196. text: this._formatItemValue(name)
  197. }, nameStyle, item.checked === false ? unCheckStyle : null)
  198. });
  199. }
  200. if (value) {
  201. var valueX = startX;
  202. if (nameText) {
  203. valueX += nameText.getBBox().width;
  204. }
  205. itemGroup.addShape('text', {
  206. className: 'value',
  207. attrs: Util.mix({
  208. x: valueX,
  209. y: this._titleHeight,
  210. text: value
  211. }, valueStyle, item.checked === false ? unCheckStyle : null)
  212. });
  213. }
  214. return itemGroup;
  215. };
  216. _proto._formatItemValue = function _formatItemValue(value) {
  217. var formatter = this.itemFormatter;
  218. if (formatter) {
  219. value = formatter.call(this, value);
  220. }
  221. return value;
  222. };
  223. _proto._getMaxItemWidth = function _getMaxItemWidth() {
  224. var width;
  225. var itemWidth = this.itemWidth;
  226. if (Util.isNumber(itemWidth) || Util.isNil(itemWidth)) {
  227. return itemWidth;
  228. }
  229. if (itemWidth === 'auto') {
  230. var itemsGroup = this.itemsGroup;
  231. var children = itemsGroup.get('children');
  232. var count = children.length;
  233. var maxItemWidth = 0;
  234. for (var i = 0; i < count; i++) {
  235. var _children$i$getBBox = children[i].getBBox(),
  236. _width = _children$i$getBBox.width;
  237. maxItemWidth = Math.max(maxItemWidth, _width);
  238. }
  239. var maxLength = this.maxLength;
  240. var itemGap = this.itemGap;
  241. var twoAvgWidth = (maxLength - itemGap) / 2;
  242. var threeAvgWidth = (maxLength - itemGap * 2) / 3;
  243. if (count === 2) {
  244. width = Math.max(maxItemWidth, twoAvgWidth);
  245. } else {
  246. // 1. max <= 3Avg, 3Avg
  247. // 2. 3Avg < max && max < 2avg, 2avg
  248. // 3. max > 2avg, max, one column
  249. if (maxItemWidth <= threeAvgWidth) {
  250. width = threeAvgWidth;
  251. } else if (maxItemWidth <= twoAvgWidth) {
  252. width = twoAvgWidth;
  253. } else {
  254. width = maxItemWidth;
  255. }
  256. }
  257. return width;
  258. }
  259. };
  260. _proto._adjustHorizontal = function _adjustHorizontal() {
  261. var maxLength = this.maxLength,
  262. itemsGroup = this.itemsGroup;
  263. var children = itemsGroup.get('children');
  264. var itemGap = this.itemGap,
  265. itemMarginBottom = this.itemMarginBottom;
  266. var titleHeight = this._titleHeight;
  267. var row = 0;
  268. var rowWidth = 0;
  269. var width;
  270. var height;
  271. var itemWidth = this._getMaxItemWidth();
  272. var legendHitBoxes = [];
  273. for (var i = 0, len = children.length; i < len; i++) {
  274. var child = children[i];
  275. var box = child.getBBox();
  276. var childHeight = box.height;
  277. var childWidth = box.width;
  278. width = itemWidth || childWidth;
  279. height = childHeight + itemMarginBottom;
  280. if (width - (maxLength - rowWidth) > 0.0001) {
  281. row++;
  282. rowWidth = 0;
  283. }
  284. child.moveTo(rowWidth, row * height);
  285. legendHitBoxes.push({
  286. x: rowWidth,
  287. y: row * height + titleHeight - childHeight / 2,
  288. width: childWidth * 1.375,
  289. height: childHeight * 1.375
  290. });
  291. rowWidth += width + itemGap;
  292. }
  293. this.legendHitBoxes = legendHitBoxes;
  294. return;
  295. };
  296. _proto._adjustVertical = function _adjustVertical() {
  297. var maxLength = this.maxLength,
  298. itemsGroup = this.itemsGroup;
  299. var itemGap = this.itemGap,
  300. itemMarginBottom = this.itemMarginBottom,
  301. itemWidth = this.itemWidth;
  302. var titleHeight = this._titleHeight;
  303. var children = itemsGroup.get('children');
  304. var colHeight = 0;
  305. var width;
  306. var height;
  307. var maxItemWidth = 0;
  308. var totalWidth = 0;
  309. var legendHitBoxes = [];
  310. for (var i = 0, length = children.length; i < length; i++) {
  311. var child = children[i];
  312. var bbox = child.getBBox();
  313. width = bbox.width;
  314. height = bbox.height;
  315. if (Util.isNumber(itemWidth)) {
  316. maxItemWidth = itemWidth + itemGap;
  317. } else if (width > maxItemWidth) {
  318. maxItemWidth = width + itemGap;
  319. }
  320. if (maxLength - colHeight < height) {
  321. colHeight = 0;
  322. totalWidth += maxItemWidth;
  323. child.moveTo(totalWidth, 0);
  324. legendHitBoxes.push({
  325. x: totalWidth,
  326. y: titleHeight - height / 2,
  327. width: width * 1.375,
  328. height: height * 1.375
  329. });
  330. } else {
  331. child.moveTo(totalWidth, colHeight);
  332. legendHitBoxes.push({
  333. x: totalWidth,
  334. y: colHeight - height / 2 + titleHeight,
  335. width: width * 1.375,
  336. height: height * 1.375
  337. });
  338. }
  339. colHeight += height + itemMarginBottom;
  340. }
  341. this.legendHitBoxes = legendHitBoxes;
  342. return;
  343. };
  344. _proto._adjustItems = function _adjustItems() {
  345. var layout = this.layout;
  346. if (layout === 'horizontal') {
  347. this._adjustHorizontal();
  348. } else {
  349. this._adjustVertical();
  350. }
  351. };
  352. _proto.moveTo = function moveTo(x, y) {
  353. this.x = x;
  354. this.y = y;
  355. var container = this.container;
  356. container && container.moveTo(x, y);
  357. return this;
  358. };
  359. _proto.setItems = function setItems(items) {
  360. this.clearItems();
  361. this._renderItems(items);
  362. };
  363. _proto.setTitle = function setTitle(title) {
  364. this._renderTitle(title);
  365. };
  366. _proto.clearItems = function clearItems() {
  367. var itemsGroup = this.itemsGroup;
  368. itemsGroup.clear();
  369. };
  370. _proto.getWidth = function getWidth() {
  371. var container = this.container;
  372. var bbox = container.getBBox();
  373. return bbox.width;
  374. };
  375. _proto.getHeight = function getHeight() {
  376. var container = this.container;
  377. var bbox = container.getBBox();
  378. return bbox.height;
  379. };
  380. _proto.show = function show() {
  381. var container = this.container;
  382. container.show();
  383. };
  384. _proto.hide = function hide() {
  385. var container = this.container;
  386. container.hide();
  387. };
  388. _proto.clear = function clear() {
  389. var container = this.container;
  390. container.clear();
  391. container.remove(true);
  392. };
  393. return List;
  394. }();
  395. module.exports = List;