BubbleLegend.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /* *
  2. *
  3. * (c) 2010-2020 Highsoft AS
  4. *
  5. * Author: Paweł Potaczek
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import Chart from '../../Core/Chart/Chart.js';
  14. import Color from '../../Core/Color.js';
  15. var color = Color.parse;
  16. import H from '../../Core/Globals.js';
  17. import Legend from '../../Core/Legend.js';
  18. import U from '../../Core/Utilities.js';
  19. var addEvent = U.addEvent, arrayMax = U.arrayMax, arrayMin = U.arrayMin, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, pick = U.pick, setOptions = U.setOptions, stableSort = U.stableSort, wrap = U.wrap;
  20. /**
  21. * @interface Highcharts.BubbleLegendFormatterContextObject
  22. */ /**
  23. * The center y position of the range.
  24. * @name Highcharts.BubbleLegendFormatterContextObject#center
  25. * @type {number}
  26. */ /**
  27. * The radius of the bubble range.
  28. * @name Highcharts.BubbleLegendFormatterContextObject#radius
  29. * @type {number}
  30. */ /**
  31. * The bubble value.
  32. * @name Highcharts.BubbleLegendFormatterContextObject#value
  33. * @type {number}
  34. */
  35. ''; // detach doclets above
  36. import './BubbleSeries.js';
  37. var Series = H.Series, noop = H.noop;
  38. setOptions({
  39. legend: {
  40. /**
  41. * The bubble legend is an additional element in legend which
  42. * presents the scale of the bubble series. Individual bubble ranges
  43. * can be defined by user or calculated from series. In the case of
  44. * automatically calculated ranges, a 1px margin of error is
  45. * permitted.
  46. *
  47. * @since 7.0.0
  48. * @product highcharts highstock highmaps
  49. * @requires highcharts-more
  50. * @optionparent legend.bubbleLegend
  51. */
  52. bubbleLegend: {
  53. /**
  54. * The color of the ranges borders, can be also defined for an
  55. * individual range.
  56. *
  57. * @sample highcharts/bubble-legend/similartoseries/
  58. * Similat look to the bubble series
  59. * @sample highcharts/bubble-legend/bordercolor/
  60. * Individual bubble border color
  61. *
  62. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  63. */
  64. borderColor: void 0,
  65. /**
  66. * The width of the ranges borders in pixels, can be also
  67. * defined for an individual range.
  68. */
  69. borderWidth: 2,
  70. /**
  71. * An additional class name to apply to the bubble legend'
  72. * circle graphical elements. This option does not replace
  73. * default class names of the graphical element.
  74. *
  75. * @sample {highcharts} highcharts/css/bubble-legend/
  76. * Styling by CSS
  77. *
  78. * @type {string}
  79. */
  80. className: void 0,
  81. /**
  82. * The main color of the bubble legend. Applies to ranges, if
  83. * individual color is not defined.
  84. *
  85. * @sample highcharts/bubble-legend/similartoseries/
  86. * Similat look to the bubble series
  87. * @sample highcharts/bubble-legend/color/
  88. * Individual bubble color
  89. *
  90. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  91. */
  92. color: void 0,
  93. /**
  94. * An additional class name to apply to the bubble legend's
  95. * connector graphical elements. This option does not replace
  96. * default class names of the graphical element.
  97. *
  98. * @sample {highcharts} highcharts/css/bubble-legend/
  99. * Styling by CSS
  100. *
  101. * @type {string}
  102. */
  103. connectorClassName: void 0,
  104. /**
  105. * The color of the connector, can be also defined
  106. * for an individual range.
  107. *
  108. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  109. */
  110. connectorColor: void 0,
  111. /**
  112. * The length of the connectors in pixels. If labels are
  113. * centered, the distance is reduced to 0.
  114. *
  115. * @sample highcharts/bubble-legend/connectorandlabels/
  116. * Increased connector length
  117. */
  118. connectorDistance: 60,
  119. /**
  120. * The width of the connectors in pixels.
  121. *
  122. * @sample highcharts/bubble-legend/connectorandlabels/
  123. * Increased connector width
  124. */
  125. connectorWidth: 1,
  126. /**
  127. * Enable or disable the bubble legend.
  128. */
  129. enabled: false,
  130. /**
  131. * Options for the bubble legend labels.
  132. */
  133. labels: {
  134. /**
  135. * An additional class name to apply to the bubble legend
  136. * label graphical elements. This option does not replace
  137. * default class names of the graphical element.
  138. *
  139. * @sample {highcharts} highcharts/css/bubble-legend/
  140. * Styling by CSS
  141. *
  142. * @type {string}
  143. */
  144. className: void 0,
  145. /**
  146. * Whether to allow data labels to overlap.
  147. */
  148. allowOverlap: false,
  149. /**
  150. * A format string for the bubble legend labels. Available
  151. * variables are the same as for `formatter`.
  152. *
  153. * @sample highcharts/bubble-legend/format/
  154. * Add a unit
  155. *
  156. * @type {string}
  157. */
  158. format: '',
  159. /**
  160. * Available `this` properties are:
  161. *
  162. * - `this.value`: The bubble value.
  163. *
  164. * - `this.radius`: The radius of the bubble range.
  165. *
  166. * - `this.center`: The center y position of the range.
  167. *
  168. * @type {Highcharts.FormatterCallbackFunction<Highcharts.BubbleLegendFormatterContextObject>}
  169. */
  170. formatter: void 0,
  171. /**
  172. * The alignment of the labels compared to the bubble
  173. * legend. Can be one of `left`, `center` or `right`.
  174. *
  175. * @sample highcharts/bubble-legend/connectorandlabels/
  176. * Labels on left
  177. *
  178. * @type {Highcharts.AlignValue}
  179. */
  180. align: 'right',
  181. /**
  182. * CSS styles for the labels.
  183. *
  184. * @type {Highcharts.CSSObject}
  185. */
  186. style: {
  187. /** @ignore-option */
  188. fontSize: 10,
  189. /** @ignore-option */
  190. color: void 0
  191. },
  192. /**
  193. * The x position offset of the label relative to the
  194. * connector.
  195. */
  196. x: 0,
  197. /**
  198. * The y position offset of the label relative to the
  199. * connector.
  200. */
  201. y: 0
  202. },
  203. /**
  204. * Miximum bubble legend range size. If values for ranges are
  205. * not specified, the `minSize` and the `maxSize` are calculated
  206. * from bubble series.
  207. */
  208. maxSize: 60,
  209. /**
  210. * Minimum bubble legend range size. If values for ranges are
  211. * not specified, the `minSize` and the `maxSize` are calculated
  212. * from bubble series.
  213. */
  214. minSize: 10,
  215. /**
  216. * The position of the bubble legend in the legend.
  217. * @sample highcharts/bubble-legend/connectorandlabels/
  218. * Bubble legend as last item in legend
  219. */
  220. legendIndex: 0,
  221. /**
  222. * Options for specific range. One range consists of bubble,
  223. * label and connector.
  224. *
  225. * @sample highcharts/bubble-legend/ranges/
  226. * Manually defined ranges
  227. * @sample highcharts/bubble-legend/autoranges/
  228. * Auto calculated ranges
  229. *
  230. * @type {Array<*>}
  231. */
  232. ranges: {
  233. /**
  234. * Range size value, similar to bubble Z data.
  235. * @type {number}
  236. */
  237. value: void 0,
  238. /**
  239. * The color of the border for individual range.
  240. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  241. */
  242. borderColor: void 0,
  243. /**
  244. * The color of the bubble for individual range.
  245. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  246. */
  247. color: void 0,
  248. /**
  249. * The color of the connector for individual range.
  250. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  251. */
  252. connectorColor: void 0
  253. },
  254. /**
  255. * Whether the bubble legend range value should be represented
  256. * by the area or the width of the bubble. The default, area,
  257. * corresponds best to the human perception of the size of each
  258. * bubble.
  259. *
  260. * @sample highcharts/bubble-legend/ranges/
  261. * Size by width
  262. *
  263. * @type {Highcharts.BubbleSizeByValue}
  264. */
  265. sizeBy: 'area',
  266. /**
  267. * When this is true, the absolute value of z determines the
  268. * size of the bubble. This means that with the default
  269. * zThreshold of 0, a bubble of value -1 will have the same size
  270. * as a bubble of value 1, while a bubble of value 0 will have a
  271. * smaller size according to minSize.
  272. */
  273. sizeByAbsoluteValue: false,
  274. /**
  275. * Define the visual z index of the bubble legend.
  276. */
  277. zIndex: 1,
  278. /**
  279. * Ranges with with lower value than zThreshold, are skipped.
  280. */
  281. zThreshold: 0
  282. }
  283. }
  284. });
  285. /* eslint-disable no-invalid-this, valid-jsdoc */
  286. /**
  287. * BubbleLegend class.
  288. *
  289. * @private
  290. * @class
  291. * @name Highcharts.BubbleLegend
  292. * @param {Highcharts.LegendBubbleLegendOptions} options
  293. * Bubble legend options
  294. * @param {Highcharts.Legend} legend
  295. * Legend
  296. */
  297. var BubbleLegend = /** @class */ (function () {
  298. function BubbleLegend(options, legend) {
  299. this.chart = void 0;
  300. this.fontMetrics = void 0;
  301. this.legend = void 0;
  302. this.legendGroup = void 0;
  303. this.legendItem = void 0;
  304. this.legendItemHeight = void 0;
  305. this.legendItemWidth = void 0;
  306. this.legendSymbol = void 0;
  307. this.maxLabel = void 0;
  308. this.movementX = void 0;
  309. this.ranges = void 0;
  310. this.visible = void 0;
  311. this.symbols = void 0;
  312. this.options = void 0;
  313. this.setState = noop;
  314. this.init(options, legend);
  315. }
  316. /**
  317. * Create basic bubbleLegend properties similar to item in legend.
  318. *
  319. * @private
  320. * @function Highcharts.BubbleLegend#init
  321. * @param {Highcharts.LegendBubbleLegendOptions} options
  322. * Bubble legend options
  323. * @param {Highcharts.Legend} legend
  324. * Legend
  325. * @return {void}
  326. */
  327. BubbleLegend.prototype.init = function (options, legend) {
  328. this.options = options;
  329. this.visible = true;
  330. this.chart = legend.chart;
  331. this.legend = legend;
  332. };
  333. /**
  334. * Depending on the position option, add bubbleLegend to legend items.
  335. *
  336. * @private
  337. * @function Highcharts.BubbleLegend#addToLegend
  338. * @param {Array<(Highcharts.Point|Highcharts.Series)>}
  339. * All legend items
  340. * @return {void}
  341. */
  342. BubbleLegend.prototype.addToLegend = function (items) {
  343. // Insert bubbleLegend into legend items
  344. items.splice(this.options.legendIndex, 0, this);
  345. };
  346. /**
  347. * Calculate ranges, sizes and call the next steps of bubbleLegend
  348. * creation.
  349. *
  350. * @private
  351. * @function Highcharts.BubbleLegend#drawLegendSymbol
  352. * @param {Highcharts.Legend} legend
  353. * Legend instance
  354. * @return {void}
  355. */
  356. BubbleLegend.prototype.drawLegendSymbol = function (legend) {
  357. var chart = this.chart, options = this.options, size, itemDistance = pick(legend.options.itemDistance, 20), connectorSpace, ranges = options.ranges, radius, maxLabel, connectorDistance = options.connectorDistance;
  358. // Predict label dimensions
  359. this.fontMetrics = chart.renderer.fontMetrics(options.labels.style.fontSize.toString() + 'px');
  360. // Do not create bubbleLegend now if ranges or ranges valeus are not
  361. // specified or if are empty array.
  362. if (!ranges || !ranges.length || !isNumber(ranges[0].value)) {
  363. legend.options.bubbleLegend.autoRanges = true;
  364. return;
  365. }
  366. // Sort ranges to right render order
  367. stableSort(ranges, function (a, b) {
  368. return b.value - a.value;
  369. });
  370. this.ranges = ranges;
  371. this.setOptions();
  372. this.render();
  373. // Get max label size
  374. maxLabel = this.getMaxLabelSize();
  375. radius = this.ranges[0].radius;
  376. size = radius * 2;
  377. // Space for connectors and labels.
  378. connectorSpace =
  379. connectorDistance - radius + maxLabel.width;
  380. connectorSpace = connectorSpace > 0 ? connectorSpace : 0;
  381. this.maxLabel = maxLabel;
  382. this.movementX = options.labels.align === 'left' ?
  383. connectorSpace : 0;
  384. this.legendItemWidth = size + connectorSpace + itemDistance;
  385. this.legendItemHeight = size + this.fontMetrics.h / 2;
  386. };
  387. /**
  388. * Set style options for each bubbleLegend range.
  389. *
  390. * @private
  391. * @function Highcharts.BubbleLegend#setOptions
  392. * @return {void}
  393. */
  394. BubbleLegend.prototype.setOptions = function () {
  395. var ranges = this.ranges, options = this.options, series = this.chart.series[options.seriesIndex], baseline = this.legend.baseline, bubbleStyle = {
  396. 'z-index': options.zIndex,
  397. 'stroke-width': options.borderWidth
  398. }, connectorStyle = {
  399. 'z-index': options.zIndex,
  400. 'stroke-width': options.connectorWidth
  401. }, labelStyle = this.getLabelStyles(), fillOpacity = series.options.marker.fillOpacity, styledMode = this.chart.styledMode;
  402. // Allow to parts of styles be used individually for range
  403. ranges.forEach(function (range, i) {
  404. if (!styledMode) {
  405. bubbleStyle.stroke = pick(range.borderColor, options.borderColor, series.color);
  406. bubbleStyle.fill = pick(range.color, options.color, fillOpacity !== 1 ?
  407. color(series.color).setOpacity(fillOpacity)
  408. .get('rgba') :
  409. series.color);
  410. connectorStyle.stroke = pick(range.connectorColor, options.connectorColor, series.color);
  411. }
  412. // Set options needed for rendering each range
  413. ranges[i].radius = this.getRangeRadius(range.value);
  414. ranges[i] = merge(ranges[i], {
  415. center: (ranges[0].radius - ranges[i].radius +
  416. baseline)
  417. });
  418. if (!styledMode) {
  419. merge(true, ranges[i], {
  420. bubbleStyle: merge(false, bubbleStyle),
  421. connectorStyle: merge(false, connectorStyle),
  422. labelStyle: labelStyle
  423. });
  424. }
  425. }, this);
  426. };
  427. /**
  428. * Merge options for bubbleLegend labels.
  429. *
  430. * @private
  431. * @function Highcharts.BubbleLegend#getLabelStyles
  432. * @return {Highcharts.CSSObject}
  433. */
  434. BubbleLegend.prototype.getLabelStyles = function () {
  435. var options = this.options, additionalLabelsStyle = {}, labelsOnLeft = options.labels.align === 'left', rtl = this.legend.options.rtl;
  436. // To separate additional style options
  437. objectEach(options.labels.style, function (value, key) {
  438. if (key !== 'color' &&
  439. key !== 'fontSize' &&
  440. key !== 'z-index') {
  441. additionalLabelsStyle[key] = value;
  442. }
  443. });
  444. return merge(false, additionalLabelsStyle, {
  445. 'font-size': options.labels.style.fontSize,
  446. fill: pick(options.labels.style.color, '#000000'),
  447. 'z-index': options.zIndex,
  448. align: rtl || labelsOnLeft ? 'right' : 'left'
  449. });
  450. };
  451. /**
  452. * Calculate radius for each bubble range,
  453. * used code from BubbleSeries.js 'getRadius' method.
  454. *
  455. * @private
  456. * @function Highcharts.BubbleLegend#getRangeRadius
  457. * @param {number} value
  458. * Range value
  459. * @return {number|null}
  460. * Radius for one range
  461. */
  462. BubbleLegend.prototype.getRangeRadius = function (value) {
  463. var options = this.options, seriesIndex = this.options.seriesIndex, bubbleSeries = this.chart.series[seriesIndex], zMax = options.ranges[0].value, zMin = options.ranges[options.ranges.length - 1].value, minSize = options.minSize, maxSize = options.maxSize;
  464. return bubbleSeries.getRadius.call(this, zMin, zMax, minSize, maxSize, value);
  465. };
  466. /**
  467. * Render the legendSymbol group.
  468. *
  469. * @private
  470. * @function Highcharts.BubbleLegend#render
  471. * @return {void}
  472. */
  473. BubbleLegend.prototype.render = function () {
  474. var renderer = this.chart.renderer, zThreshold = this.options.zThreshold;
  475. if (!this.symbols) {
  476. this.symbols = {
  477. connectors: [],
  478. bubbleItems: [],
  479. labels: []
  480. };
  481. }
  482. // Nesting SVG groups to enable handleOverflow
  483. this.legendSymbol = renderer.g('bubble-legend');
  484. this.legendItem = renderer.g('bubble-legend-item');
  485. // To enable default 'hideOverlappingLabels' method
  486. this.legendSymbol.translateX = 0;
  487. this.legendSymbol.translateY = 0;
  488. this.ranges.forEach(function (range) {
  489. if (range.value >= zThreshold) {
  490. this.renderRange(range);
  491. }
  492. }, this);
  493. // To use handleOverflow method
  494. this.legendSymbol.add(this.legendItem);
  495. this.legendItem.add(this.legendGroup);
  496. this.hideOverlappingLabels();
  497. };
  498. /**
  499. * Render one range, consisting of bubble symbol, connector and label.
  500. *
  501. * @private
  502. * @function Highcharts.BubbleLegend#renderRange
  503. * @param {Highcharts.LegendBubbleLegendRangesOptions} range
  504. * Range options
  505. * @return {void}
  506. */
  507. BubbleLegend.prototype.renderRange = function (range) {
  508. var mainRange = this.ranges[0], legend = this.legend, options = this.options, labelsOptions = options.labels, chart = this.chart, renderer = chart.renderer, symbols = this.symbols, labels = symbols.labels, label, elementCenter = range.center, absoluteRadius = Math.abs(range.radius), connectorDistance = options.connectorDistance || 0, labelsAlign = labelsOptions.align, rtl = legend.options.rtl, fontSize = labelsOptions.style.fontSize, connectorLength = rtl || labelsAlign === 'left' ?
  509. -connectorDistance : connectorDistance, borderWidth = options.borderWidth, connectorWidth = options.connectorWidth, posX = mainRange.radius || 0, posY = elementCenter - absoluteRadius -
  510. borderWidth / 2 + connectorWidth / 2, labelY, labelX, fontMetrics = this.fontMetrics, labelMovement = fontSize / 2 - (fontMetrics.h - fontSize) / 2, crispMovement = (posY % 1 ? 1 : 0.5) -
  511. (connectorWidth % 2 ? 0 : 0.5), styledMode = renderer.styledMode;
  512. // Set options for centered labels
  513. if (labelsAlign === 'center') {
  514. connectorLength = 0; // do not use connector
  515. options.connectorDistance = 0;
  516. range.labelStyle.align = 'center';
  517. }
  518. labelY = posY + options.labels.y;
  519. labelX = posX + connectorLength + options.labels.x;
  520. // Render bubble symbol
  521. symbols.bubbleItems.push(renderer
  522. .circle(posX, elementCenter + crispMovement, absoluteRadius)
  523. .attr(styledMode ? {} : range.bubbleStyle)
  524. .addClass((styledMode ?
  525. 'highcharts-color-' +
  526. this.options.seriesIndex + ' ' :
  527. '') +
  528. 'highcharts-bubble-legend-symbol ' +
  529. (options.className || '')).add(this.legendSymbol));
  530. // Render connector
  531. symbols.connectors.push(renderer
  532. .path(renderer.crispLine([
  533. ['M', posX, posY],
  534. ['L', posX + connectorLength, posY]
  535. ], options.connectorWidth))
  536. .attr(styledMode ? {} : range.connectorStyle)
  537. .addClass((styledMode ?
  538. 'highcharts-color-' +
  539. this.options.seriesIndex + ' ' : '') +
  540. 'highcharts-bubble-legend-connectors ' +
  541. (options.connectorClassName || '')).add(this.legendSymbol));
  542. // Render label
  543. label = renderer
  544. .text(this.formatLabel(range), labelX, labelY + labelMovement)
  545. .attr(styledMode ? {} : range.labelStyle)
  546. .addClass('highcharts-bubble-legend-labels ' +
  547. (options.labels.className || '')).add(this.legendSymbol);
  548. labels.push(label);
  549. // To enable default 'hideOverlappingLabels' method
  550. label.placed = true;
  551. label.alignAttr = {
  552. x: labelX,
  553. y: labelY + labelMovement
  554. };
  555. };
  556. /**
  557. * Get the label which takes up the most space.
  558. *
  559. * @private
  560. * @function Highcharts.BubbleLegend#getMaxLabelSize
  561. * @return {Highcharts.BBoxObject}
  562. */
  563. BubbleLegend.prototype.getMaxLabelSize = function () {
  564. var labels = this.symbols.labels, maxLabel, labelSize;
  565. labels.forEach(function (label) {
  566. labelSize = label.getBBox(true);
  567. if (maxLabel) {
  568. maxLabel = labelSize.width > maxLabel.width ?
  569. labelSize : maxLabel;
  570. }
  571. else {
  572. maxLabel = labelSize;
  573. }
  574. });
  575. return maxLabel || {};
  576. };
  577. /**
  578. * Get formatted label for range.
  579. *
  580. * @private
  581. * @function Highcharts.BubbleLegend#formatLabel
  582. * @param {Highcharts.LegendBubbleLegendRangesOptions} range
  583. * Range options
  584. * @return {string}
  585. * Range label text
  586. */
  587. BubbleLegend.prototype.formatLabel = function (range) {
  588. var options = this.options, formatter = options.labels.formatter, format = options.labels.format;
  589. var numberFormatter = this.chart.numberFormatter;
  590. return format ? U.format(format, range) :
  591. formatter ? formatter.call(range) :
  592. numberFormatter(range.value, 1);
  593. };
  594. /**
  595. * By using default chart 'hideOverlappingLabels' method, hide or show
  596. * labels and connectors.
  597. *
  598. * @private
  599. * @function Highcharts.BubbleLegend#hideOverlappingLabels
  600. * @return {void}
  601. */
  602. BubbleLegend.prototype.hideOverlappingLabels = function () {
  603. var chart = this.chart, allowOverlap = this.options.labels.allowOverlap, symbols = this.symbols;
  604. if (!allowOverlap && symbols) {
  605. chart.hideOverlappingLabels(symbols.labels);
  606. // Hide or show connectors
  607. symbols.labels.forEach(function (label, index) {
  608. if (!label.newOpacity) {
  609. symbols.connectors[index].hide();
  610. }
  611. else if (label.newOpacity !== label.oldOpacity) {
  612. symbols.connectors[index].show();
  613. }
  614. });
  615. }
  616. };
  617. /**
  618. * Calculate ranges from created series.
  619. *
  620. * @private
  621. * @function Highcharts.BubbleLegend#getRanges
  622. * @return {Array<Highcharts.LegendBubbleLegendRangesOptions>}
  623. * Array of range objects
  624. */
  625. BubbleLegend.prototype.getRanges = function () {
  626. var bubbleLegend = this.legend.bubbleLegend, series = bubbleLegend.chart.series, ranges, rangesOptions = bubbleLegend.options.ranges, zData, minZ = Number.MAX_VALUE, maxZ = -Number.MAX_VALUE;
  627. series.forEach(function (s) {
  628. // Find the min and max Z, like in bubble series
  629. if (s.isBubble && !s.ignoreSeries) {
  630. zData = s.zData.filter(isNumber);
  631. if (zData.length) {
  632. minZ = pick(s.options.zMin, Math.min(minZ, Math.max(arrayMin(zData), s.options.displayNegative === false ?
  633. s.options.zThreshold :
  634. -Number.MAX_VALUE)));
  635. maxZ = pick(s.options.zMax, Math.max(maxZ, arrayMax(zData)));
  636. }
  637. }
  638. });
  639. // Set values for ranges
  640. if (minZ === maxZ) {
  641. // Only one range if min and max values are the same.
  642. ranges = [{ value: maxZ }];
  643. }
  644. else {
  645. ranges = [
  646. { value: minZ },
  647. { value: (minZ + maxZ) / 2 },
  648. { value: maxZ, autoRanges: true }
  649. ];
  650. }
  651. // Prevent reverse order of ranges after redraw
  652. if (rangesOptions.length && rangesOptions[0].radius) {
  653. ranges.reverse();
  654. }
  655. // Merge ranges values with user options
  656. ranges.forEach(function (range, i) {
  657. if (rangesOptions && rangesOptions[i]) {
  658. ranges[i] = merge(false, rangesOptions[i], range);
  659. }
  660. });
  661. return ranges;
  662. };
  663. /**
  664. * Calculate bubble legend sizes from rendered series.
  665. *
  666. * @private
  667. * @function Highcharts.BubbleLegend#predictBubbleSizes
  668. * @return {Array<number,number>}
  669. * Calculated min and max bubble sizes
  670. */
  671. BubbleLegend.prototype.predictBubbleSizes = function () {
  672. var chart = this.chart, fontMetrics = this.fontMetrics, legendOptions = chart.legend.options, floating = legendOptions.floating, horizontal = legendOptions.layout === 'horizontal', lastLineHeight = horizontal ? chart.legend.lastLineHeight : 0, plotSizeX = chart.plotSizeX, plotSizeY = chart.plotSizeY, bubbleSeries = chart.series[this.options.seriesIndex], minSize = Math.ceil(bubbleSeries.minPxSize), maxPxSize = Math.ceil(bubbleSeries.maxPxSize), maxSize = bubbleSeries.options.maxSize, plotSize = Math.min(plotSizeY, plotSizeX), calculatedSize;
  673. // Calculate prediceted max size of bubble
  674. if (floating || !(/%$/.test(maxSize))) {
  675. calculatedSize = maxPxSize;
  676. }
  677. else {
  678. maxSize = parseFloat(maxSize);
  679. calculatedSize = ((plotSize + lastLineHeight -
  680. fontMetrics.h / 2) * maxSize / 100) / (maxSize / 100 + 1);
  681. // Get maxPxSize from bubble series if calculated bubble legend
  682. // size will not affect to bubbles series.
  683. if ((horizontal && plotSizeY - calculatedSize >=
  684. plotSizeX) || (!horizontal && plotSizeX -
  685. calculatedSize >= plotSizeY)) {
  686. calculatedSize = maxPxSize;
  687. }
  688. }
  689. return [minSize, Math.ceil(calculatedSize)];
  690. };
  691. /**
  692. * Correct ranges with calculated sizes.
  693. *
  694. * @private
  695. * @function Highcharts.BubbleLegend#updateRanges
  696. * @param {number} min
  697. * @param {number} max
  698. * @return {void}
  699. */
  700. BubbleLegend.prototype.updateRanges = function (min, max) {
  701. var bubbleLegendOptions = this.legend.options.bubbleLegend;
  702. bubbleLegendOptions.minSize = min;
  703. bubbleLegendOptions.maxSize = max;
  704. bubbleLegendOptions.ranges = this.getRanges();
  705. };
  706. /**
  707. * Because of the possibility of creating another legend line, predicted
  708. * bubble legend sizes may differ by a few pixels, so it is necessary to
  709. * correct them.
  710. *
  711. * @private
  712. * @function Highcharts.BubbleLegend#correctSizes
  713. * @return {void}
  714. */
  715. BubbleLegend.prototype.correctSizes = function () {
  716. var legend = this.legend, chart = this.chart, bubbleSeries = chart.series[this.options.seriesIndex], bubbleSeriesSize = bubbleSeries.maxPxSize, bubbleLegendSize = this.options.maxSize;
  717. if (Math.abs(Math.ceil(bubbleSeriesSize) - bubbleLegendSize) >
  718. 1) {
  719. this.updateRanges(this.options.minSize, bubbleSeries.maxPxSize);
  720. legend.render();
  721. }
  722. };
  723. return BubbleLegend;
  724. }());
  725. // Start the bubble legend creation process.
  726. addEvent(Legend, 'afterGetAllItems', function (e) {
  727. var legend = this, bubbleLegend = legend.bubbleLegend, legendOptions = legend.options, options = legendOptions.bubbleLegend, bubbleSeriesIndex = legend.chart.getVisibleBubbleSeriesIndex();
  728. // Remove unnecessary element
  729. if (bubbleLegend && bubbleLegend.ranges && bubbleLegend.ranges.length) {
  730. // Allow change the way of calculating ranges in update
  731. if (options.ranges.length) {
  732. options.autoRanges =
  733. !!options.ranges[0].autoRanges;
  734. }
  735. // Update bubbleLegend dimensions in each redraw
  736. legend.destroyItem(bubbleLegend);
  737. }
  738. // Create bubble legend
  739. if (bubbleSeriesIndex >= 0 &&
  740. legendOptions.enabled &&
  741. options.enabled) {
  742. options.seriesIndex = bubbleSeriesIndex;
  743. legend.bubbleLegend = new H.BubbleLegend(options, legend);
  744. legend.bubbleLegend.addToLegend(e.allItems);
  745. }
  746. });
  747. /**
  748. * Check if there is at least one visible bubble series.
  749. *
  750. * @private
  751. * @function Highcharts.Chart#getVisibleBubbleSeriesIndex
  752. * @return {number}
  753. * First visible bubble series index
  754. */
  755. Chart.prototype.getVisibleBubbleSeriesIndex = function () {
  756. var series = this.series, i = 0;
  757. while (i < series.length) {
  758. if (series[i] &&
  759. series[i].isBubble &&
  760. series[i].visible &&
  761. series[i].zData.length) {
  762. return i;
  763. }
  764. i++;
  765. }
  766. return -1;
  767. };
  768. /**
  769. * Calculate height for each row in legend.
  770. *
  771. * @private
  772. * @function Highcharts.Legend#getLinesHeights
  773. * @return {Array<Highcharts.Dictionary<number>>}
  774. * Informations about line height and items amount
  775. */
  776. Legend.prototype.getLinesHeights = function () {
  777. var items = this.allItems, lines = [], lastLine, length = items.length, i = 0, j = 0;
  778. for (i = 0; i < length; i++) {
  779. if (items[i].legendItemHeight) {
  780. // for bubbleLegend
  781. items[i].itemHeight = items[i].legendItemHeight;
  782. }
  783. if ( // Line break
  784. items[i] === items[length - 1] ||
  785. items[i + 1] &&
  786. items[i]._legendItemPos[1] !==
  787. items[i + 1]._legendItemPos[1]) {
  788. lines.push({ height: 0 });
  789. lastLine = lines[lines.length - 1];
  790. // Find the highest item in line
  791. for (j; j <= i; j++) {
  792. if (items[j].itemHeight > lastLine.height) {
  793. lastLine.height = items[j].itemHeight;
  794. }
  795. }
  796. lastLine.step = i;
  797. }
  798. }
  799. return lines;
  800. };
  801. /**
  802. * Correct legend items translation in case of different elements heights.
  803. *
  804. * @private
  805. * @function Highcharts.Legend#retranslateItems
  806. * @param {Array<Highcharts.Dictionary<number>>} lines
  807. * Informations about line height and items amount
  808. * @return {void}
  809. */
  810. Legend.prototype.retranslateItems = function (lines) {
  811. var items = this.allItems, orgTranslateX, orgTranslateY, movementX, rtl = this.options.rtl, actualLine = 0;
  812. items.forEach(function (item, index) {
  813. orgTranslateX = item.legendGroup.translateX;
  814. orgTranslateY = item._legendItemPos[1];
  815. movementX = item.movementX;
  816. if (movementX || (rtl && item.ranges)) {
  817. movementX = rtl ?
  818. orgTranslateX - item.options.maxSize / 2 :
  819. orgTranslateX + movementX;
  820. item.legendGroup.attr({ translateX: movementX });
  821. }
  822. if (index > lines[actualLine].step) {
  823. actualLine++;
  824. }
  825. item.legendGroup.attr({
  826. translateY: Math.round(orgTranslateY + lines[actualLine].height / 2)
  827. });
  828. item._legendItemPos[1] = orgTranslateY +
  829. lines[actualLine].height / 2;
  830. });
  831. };
  832. // Toggle bubble legend depending on the visible status of bubble series.
  833. addEvent(Series, 'legendItemClick', function () {
  834. var series = this, chart = series.chart, visible = series.visible, legend = series.chart.legend, status;
  835. if (legend && legend.bubbleLegend) {
  836. // Temporary correct 'visible' property
  837. series.visible = !visible;
  838. // Save future status for getRanges method
  839. series.ignoreSeries = visible;
  840. // Check if at lest one bubble series is visible
  841. status = chart.getVisibleBubbleSeriesIndex() >= 0;
  842. // Hide bubble legend if all bubble series are disabled
  843. if (legend.bubbleLegend.visible !== status) {
  844. // Show or hide bubble legend
  845. legend.update({
  846. bubbleLegend: { enabled: status }
  847. });
  848. legend.bubbleLegend.visible = status; // Restore default status
  849. }
  850. series.visible = visible;
  851. }
  852. });
  853. // If ranges are not specified, determine ranges from rendered bubble series
  854. // and render legend again.
  855. wrap(Chart.prototype, 'drawChartBox', function (proceed, options, callback) {
  856. var chart = this, legend = chart.legend, bubbleSeries = chart.getVisibleBubbleSeriesIndex() >= 0, bubbleLegendOptions, bubbleSizes;
  857. if (legend && legend.options.enabled && legend.bubbleLegend &&
  858. legend.options.bubbleLegend.autoRanges && bubbleSeries) {
  859. bubbleLegendOptions = legend.bubbleLegend.options;
  860. bubbleSizes = legend.bubbleLegend.predictBubbleSizes();
  861. legend.bubbleLegend.updateRanges(bubbleSizes[0], bubbleSizes[1]);
  862. // Disable animation on init
  863. if (!bubbleLegendOptions.placed) {
  864. legend.group.placed = false;
  865. legend.allItems.forEach(function (item) {
  866. item.legendGroup.translateY = null;
  867. });
  868. }
  869. // Create legend with bubbleLegend
  870. legend.render();
  871. chart.getMargins();
  872. chart.axes.forEach(function (axis) {
  873. if (axis.visible) { // #11448
  874. axis.render();
  875. }
  876. if (!bubbleLegendOptions.placed) {
  877. axis.setScale();
  878. axis.updateNames();
  879. // Disable axis animation on init
  880. objectEach(axis.ticks, function (tick) {
  881. tick.isNew = true;
  882. tick.isNewLabel = true;
  883. });
  884. }
  885. });
  886. bubbleLegendOptions.placed = true;
  887. // After recalculate axes, calculate margins again.
  888. chart.getMargins();
  889. // Call default 'drawChartBox' method.
  890. proceed.call(chart, options, callback);
  891. // Check bubble legend sizes and correct them if necessary.
  892. legend.bubbleLegend.correctSizes();
  893. // Correct items positions with different dimensions in legend.
  894. legend.retranslateItems(legend.getLinesHeights());
  895. }
  896. else {
  897. proceed.call(chart, options, callback);
  898. // Allow color change on static bubble legend after click on legend
  899. if (legend && legend.options.enabled && legend.bubbleLegend) {
  900. legend.render();
  901. legend.retranslateItems(legend.getLinesHeights());
  902. }
  903. }
  904. });
  905. H.BubbleLegend = BubbleLegend;
  906. export default H.BubbleLegend;