Legend.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /* *
  2. *
  3. * (c) 2010-2021 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import A from './Animation/AnimationUtilities.js';
  12. var animObject = A.animObject, setAnimation = A.setAnimation;
  13. import H from './Globals.js';
  14. var isFirefox = H.isFirefox, marginNames = H.marginNames, win = H.win;
  15. import Point from './Series/Point.js';
  16. import U from './Utilities.js';
  17. var addEvent = U.addEvent, createElement = U.createElement, css = U.css, defined = U.defined, discardElement = U.discardElement, find = U.find, fireEvent = U.fireEvent, format = U.format, isNumber = U.isNumber, merge = U.merge, pick = U.pick, relativeLength = U.relativeLength, stableSort = U.stableSort, syncTimeout = U.syncTimeout, wrap = U.wrap;
  18. /**
  19. * Gets fired when the legend item belonging to a point is clicked. The default
  20. * action is to toggle the visibility of the point. This can be prevented by
  21. * returning `false` or calling `event.preventDefault()`.
  22. *
  23. * @callback Highcharts.PointLegendItemClickCallbackFunction
  24. *
  25. * @param {Highcharts.Point} this
  26. * The point on which the event occured.
  27. *
  28. * @param {Highcharts.PointLegendItemClickEventObject} event
  29. * The event that occured.
  30. */
  31. /**
  32. * Information about the legend click event.
  33. *
  34. * @interface Highcharts.PointLegendItemClickEventObject
  35. */ /**
  36. * Related browser event.
  37. * @name Highcharts.PointLegendItemClickEventObject#browserEvent
  38. * @type {Highcharts.PointerEvent}
  39. */ /**
  40. * Prevent the default action of toggle the visibility of the point.
  41. * @name Highcharts.PointLegendItemClickEventObject#preventDefault
  42. * @type {Function}
  43. */ /**
  44. * Related point.
  45. * @name Highcharts.PointLegendItemClickEventObject#target
  46. * @type {Highcharts.Point}
  47. */ /**
  48. * Event type.
  49. * @name Highcharts.PointLegendItemClickEventObject#type
  50. * @type {"legendItemClick"}
  51. */
  52. /**
  53. * Gets fired when the legend item belonging to a series is clicked. The default
  54. * action is to toggle the visibility of the series. This can be prevented by
  55. * returning `false` or calling `event.preventDefault()`.
  56. *
  57. * @callback Highcharts.SeriesLegendItemClickCallbackFunction
  58. *
  59. * @param {Highcharts.Series} this
  60. * The series where the event occured.
  61. *
  62. * @param {Highcharts.SeriesLegendItemClickEventObject} event
  63. * The event that occured.
  64. */
  65. /**
  66. * Information about the legend click event.
  67. *
  68. * @interface Highcharts.SeriesLegendItemClickEventObject
  69. */ /**
  70. * Related browser event.
  71. * @name Highcharts.SeriesLegendItemClickEventObject#browserEvent
  72. * @type {Highcharts.PointerEvent}
  73. */ /**
  74. * Prevent the default action of toggle the visibility of the series.
  75. * @name Highcharts.SeriesLegendItemClickEventObject#preventDefault
  76. * @type {Function}
  77. */ /**
  78. * Related series.
  79. * @name Highcharts.SeriesLegendItemClickEventObject#target
  80. * @type {Highcharts.Series}
  81. */ /**
  82. * Event type.
  83. * @name Highcharts.SeriesLegendItemClickEventObject#type
  84. * @type {"legendItemClick"}
  85. */
  86. /* eslint-disable no-invalid-this, valid-jsdoc */
  87. /**
  88. * The overview of the chart's series. The legend object is instanciated
  89. * internally in the chart constructor, and is available from the `chart.legend`
  90. * property. Each chart has only one legend.
  91. *
  92. * @class
  93. * @name Highcharts.Legend
  94. *
  95. * @param {Highcharts.Chart} chart
  96. * The chart instance.
  97. *
  98. * @param {Highcharts.LegendOptions} options
  99. * Legend options.
  100. */
  101. var Legend = /** @class */ (function () {
  102. /* *
  103. *
  104. * Constructors
  105. *
  106. * */
  107. function Legend(chart, options) {
  108. /* *
  109. *
  110. * Properties
  111. *
  112. * */
  113. this.allItems = [];
  114. this.box = void 0;
  115. this.contentGroup = void 0;
  116. this.display = false;
  117. this.group = void 0;
  118. this.initialItemY = 0;
  119. this.itemHeight = 0;
  120. this.itemMarginBottom = 0;
  121. this.itemMarginTop = 0;
  122. this.itemX = 0;
  123. this.itemY = 0;
  124. this.lastItemY = 0;
  125. this.lastLineHeight = 0;
  126. this.legendHeight = 0;
  127. this.legendWidth = 0;
  128. this.maxItemWidth = 0;
  129. this.maxLegendWidth = 0;
  130. this.offsetWidth = 0;
  131. this.options = {};
  132. this.padding = 0;
  133. this.pages = [];
  134. this.proximate = false;
  135. this.scrollGroup = void 0;
  136. this.symbolHeight = 0;
  137. this.symbolWidth = 0;
  138. this.titleHeight = 0;
  139. this.totalItemWidth = 0;
  140. this.widthOption = 0;
  141. this.chart = chart;
  142. this.init(chart, options);
  143. }
  144. /* *
  145. *
  146. * Functions
  147. *
  148. * */
  149. /**
  150. * Initialize the legend.
  151. *
  152. * @private
  153. * @function Highcharts.Legend#init
  154. *
  155. * @param {Highcharts.Chart} chart
  156. * The chart instance.
  157. *
  158. * @param {Highcharts.LegendOptions} options
  159. * Legend options.
  160. */
  161. Legend.prototype.init = function (chart, options) {
  162. /**
  163. * Chart of this legend.
  164. *
  165. * @readonly
  166. * @name Highcharts.Legend#chart
  167. * @type {Highcharts.Chart}
  168. */
  169. this.chart = chart;
  170. this.setOptions(options);
  171. if (options.enabled) {
  172. // Render it
  173. this.render();
  174. // move checkboxes
  175. addEvent(this.chart, 'endResize', function () {
  176. this.legend.positionCheckboxes();
  177. });
  178. if (this.proximate) {
  179. this.unchartrender = addEvent(this.chart, 'render', function () {
  180. this.legend.proximatePositions();
  181. this.legend.positionItems();
  182. });
  183. }
  184. else if (this.unchartrender) {
  185. this.unchartrender();
  186. }
  187. }
  188. };
  189. /**
  190. * @private
  191. * @function Highcharts.Legend#setOptions
  192. * @param {Highcharts.LegendOptions} options
  193. */
  194. Legend.prototype.setOptions = function (options) {
  195. var padding = pick(options.padding, 8);
  196. /**
  197. * Legend options.
  198. *
  199. * @readonly
  200. * @name Highcharts.Legend#options
  201. * @type {Highcharts.LegendOptions}
  202. */
  203. this.options = options;
  204. if (!this.chart.styledMode) {
  205. this.itemStyle = options.itemStyle;
  206. this.itemHiddenStyle = merge(this.itemStyle, options.itemHiddenStyle);
  207. }
  208. this.itemMarginTop = options.itemMarginTop || 0;
  209. this.itemMarginBottom = options.itemMarginBottom || 0;
  210. this.padding = padding;
  211. this.initialItemY = padding - 5; // 5 is pixels above the text
  212. this.symbolWidth = pick(options.symbolWidth, 16);
  213. this.pages = [];
  214. this.proximate = options.layout === 'proximate' && !this.chart.inverted;
  215. this.baseline = void 0; // #12705: baseline has to be reset on every update
  216. };
  217. /**
  218. * Update the legend with new options. Equivalent to running `chart.update`
  219. * with a legend configuration option.
  220. *
  221. * @sample highcharts/legend/legend-update/
  222. * Legend update
  223. *
  224. * @function Highcharts.Legend#update
  225. *
  226. * @param {Highcharts.LegendOptions} options
  227. * Legend options.
  228. *
  229. * @param {boolean} [redraw=true]
  230. * Whether to redraw the chart after the axis is altered. If doing more
  231. * operations on the chart, it is a good idea to set redraw to false and
  232. * call {@link Chart#redraw} after. Whether to redraw the chart.
  233. *
  234. * @fires Highcharts.Legends#event:afterUpdate
  235. */
  236. Legend.prototype.update = function (options, redraw) {
  237. var chart = this.chart;
  238. this.setOptions(merge(true, this.options, options));
  239. this.destroy();
  240. chart.isDirtyLegend = chart.isDirtyBox = true;
  241. if (pick(redraw, true)) {
  242. chart.redraw();
  243. }
  244. fireEvent(this, 'afterUpdate');
  245. };
  246. /**
  247. * Set the colors for the legend item.
  248. *
  249. * @private
  250. * @function Highcharts.Legend#colorizeItem
  251. * @param {Highcharts.BubbleLegend|Highcharts.Point|Highcharts.Series} item
  252. * A Series or Point instance
  253. * @param {boolean} [visible=false]
  254. * Dimmed or colored
  255. *
  256. * @todo
  257. * Make events official: Fires the event `afterColorizeItem`.
  258. */
  259. Legend.prototype.colorizeItem = function (item, visible) {
  260. item.legendGroup[visible ? 'removeClass' : 'addClass']('highcharts-legend-item-hidden');
  261. if (!this.chart.styledMode) {
  262. var legend = this, options = legend.options, legendItem = item.legendItem, legendLine = item.legendLine, legendSymbol = item.legendSymbol, hiddenColor = legend.itemHiddenStyle.color, textColor = visible ?
  263. options.itemStyle.color :
  264. hiddenColor, symbolColor = visible ?
  265. (item.color || hiddenColor) :
  266. hiddenColor, markerOptions = item.options && item.options.marker, symbolAttr = { fill: symbolColor };
  267. if (legendItem) {
  268. legendItem.css({
  269. fill: textColor,
  270. color: textColor // #1553, oldIE
  271. });
  272. }
  273. if (legendLine) {
  274. legendLine.attr({ stroke: symbolColor });
  275. }
  276. if (legendSymbol) {
  277. // Apply marker options
  278. if (markerOptions && legendSymbol.isMarker) { // #585
  279. symbolAttr = item.pointAttribs();
  280. if (!visible) {
  281. // #6769
  282. symbolAttr.stroke = symbolAttr.fill = hiddenColor;
  283. }
  284. }
  285. legendSymbol.attr(symbolAttr);
  286. }
  287. }
  288. fireEvent(this, 'afterColorizeItem', { item: item, visible: visible });
  289. };
  290. /**
  291. * @private
  292. * @function Highcharts.Legend#positionItems
  293. */
  294. Legend.prototype.positionItems = function () {
  295. // Now that the legend width and height are established, put the items
  296. // in the final position
  297. this.allItems.forEach(this.positionItem, this);
  298. if (!this.chart.isResizing) {
  299. this.positionCheckboxes();
  300. }
  301. };
  302. /**
  303. * Position the legend item.
  304. *
  305. * @private
  306. * @function Highcharts.Legend#positionItem
  307. * @param {Highcharts.BubbleLegend|Highcharts.Point|Highcharts.Series} item
  308. * The item to position
  309. */
  310. Legend.prototype.positionItem = function (item) {
  311. var _this = this;
  312. var legend = this, options = legend.options, symbolPadding = options.symbolPadding, ltr = !options.rtl, legendItemPos = item._legendItemPos, itemX = legendItemPos[0], itemY = legendItemPos[1], checkbox = item.checkbox, legendGroup = item.legendGroup;
  313. if (legendGroup && legendGroup.element) {
  314. var attribs = {
  315. translateX: ltr ?
  316. itemX :
  317. legend.legendWidth - itemX - 2 * symbolPadding - 4,
  318. translateY: itemY
  319. };
  320. var complete = function () {
  321. fireEvent(_this, 'afterPositionItem', { item: item });
  322. };
  323. if (defined(legendGroup.translateY)) {
  324. legendGroup.animate(attribs, void 0, complete);
  325. }
  326. else {
  327. legendGroup.attr(attribs);
  328. complete();
  329. }
  330. }
  331. if (checkbox) {
  332. checkbox.x = itemX;
  333. checkbox.y = itemY;
  334. }
  335. };
  336. /**
  337. * Destroy a single legend item, used internally on removing series items.
  338. *
  339. * @private
  340. * @function Highcharts.Legend#destroyItem
  341. * @param {Highcharts.BubbleLegend|Highcharts.Point|Highcharts.Series} item
  342. * The item to remove
  343. */
  344. Legend.prototype.destroyItem = function (item) {
  345. var checkbox = item.checkbox;
  346. // destroy SVG elements
  347. ['legendItem', 'legendLine', 'legendSymbol', 'legendGroup'].forEach(function (key) {
  348. if (item[key]) {
  349. item[key] = item[key].destroy();
  350. }
  351. });
  352. if (checkbox) {
  353. discardElement(item.checkbox);
  354. }
  355. };
  356. /**
  357. * Destroy the legend. Used internally. To reflow objects, `chart.redraw`
  358. * must be called after destruction.
  359. *
  360. * @private
  361. * @function Highcharts.Legend#destroy
  362. */
  363. Legend.prototype.destroy = function () {
  364. /**
  365. * @private
  366. * @param {string} key
  367. * @return {void}
  368. */
  369. function destroyItems(key) {
  370. if (this[key]) {
  371. this[key] = this[key].destroy();
  372. }
  373. }
  374. // Destroy items
  375. this.getAllItems().forEach(function (item) {
  376. ['legendItem', 'legendGroup'].forEach(destroyItems, item);
  377. });
  378. // Destroy legend elements
  379. [
  380. 'clipRect',
  381. 'up',
  382. 'down',
  383. 'pager',
  384. 'nav',
  385. 'box',
  386. 'title',
  387. 'group'
  388. ].forEach(destroyItems, this);
  389. this.display = null; // Reset in .render on update.
  390. };
  391. /**
  392. * Position the checkboxes after the width is determined.
  393. *
  394. * @private
  395. * @function Highcharts.Legend#positionCheckboxes
  396. */
  397. Legend.prototype.positionCheckboxes = function () {
  398. var alignAttr = this.group && this.group.alignAttr, translateY, clipHeight = this.clipHeight || this.legendHeight, titleHeight = this.titleHeight;
  399. if (alignAttr) {
  400. translateY = alignAttr.translateY;
  401. this.allItems.forEach(function (item) {
  402. var checkbox = item.checkbox, top;
  403. if (checkbox) {
  404. top = translateY + titleHeight + checkbox.y +
  405. (this.scrollOffset || 0) + 3;
  406. css(checkbox, {
  407. left: (alignAttr.translateX + item.checkboxOffset +
  408. checkbox.x - 20) + 'px',
  409. top: top + 'px',
  410. display: this.proximate || (top > translateY - 6 &&
  411. top < translateY + clipHeight - 6) ?
  412. '' :
  413. 'none'
  414. });
  415. }
  416. }, this);
  417. }
  418. };
  419. /**
  420. * Render the legend title on top of the legend.
  421. *
  422. * @private
  423. * @function Highcharts.Legend#renderTitle
  424. */
  425. Legend.prototype.renderTitle = function () {
  426. var options = this.options, padding = this.padding, titleOptions = options.title, titleHeight = 0, bBox;
  427. if (titleOptions.text) {
  428. if (!this.title) {
  429. /**
  430. * SVG element of the legend title.
  431. *
  432. * @readonly
  433. * @name Highcharts.Legend#title
  434. * @type {Highcharts.SVGElement}
  435. */
  436. this.title = this.chart.renderer.label(titleOptions.text, padding - 3, padding - 4, null, null, null, options.useHTML, null, 'legend-title')
  437. .attr({ zIndex: 1 });
  438. if (!this.chart.styledMode) {
  439. this.title.css(titleOptions.style);
  440. }
  441. this.title.add(this.group);
  442. }
  443. // Set the max title width (#7253)
  444. if (!titleOptions.width) {
  445. this.title.css({
  446. width: this.maxLegendWidth + 'px'
  447. });
  448. }
  449. bBox = this.title.getBBox();
  450. titleHeight = bBox.height;
  451. this.offsetWidth = bBox.width; // #1717
  452. this.contentGroup.attr({ translateY: titleHeight });
  453. }
  454. this.titleHeight = titleHeight;
  455. };
  456. /**
  457. * Set the legend item text.
  458. *
  459. * @function Highcharts.Legend#setText
  460. * @param {Highcharts.Point|Highcharts.Series} item
  461. * The item for which to update the text in the legend.
  462. */
  463. Legend.prototype.setText = function (item) {
  464. var options = this.options;
  465. item.legendItem.attr({
  466. text: options.labelFormat ?
  467. format(options.labelFormat, item, this.chart) :
  468. options.labelFormatter.call(item)
  469. });
  470. };
  471. /**
  472. * Render a single specific legend item. Called internally from the `render`
  473. * function.
  474. *
  475. * @private
  476. * @function Highcharts.Legend#renderItem
  477. * @param {Highcharts.BubbleLegend|Highcharts.Point|Highcharts.Series} item
  478. * The item to render.
  479. */
  480. Legend.prototype.renderItem = function (item) {
  481. var legend = this, chart = legend.chart, renderer = chart.renderer, options = legend.options, horizontal = options.layout === 'horizontal', symbolWidth = legend.symbolWidth, symbolPadding = options.symbolPadding, itemStyle = legend.itemStyle, itemHiddenStyle = legend.itemHiddenStyle, itemDistance = horizontal ? pick(options.itemDistance, 20) : 0, ltr = !options.rtl, bBox, li = item.legendItem, isSeries = !item.series, series = !isSeries && item.series.drawLegendSymbol ?
  482. item.series :
  483. item, seriesOptions = series.options, showCheckbox = legend.createCheckboxForItem &&
  484. seriesOptions &&
  485. seriesOptions.showCheckbox,
  486. // full width minus text width
  487. itemExtraWidth = symbolWidth + symbolPadding +
  488. itemDistance + (showCheckbox ? 20 : 0), useHTML = options.useHTML, itemClassName = item.options.className;
  489. if (!li) { // generate it once, later move it
  490. // Generate the group box, a group to hold the symbol and text. Text
  491. // is to be appended in Legend class.
  492. item.legendGroup = renderer
  493. .g('legend-item')
  494. .addClass('highcharts-' + series.type + '-series ' +
  495. 'highcharts-color-' + item.colorIndex +
  496. (itemClassName ? ' ' + itemClassName : '') +
  497. (isSeries ?
  498. ' highcharts-series-' + item.index :
  499. ''))
  500. .attr({ zIndex: 1 })
  501. .add(legend.scrollGroup);
  502. // Generate the list item text and add it to the group
  503. item.legendItem = li = renderer.text('', ltr ?
  504. symbolWidth + symbolPadding :
  505. -symbolPadding, legend.baseline || 0, useHTML);
  506. if (!chart.styledMode) {
  507. // merge to prevent modifying original (#1021)
  508. li.css(merge(item.visible ?
  509. itemStyle :
  510. itemHiddenStyle));
  511. }
  512. li
  513. .attr({
  514. align: ltr ? 'left' : 'right',
  515. zIndex: 2
  516. })
  517. .add(item.legendGroup);
  518. // Get the baseline for the first item - the font size is equal for
  519. // all
  520. if (!legend.baseline) {
  521. legend.fontMetrics = renderer.fontMetrics(chart.styledMode ? 12 : itemStyle.fontSize, li);
  522. legend.baseline =
  523. legend.fontMetrics.f + 3 + legend.itemMarginTop;
  524. li.attr('y', legend.baseline);
  525. }
  526. // Draw the legend symbol inside the group box
  527. legend.symbolHeight =
  528. options.symbolHeight || legend.fontMetrics.f;
  529. series.drawLegendSymbol(legend, item);
  530. if (legend.setItemEvents) {
  531. legend.setItemEvents(item, li, useHTML);
  532. }
  533. }
  534. // Add the HTML checkbox on top
  535. if (showCheckbox && !item.checkbox && legend.createCheckboxForItem) {
  536. legend.createCheckboxForItem(item);
  537. }
  538. // Colorize the items
  539. legend.colorizeItem(item, item.visible);
  540. // Take care of max width and text overflow (#6659)
  541. if (chart.styledMode || !itemStyle.width) {
  542. li.css({
  543. width: ((options.itemWidth ||
  544. legend.widthOption ||
  545. chart.spacingBox.width) - itemExtraWidth) + 'px'
  546. });
  547. }
  548. // Always update the text
  549. legend.setText(item);
  550. // calculate the positions for the next line
  551. bBox = li.getBBox();
  552. item.itemWidth = item.checkboxOffset =
  553. options.itemWidth ||
  554. item.legendItemWidth ||
  555. bBox.width + itemExtraWidth;
  556. legend.maxItemWidth = Math.max(legend.maxItemWidth, item.itemWidth);
  557. legend.totalItemWidth += item.itemWidth;
  558. legend.itemHeight = item.itemHeight = Math.round(item.legendItemHeight || bBox.height || legend.symbolHeight);
  559. };
  560. /**
  561. * Get the position of the item in the layout. We now know the
  562. * maxItemWidth from the previous loop.
  563. *
  564. * @private
  565. * @function Highcharts.Legend#layoutItem
  566. * @param {Highcharts.BubbleLegend|Highcharts.Point|Highcharts.Series} item
  567. */
  568. Legend.prototype.layoutItem = function (item) {
  569. var options = this.options, padding = this.padding, horizontal = options.layout === 'horizontal', itemHeight = item.itemHeight, itemMarginBottom = this.itemMarginBottom, itemMarginTop = this.itemMarginTop, itemDistance = horizontal ? pick(options.itemDistance, 20) : 0, maxLegendWidth = this.maxLegendWidth, itemWidth = (options.alignColumns &&
  570. this.totalItemWidth > maxLegendWidth) ?
  571. this.maxItemWidth :
  572. item.itemWidth;
  573. // If the item exceeds the width, start a new line
  574. if (horizontal &&
  575. this.itemX - padding + itemWidth > maxLegendWidth) {
  576. this.itemX = padding;
  577. if (this.lastLineHeight) { // Not for the first line (#10167)
  578. this.itemY += (itemMarginTop +
  579. this.lastLineHeight +
  580. itemMarginBottom);
  581. }
  582. this.lastLineHeight = 0; // reset for next line (#915, #3976)
  583. }
  584. // Set the edge positions
  585. this.lastItemY = itemMarginTop + this.itemY + itemMarginBottom;
  586. this.lastLineHeight = Math.max(// #915
  587. itemHeight, this.lastLineHeight);
  588. // cache the position of the newly generated or reordered items
  589. item._legendItemPos = [this.itemX, this.itemY];
  590. // advance
  591. if (horizontal) {
  592. this.itemX += itemWidth;
  593. }
  594. else {
  595. this.itemY +=
  596. itemMarginTop + itemHeight + itemMarginBottom;
  597. this.lastLineHeight = itemHeight;
  598. }
  599. // the width of the widest item
  600. this.offsetWidth = this.widthOption || Math.max((horizontal ? this.itemX - padding - (item.checkbox ?
  601. // decrease by itemDistance only when no checkbox #4853
  602. 0 :
  603. itemDistance) : itemWidth) + padding, this.offsetWidth);
  604. };
  605. /**
  606. * Get all items, which is one item per series for most series and one
  607. * item per point for pie series and its derivatives. Fires the event
  608. * `afterGetAllItems`.
  609. *
  610. * @private
  611. * @function Highcharts.Legend#getAllItems
  612. * @return {Array<(Highcharts.BubbleLegend|Highcharts.Point|Highcharts.Series)>}
  613. * The current items in the legend.
  614. * @fires Highcharts.Legend#event:afterGetAllItems
  615. */
  616. Legend.prototype.getAllItems = function () {
  617. var allItems = [];
  618. this.chart.series.forEach(function (series) {
  619. var seriesOptions = series && series.options;
  620. // Handle showInLegend. If the series is linked to another series,
  621. // defaults to false.
  622. if (series && pick(seriesOptions.showInLegend, !defined(seriesOptions.linkedTo) ? void 0 : false, true)) {
  623. // Use points or series for the legend item depending on
  624. // legendType
  625. allItems = allItems.concat(series.legendItems ||
  626. (seriesOptions.legendType === 'point' ?
  627. series.data :
  628. series));
  629. }
  630. });
  631. fireEvent(this, 'afterGetAllItems', { allItems: allItems });
  632. return allItems;
  633. };
  634. /**
  635. * Get a short, three letter string reflecting the alignment and layout.
  636. *
  637. * @private
  638. * @function Highcharts.Legend#getAlignment
  639. * @return {string}
  640. * The alignment, empty string if floating
  641. */
  642. Legend.prototype.getAlignment = function () {
  643. var options = this.options;
  644. // Use the first letter of each alignment option in order to detect
  645. // the side. (#4189 - use charAt(x) notation instead of [x] for IE7)
  646. if (this.proximate) {
  647. return options.align.charAt(0) + 'tv';
  648. }
  649. return options.floating ? '' : (options.align.charAt(0) +
  650. options.verticalAlign.charAt(0) +
  651. options.layout.charAt(0));
  652. };
  653. /**
  654. * Adjust the chart margins by reserving space for the legend on only one
  655. * side of the chart. If the position is set to a corner, top or bottom is
  656. * reserved for horizontal legends and left or right for vertical ones.
  657. *
  658. * @private
  659. * @function Highcharts.Legend#adjustMargins
  660. * @param {Array<number>} margin
  661. * @param {Array<number>} spacing
  662. */
  663. Legend.prototype.adjustMargins = function (margin, spacing) {
  664. var chart = this.chart, options = this.options, alignment = this.getAlignment();
  665. if (alignment) {
  666. ([
  667. /(lth|ct|rth)/,
  668. /(rtv|rm|rbv)/,
  669. /(rbh|cb|lbh)/,
  670. /(lbv|lm|ltv)/
  671. ]).forEach(function (alignments, side) {
  672. if (alignments.test(alignment) && !defined(margin[side])) {
  673. // Now we have detected on which side of the chart we should
  674. // reserve space for the legend
  675. chart[marginNames[side]] = Math.max(chart[marginNames[side]], (chart.legend[(side + 1) % 2 ? 'legendHeight' : 'legendWidth'] +
  676. [1, -1, -1, 1][side] * options[(side % 2) ? 'x' : 'y'] +
  677. pick(options.margin, 12) +
  678. spacing[side] +
  679. (chart.titleOffset[side] || 0)));
  680. }
  681. });
  682. }
  683. };
  684. /**
  685. * @private
  686. * @function Highcharts.Legend#proximatePositions
  687. */
  688. Legend.prototype.proximatePositions = function () {
  689. var chart = this.chart, boxes = [], alignLeft = this.options.align === 'left';
  690. this.allItems.forEach(function (item) {
  691. var lastPoint, height, useFirstPoint = alignLeft, target, top;
  692. if (item.yAxis) {
  693. if (item.xAxis.options.reversed) {
  694. useFirstPoint = !useFirstPoint;
  695. }
  696. if (item.points) {
  697. lastPoint = find(useFirstPoint ?
  698. item.points :
  699. item.points.slice(0).reverse(), function (item) {
  700. return isNumber(item.plotY);
  701. });
  702. }
  703. height = this.itemMarginTop +
  704. item.legendItem.getBBox().height +
  705. this.itemMarginBottom;
  706. top = item.yAxis.top - chart.plotTop;
  707. if (item.visible) {
  708. target = lastPoint ?
  709. lastPoint.plotY :
  710. item.yAxis.height;
  711. target += top - 0.3 * height;
  712. }
  713. else {
  714. target = top + item.yAxis.height;
  715. }
  716. boxes.push({
  717. target: target,
  718. size: height,
  719. item: item
  720. });
  721. }
  722. }, this);
  723. H.distribute(boxes, chart.plotHeight);
  724. boxes.forEach(function (box) {
  725. box.item._legendItemPos[1] =
  726. chart.plotTop - chart.spacing[0] + box.pos;
  727. });
  728. };
  729. /**
  730. * Render the legend. This method can be called both before and after
  731. * `chart.render`. If called after, it will only rearrange items instead
  732. * of creating new ones. Called internally on initial render and after
  733. * redraws.
  734. *
  735. * @private
  736. * @function Highcharts.Legend#render
  737. */
  738. Legend.prototype.render = function () {
  739. var legend = this, chart = legend.chart, renderer = chart.renderer, legendGroup = legend.group, allItems, display, legendWidth, legendHeight, box = legend.box, options = legend.options, padding = legend.padding, allowedWidth;
  740. legend.itemX = padding;
  741. legend.itemY = legend.initialItemY;
  742. legend.offsetWidth = 0;
  743. legend.lastItemY = 0;
  744. legend.widthOption = relativeLength(options.width, chart.spacingBox.width - padding);
  745. // Compute how wide the legend is allowed to be
  746. allowedWidth =
  747. chart.spacingBox.width - 2 * padding - options.x;
  748. if (['rm', 'lm'].indexOf(legend.getAlignment().substring(0, 2)) > -1) {
  749. allowedWidth /= 2;
  750. }
  751. legend.maxLegendWidth = legend.widthOption || allowedWidth;
  752. if (!legendGroup) {
  753. /**
  754. * SVG group of the legend.
  755. *
  756. * @readonly
  757. * @name Highcharts.Legend#group
  758. * @type {Highcharts.SVGElement}
  759. */
  760. legend.group = legendGroup = renderer.g('legend')
  761. .attr({ zIndex: 7 })
  762. .add();
  763. legend.contentGroup = renderer.g()
  764. .attr({ zIndex: 1 }) // above background
  765. .add(legendGroup);
  766. legend.scrollGroup = renderer.g()
  767. .add(legend.contentGroup);
  768. }
  769. legend.renderTitle();
  770. // add each series or point
  771. allItems = legend.getAllItems();
  772. // sort by legendIndex
  773. stableSort(allItems, function (a, b) {
  774. return ((a.options && a.options.legendIndex) || 0) -
  775. ((b.options && b.options.legendIndex) || 0);
  776. });
  777. // reversed legend
  778. if (options.reversed) {
  779. allItems.reverse();
  780. }
  781. /**
  782. * All items for the legend, which is an array of series for most series
  783. * and an array of points for pie series and its derivatives.
  784. *
  785. * @readonly
  786. * @name Highcharts.Legend#allItems
  787. * @type {Array<(Highcharts.Point|Highcharts.Series)>}
  788. */
  789. legend.allItems = allItems;
  790. legend.display = display = !!allItems.length;
  791. // Render the items. First we run a loop to set the text and properties
  792. // and read all the bounding boxes. The next loop computes the item
  793. // positions based on the bounding boxes.
  794. legend.lastLineHeight = 0;
  795. legend.maxItemWidth = 0;
  796. legend.totalItemWidth = 0;
  797. legend.itemHeight = 0;
  798. allItems.forEach(legend.renderItem, legend);
  799. allItems.forEach(legend.layoutItem, legend);
  800. // Get the box
  801. legendWidth = (legend.widthOption || legend.offsetWidth) + padding;
  802. legendHeight = legend.lastItemY + legend.lastLineHeight +
  803. legend.titleHeight;
  804. legendHeight = legend.handleOverflow(legendHeight);
  805. legendHeight += padding;
  806. // Draw the border and/or background
  807. if (!box) {
  808. /**
  809. * SVG element of the legend box.
  810. *
  811. * @readonly
  812. * @name Highcharts.Legend#box
  813. * @type {Highcharts.SVGElement}
  814. */
  815. legend.box = box = renderer.rect()
  816. .addClass('highcharts-legend-box')
  817. .attr({
  818. r: options.borderRadius
  819. })
  820. .add(legendGroup);
  821. box.isNew = true;
  822. }
  823. // Presentational
  824. if (!chart.styledMode) {
  825. box
  826. .attr({
  827. stroke: options.borderColor,
  828. 'stroke-width': options.borderWidth || 0,
  829. fill: options.backgroundColor || 'none'
  830. })
  831. .shadow(options.shadow);
  832. }
  833. if (legendWidth > 0 && legendHeight > 0) {
  834. box[box.isNew ? 'attr' : 'animate'](box.crisp.call({}, {
  835. x: 0,
  836. y: 0,
  837. width: legendWidth,
  838. height: legendHeight
  839. }, box.strokeWidth()));
  840. box.isNew = false;
  841. }
  842. // hide the border if no items
  843. box[display ? 'show' : 'hide']();
  844. // Open for responsiveness
  845. if (chart.styledMode && legendGroup.getStyle('display') === 'none') {
  846. legendWidth = legendHeight = 0;
  847. }
  848. legend.legendWidth = legendWidth;
  849. legend.legendHeight = legendHeight;
  850. if (display) {
  851. legend.align();
  852. }
  853. if (!this.proximate) {
  854. this.positionItems();
  855. }
  856. fireEvent(this, 'afterRender');
  857. };
  858. /**
  859. * Align the legend to chart's box.
  860. *
  861. * @private
  862. * @function Highcharts.align
  863. * @param {Highcharts.BBoxObject} alignTo
  864. * @return {void}
  865. */
  866. Legend.prototype.align = function (alignTo) {
  867. if (alignTo === void 0) { alignTo = this.chart.spacingBox; }
  868. var chart = this.chart, options = this.options;
  869. // If aligning to the top and the layout is horizontal, adjust for
  870. // the title (#7428)
  871. var y = alignTo.y;
  872. if (/(lth|ct|rth)/.test(this.getAlignment()) &&
  873. chart.titleOffset[0] > 0) {
  874. y += chart.titleOffset[0];
  875. }
  876. else if (/(lbh|cb|rbh)/.test(this.getAlignment()) &&
  877. chart.titleOffset[2] > 0) {
  878. y -= chart.titleOffset[2];
  879. }
  880. if (y !== alignTo.y) {
  881. alignTo = merge(alignTo, { y: y });
  882. }
  883. this.group.align(merge(options, {
  884. width: this.legendWidth,
  885. height: this.legendHeight,
  886. verticalAlign: this.proximate ? 'top' : options.verticalAlign
  887. }), true, alignTo);
  888. };
  889. /**
  890. * Set up the overflow handling by adding navigation with up and down arrows
  891. * below the legend.
  892. *
  893. * @private
  894. * @function Highcharts.Legend#handleOverflow
  895. * @param {number} legendHeight
  896. * @return {number}
  897. */
  898. Legend.prototype.handleOverflow = function (legendHeight) {
  899. var legend = this, chart = this.chart, renderer = chart.renderer, options = this.options, optionsY = options.y, alignTop = options.verticalAlign === 'top', padding = this.padding, spaceHeight = (chart.spacingBox.height +
  900. (alignTop ? -optionsY : optionsY) - padding), maxHeight = options.maxHeight, clipHeight, clipRect = this.clipRect, navOptions = options.navigation, animation = pick(navOptions.animation, true), arrowSize = navOptions.arrowSize || 12, nav = this.nav, pages = this.pages, lastY, allItems = this.allItems, clipToHeight = function (height) {
  901. if (typeof height === 'number') {
  902. clipRect.attr({
  903. height: height
  904. });
  905. }
  906. else if (clipRect) { // Reset (#5912)
  907. legend.clipRect = clipRect.destroy();
  908. legend.contentGroup.clip();
  909. }
  910. // useHTML
  911. if (legend.contentGroup.div) {
  912. legend.contentGroup.div.style.clip = height ?
  913. 'rect(' + padding + 'px,9999px,' +
  914. (padding + height) + 'px,0)' :
  915. 'auto';
  916. }
  917. }, addTracker = function (key) {
  918. legend[key] = renderer
  919. .circle(0, 0, arrowSize * 1.3)
  920. .translate(arrowSize / 2, arrowSize / 2)
  921. .add(nav);
  922. if (!chart.styledMode) {
  923. legend[key].attr('fill', 'rgba(0,0,0,0.0001)');
  924. }
  925. return legend[key];
  926. };
  927. // Adjust the height
  928. if (options.layout === 'horizontal' &&
  929. options.verticalAlign !== 'middle' &&
  930. !options.floating) {
  931. spaceHeight /= 2;
  932. }
  933. if (maxHeight) {
  934. spaceHeight = Math.min(spaceHeight, maxHeight);
  935. }
  936. // Reset the legend height and adjust the clipping rectangle
  937. pages.length = 0;
  938. if (legendHeight > spaceHeight &&
  939. navOptions.enabled !== false) {
  940. this.clipHeight = clipHeight =
  941. Math.max(spaceHeight - 20 - this.titleHeight - padding, 0);
  942. this.currentPage = pick(this.currentPage, 1);
  943. this.fullHeight = legendHeight;
  944. // Fill pages with Y positions so that the top of each a legend item
  945. // defines the scroll top for each page (#2098)
  946. allItems.forEach(function (item, i) {
  947. var y = item._legendItemPos[1], h = Math.round(item.legendItem.getBBox().height), len = pages.length;
  948. if (!len || (y - pages[len - 1] > clipHeight &&
  949. (lastY || y) !== pages[len - 1])) {
  950. pages.push(lastY || y);
  951. len++;
  952. }
  953. // Keep track of which page each item is on
  954. item.pageIx = len - 1;
  955. if (lastY) {
  956. allItems[i - 1].pageIx = len - 1;
  957. }
  958. if (i === allItems.length - 1 &&
  959. y + h - pages[len - 1] > clipHeight &&
  960. y !== lastY // #2617
  961. ) {
  962. pages.push(y);
  963. item.pageIx = len;
  964. }
  965. if (y !== lastY) {
  966. lastY = y;
  967. }
  968. });
  969. // Only apply clipping if needed. Clipping causes blurred legend in
  970. // PDF export (#1787)
  971. if (!clipRect) {
  972. clipRect = legend.clipRect =
  973. renderer.clipRect(0, padding, 9999, 0);
  974. legend.contentGroup.clip(clipRect);
  975. }
  976. clipToHeight(clipHeight);
  977. // Add navigation elements
  978. if (!nav) {
  979. this.nav = nav = renderer.g()
  980. .attr({ zIndex: 1 })
  981. .add(this.group);
  982. this.up = renderer
  983. .symbol('triangle', 0, 0, arrowSize, arrowSize)
  984. .add(nav);
  985. addTracker('upTracker')
  986. .on('click', function () {
  987. legend.scroll(-1, animation);
  988. });
  989. this.pager = renderer.text('', 15, 10)
  990. .addClass('highcharts-legend-navigation');
  991. if (!chart.styledMode) {
  992. this.pager.css(navOptions.style);
  993. }
  994. this.pager.add(nav);
  995. this.down = renderer
  996. .symbol('triangle-down', 0, 0, arrowSize, arrowSize)
  997. .add(nav);
  998. addTracker('downTracker')
  999. .on('click', function () {
  1000. legend.scroll(1, animation);
  1001. });
  1002. }
  1003. // Set initial position
  1004. legend.scroll(0);
  1005. legendHeight = spaceHeight;
  1006. // Reset
  1007. }
  1008. else if (nav) {
  1009. clipToHeight();
  1010. this.nav = nav.destroy(); // #6322
  1011. this.scrollGroup.attr({
  1012. translateY: 1
  1013. });
  1014. this.clipHeight = 0; // #1379
  1015. }
  1016. return legendHeight;
  1017. };
  1018. /**
  1019. * Scroll the legend by a number of pages.
  1020. *
  1021. * @private
  1022. * @function Highcharts.Legend#scroll
  1023. *
  1024. * @param {number} scrollBy
  1025. * The number of pages to scroll.
  1026. *
  1027. * @param {boolean|Partial<Highcharts.AnimationOptionsObject>} [animation]
  1028. * Whether and how to apply animation.
  1029. *
  1030. * @return {void}
  1031. */
  1032. Legend.prototype.scroll = function (scrollBy, animation) {
  1033. var _this = this;
  1034. var chart = this.chart, pages = this.pages, pageCount = pages.length, currentPage = this.currentPage + scrollBy, clipHeight = this.clipHeight, navOptions = this.options.navigation, pager = this.pager, padding = this.padding;
  1035. // When resizing while looking at the last page
  1036. if (currentPage > pageCount) {
  1037. currentPage = pageCount;
  1038. }
  1039. if (currentPage > 0) {
  1040. if (typeof animation !== 'undefined') {
  1041. setAnimation(animation, chart);
  1042. }
  1043. this.nav.attr({
  1044. translateX: padding,
  1045. translateY: clipHeight + this.padding + 7 + this.titleHeight,
  1046. visibility: 'visible'
  1047. });
  1048. [this.up, this.upTracker].forEach(function (elem) {
  1049. elem.attr({
  1050. 'class': currentPage === 1 ?
  1051. 'highcharts-legend-nav-inactive' :
  1052. 'highcharts-legend-nav-active'
  1053. });
  1054. });
  1055. pager.attr({
  1056. text: currentPage + '/' + pageCount
  1057. });
  1058. [this.down, this.downTracker].forEach(function (elem) {
  1059. elem.attr({
  1060. // adjust to text width
  1061. x: 18 + this.pager.getBBox().width,
  1062. 'class': currentPage === pageCount ?
  1063. 'highcharts-legend-nav-inactive' :
  1064. 'highcharts-legend-nav-active'
  1065. });
  1066. }, this);
  1067. if (!chart.styledMode) {
  1068. this.up
  1069. .attr({
  1070. fill: currentPage === 1 ?
  1071. navOptions.inactiveColor :
  1072. navOptions.activeColor
  1073. });
  1074. this.upTracker
  1075. .css({
  1076. cursor: currentPage === 1 ? 'default' : 'pointer'
  1077. });
  1078. this.down
  1079. .attr({
  1080. fill: currentPage === pageCount ?
  1081. navOptions.inactiveColor :
  1082. navOptions.activeColor
  1083. });
  1084. this.downTracker
  1085. .css({
  1086. cursor: currentPage === pageCount ?
  1087. 'default' :
  1088. 'pointer'
  1089. });
  1090. }
  1091. this.scrollOffset = -pages[currentPage - 1] + this.initialItemY;
  1092. this.scrollGroup.animate({
  1093. translateY: this.scrollOffset
  1094. });
  1095. this.currentPage = currentPage;
  1096. this.positionCheckboxes();
  1097. // Fire event after scroll animation is complete
  1098. var animOptions = animObject(pick(animation, chart.renderer.globalAnimation, true));
  1099. syncTimeout(function () {
  1100. fireEvent(_this, 'afterScroll', { currentPage: currentPage });
  1101. }, animOptions.duration);
  1102. }
  1103. };
  1104. /**
  1105. * @private
  1106. * @function Highcharts.Legend#setItemEvents
  1107. * @param {Highcharts.BubbleLegend|Point|Highcharts.Series} item
  1108. * @param {Highcharts.SVGElement} legendItem
  1109. * @param {boolean} [useHTML=false]
  1110. * @fires Highcharts.Point#event:legendItemClick
  1111. * @fires Highcharts.Series#event:legendItemClick
  1112. */
  1113. Legend.prototype.setItemEvents = function (item, legendItem, useHTML) {
  1114. var legend = this, boxWrapper = legend.chart.renderer.boxWrapper, isPoint = item instanceof Point, activeClass = 'highcharts-legend-' +
  1115. (isPoint ? 'point' : 'series') + '-active', styledMode = legend.chart.styledMode,
  1116. // When `useHTML`, the symbol is rendered in other group, so
  1117. // we need to apply events listeners to both places
  1118. legendItems = useHTML ?
  1119. [legendItem, item.legendSymbol] :
  1120. [item.legendGroup];
  1121. // Set the events on the item group, or in case of useHTML, the item
  1122. // itself (#1249)
  1123. legendItems.forEach(function (element) {
  1124. if (element) {
  1125. element
  1126. .on('mouseover', function () {
  1127. if (item.visible) {
  1128. legend.allItems.forEach(function (inactiveItem) {
  1129. if (item !== inactiveItem) {
  1130. inactiveItem.setState('inactive', !isPoint);
  1131. }
  1132. });
  1133. }
  1134. item.setState('hover');
  1135. // A CSS class to dim or hide other than the hovered
  1136. // series.
  1137. // Works only if hovered series is visible (#10071).
  1138. if (item.visible) {
  1139. boxWrapper.addClass(activeClass);
  1140. }
  1141. if (!styledMode) {
  1142. legendItem.css(legend.options.itemHoverStyle);
  1143. }
  1144. })
  1145. .on('mouseout', function () {
  1146. if (!legend.chart.styledMode) {
  1147. legendItem.css(merge(item.visible ?
  1148. legend.itemStyle :
  1149. legend.itemHiddenStyle));
  1150. }
  1151. legend.allItems.forEach(function (inactiveItem) {
  1152. if (item !== inactiveItem) {
  1153. inactiveItem.setState('', !isPoint);
  1154. }
  1155. });
  1156. // A CSS class to dim or hide other than the hovered
  1157. // series.
  1158. boxWrapper.removeClass(activeClass);
  1159. item.setState();
  1160. })
  1161. .on('click', function (event) {
  1162. var strLegendItemClick = 'legendItemClick', fnLegendItemClick = function () {
  1163. if (item.setVisible) {
  1164. item.setVisible();
  1165. }
  1166. // Reset inactive state
  1167. legend.allItems.forEach(function (inactiveItem) {
  1168. if (item !== inactiveItem) {
  1169. inactiveItem.setState(item.visible ? 'inactive' : '', !isPoint);
  1170. }
  1171. });
  1172. };
  1173. // A CSS class to dim or hide other than the hovered
  1174. // series. Event handling in iOS causes the activeClass
  1175. // to be added prior to click in some cases (#7418).
  1176. boxWrapper.removeClass(activeClass);
  1177. // Pass over the click/touch event. #4.
  1178. event = {
  1179. browserEvent: event
  1180. };
  1181. // click the name or symbol
  1182. if (item.firePointEvent) { // point
  1183. item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
  1184. }
  1185. else {
  1186. fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
  1187. }
  1188. });
  1189. }
  1190. });
  1191. };
  1192. /**
  1193. * @private
  1194. * @function Highcharts.Legend#createCheckboxForItem
  1195. * @param {Highcharts.BubbleLegend|Point|Highcharts.Series} item
  1196. * @fires Highcharts.Series#event:checkboxClick
  1197. */
  1198. Legend.prototype.createCheckboxForItem = function (item) {
  1199. var legend = this;
  1200. item.checkbox = createElement('input', {
  1201. type: 'checkbox',
  1202. className: 'highcharts-legend-checkbox',
  1203. checked: item.selected,
  1204. defaultChecked: item.selected // required by IE7
  1205. }, legend.options.itemCheckboxStyle, legend.chart.container);
  1206. addEvent(item.checkbox, 'click', function (event) {
  1207. var target = event.target;
  1208. fireEvent(item.series || item, 'checkboxClick', {
  1209. checked: target.checked,
  1210. item: item
  1211. }, function () {
  1212. item.select();
  1213. });
  1214. });
  1215. };
  1216. return Legend;
  1217. }());
  1218. // Workaround for #2030, horizontal legend items not displaying in IE11 Preview,
  1219. // and for #2580, a similar drawing flaw in Firefox 26.
  1220. // Explore if there's a general cause for this. The problem may be related
  1221. // to nested group elements, as the legend item texts are within 4 group
  1222. // elements.
  1223. if (/Trident\/7\.0/.test(win.navigator && win.navigator.userAgent) ||
  1224. isFirefox) {
  1225. wrap(Legend.prototype, 'positionItem', function (proceed, item) {
  1226. var legend = this,
  1227. // If chart destroyed in sync, this is undefined (#2030)
  1228. runPositionItem = function () {
  1229. if (item._legendItemPos) {
  1230. proceed.call(legend, item);
  1231. }
  1232. };
  1233. // Do it now, for export and to get checkbox placement
  1234. runPositionItem();
  1235. // Do it after to work around the core issue
  1236. if (!legend.bubbleLegend) {
  1237. setTimeout(runPositionItem);
  1238. }
  1239. });
  1240. }
  1241. H.Legend = Legend;
  1242. export default H.Legend;