BubbleSeries.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. import Axis from '../../Core/Axis/Axis.js';
  11. import BaseSeries from '../../Core/Series/Series.js';
  12. import Color from '../../Core/Color/Color.js';
  13. var color = Color.parse;
  14. import H from '../../Core/Globals.js';
  15. var noop = H.noop;
  16. import Point from '../../Core/Series/Point.js';
  17. import U from '../../Core/Utilities.js';
  18. var arrayMax = U.arrayMax, arrayMin = U.arrayMin, clamp = U.clamp, extend = U.extend, isNumber = U.isNumber, pick = U.pick, pInt = U.pInt;
  19. import '../../Series/LineSeries.js';
  20. import '../../Series/ScatterSeries.js';
  21. import './BubbleLegend.js';
  22. var Series = H.Series, seriesTypes = BaseSeries.seriesTypes;
  23. /**
  24. * @typedef {"area"|"width"} Highcharts.BubbleSizeByValue
  25. */
  26. ''; // detach doclets above
  27. /**
  28. * A bubble series is a three dimensional series type where each point renders
  29. * an X, Y and Z value. Each points is drawn as a bubble where the position
  30. * along the X and Y axes mark the X and Y values, and the size of the bubble
  31. * relates to the Z value.
  32. *
  33. * @sample {highcharts} highcharts/demo/bubble/
  34. * Bubble chart
  35. *
  36. * @extends plotOptions.scatter
  37. * @excluding cluster
  38. * @product highcharts highstock
  39. * @requires highcharts-more
  40. * @optionparent plotOptions.bubble
  41. */
  42. BaseSeries.seriesType('bubble', 'scatter', {
  43. dataLabels: {
  44. formatter: function () {
  45. return this.point.z;
  46. },
  47. inside: true,
  48. verticalAlign: 'middle'
  49. },
  50. /**
  51. * If there are more points in the series than the `animationLimit`, the
  52. * animation won't run. Animation affects overall performance and doesn't
  53. * work well with heavy data series.
  54. *
  55. * @since 6.1.0
  56. */
  57. animationLimit: 250,
  58. /**
  59. * Whether to display negative sized bubbles. The threshold is given
  60. * by the [zThreshold](#plotOptions.bubble.zThreshold) option, and negative
  61. * bubbles can be visualized by setting
  62. * [negativeColor](#plotOptions.bubble.negativeColor).
  63. *
  64. * @sample {highcharts} highcharts/plotoptions/bubble-negative/
  65. * Negative bubbles
  66. *
  67. * @type {boolean}
  68. * @default true
  69. * @since 3.0
  70. * @apioption plotOptions.bubble.displayNegative
  71. */
  72. /**
  73. * @extends plotOptions.series.marker
  74. * @excluding enabled, enabledThreshold, height, radius, width
  75. */
  76. marker: {
  77. lineColor: null,
  78. lineWidth: 1,
  79. /**
  80. * The fill opacity of the bubble markers.
  81. */
  82. fillOpacity: 0.5,
  83. /**
  84. * In bubble charts, the radius is overridden and determined based on
  85. * the point's data value.
  86. *
  87. * @ignore-option
  88. */
  89. radius: null,
  90. states: {
  91. hover: {
  92. radiusPlus: 0
  93. }
  94. },
  95. /**
  96. * A predefined shape or symbol for the marker. Possible values are
  97. * "circle", "square", "diamond", "triangle" and "triangle-down".
  98. *
  99. * Additionally, the URL to a graphic can be given on the form
  100. * `url(graphic.png)`. Note that for the image to be applied to exported
  101. * charts, its URL needs to be accessible by the export server.
  102. *
  103. * Custom callbacks for symbol path generation can also be added to
  104. * `Highcharts.SVGRenderer.prototype.symbols`. The callback is then
  105. * used by its method name, as shown in the demo.
  106. *
  107. * @sample {highcharts} highcharts/plotoptions/bubble-symbol/
  108. * Bubble chart with various symbols
  109. * @sample {highcharts} highcharts/plotoptions/series-marker-symbol/
  110. * General chart with predefined, graphic and custom markers
  111. *
  112. * @type {Highcharts.SymbolKeyValue|string}
  113. * @since 5.0.11
  114. */
  115. symbol: 'circle'
  116. },
  117. /**
  118. * Minimum bubble size. Bubbles will automatically size between the
  119. * `minSize` and `maxSize` to reflect the `z` value of each bubble.
  120. * Can be either pixels (when no unit is given), or a percentage of
  121. * the smallest one of the plot width and height.
  122. *
  123. * @sample {highcharts} highcharts/plotoptions/bubble-size/
  124. * Bubble size
  125. *
  126. * @type {number|string}
  127. * @since 3.0
  128. * @product highcharts highstock
  129. */
  130. minSize: 8,
  131. /**
  132. * Maximum bubble size. Bubbles will automatically size between the
  133. * `minSize` and `maxSize` to reflect the `z` value of each bubble.
  134. * Can be either pixels (when no unit is given), or a percentage of
  135. * the smallest one of the plot width and height.
  136. *
  137. * @sample {highcharts} highcharts/plotoptions/bubble-size/
  138. * Bubble size
  139. *
  140. * @type {number|string}
  141. * @since 3.0
  142. * @product highcharts highstock
  143. */
  144. maxSize: '20%',
  145. /**
  146. * When a point's Z value is below the
  147. * [zThreshold](#plotOptions.bubble.zThreshold) setting, this color is used.
  148. *
  149. * @sample {highcharts} highcharts/plotoptions/bubble-negative/
  150. * Negative bubbles
  151. *
  152. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  153. * @since 3.0
  154. * @product highcharts
  155. * @apioption plotOptions.bubble.negativeColor
  156. */
  157. /**
  158. * Whether the bubble's value should be represented by the area or the
  159. * width of the bubble. The default, `area`, corresponds best to the
  160. * human perception of the size of each bubble.
  161. *
  162. * @sample {highcharts} highcharts/plotoptions/bubble-sizeby/
  163. * Comparison of area and size
  164. *
  165. * @type {Highcharts.BubbleSizeByValue}
  166. * @default area
  167. * @since 3.0.7
  168. * @apioption plotOptions.bubble.sizeBy
  169. */
  170. /**
  171. * When this is true, the absolute value of z determines the size of
  172. * the bubble. This means that with the default `zThreshold` of 0, a
  173. * bubble of value -1 will have the same size as a bubble of value 1,
  174. * while a bubble of value 0 will have a smaller size according to
  175. * `minSize`.
  176. *
  177. * @sample {highcharts} highcharts/plotoptions/bubble-sizebyabsolutevalue/
  178. * Size by absolute value, various thresholds
  179. *
  180. * @type {boolean}
  181. * @default false
  182. * @since 4.1.9
  183. * @product highcharts
  184. * @apioption plotOptions.bubble.sizeByAbsoluteValue
  185. */
  186. /**
  187. * When this is true, the series will not cause the Y axis to cross
  188. * the zero plane (or [threshold](#plotOptions.series.threshold) option)
  189. * unless the data actually crosses the plane.
  190. *
  191. * For example, if `softThreshold` is `false`, a series of 0, 1, 2,
  192. * 3 will make the Y axis show negative values according to the `minPadding`
  193. * option. If `softThreshold` is `true`, the Y axis starts at 0.
  194. *
  195. * @since 4.1.9
  196. * @product highcharts
  197. */
  198. softThreshold: false,
  199. states: {
  200. hover: {
  201. halo: {
  202. size: 5
  203. }
  204. }
  205. },
  206. tooltip: {
  207. pointFormat: '({point.x}, {point.y}), Size: {point.z}'
  208. },
  209. turboThreshold: 0,
  210. /**
  211. * The minimum for the Z value range. Defaults to the highest Z value
  212. * in the data.
  213. *
  214. * @see [zMin](#plotOptions.bubble.zMin)
  215. *
  216. * @sample {highcharts} highcharts/plotoptions/bubble-zmin-zmax/
  217. * Z has a possible range of 0-100
  218. *
  219. * @type {number}
  220. * @since 4.0.3
  221. * @product highcharts
  222. * @apioption plotOptions.bubble.zMax
  223. */
  224. /**
  225. * @default z
  226. * @apioption plotOptions.bubble.colorKey
  227. */
  228. /**
  229. * The minimum for the Z value range. Defaults to the lowest Z value
  230. * in the data.
  231. *
  232. * @see [zMax](#plotOptions.bubble.zMax)
  233. *
  234. * @sample {highcharts} highcharts/plotoptions/bubble-zmin-zmax/
  235. * Z has a possible range of 0-100
  236. *
  237. * @type {number}
  238. * @since 4.0.3
  239. * @product highcharts
  240. * @apioption plotOptions.bubble.zMin
  241. */
  242. /**
  243. * When [displayNegative](#plotOptions.bubble.displayNegative) is `false`,
  244. * bubbles with lower Z values are skipped. When `displayNegative`
  245. * is `true` and a [negativeColor](#plotOptions.bubble.negativeColor)
  246. * is given, points with lower Z is colored.
  247. *
  248. * @sample {highcharts} highcharts/plotoptions/bubble-negative/
  249. * Negative bubbles
  250. *
  251. * @since 3.0
  252. * @product highcharts
  253. */
  254. zThreshold: 0,
  255. zoneAxis: 'z'
  256. // Prototype members
  257. }, {
  258. pointArrayMap: ['y', 'z'],
  259. parallelArrays: ['x', 'y', 'z'],
  260. trackerGroups: ['group', 'dataLabelsGroup'],
  261. specialGroup: 'group',
  262. bubblePadding: true,
  263. zoneAxis: 'z',
  264. directTouch: true,
  265. isBubble: true,
  266. /* eslint-disable valid-jsdoc */
  267. /**
  268. * @private
  269. */
  270. pointAttribs: function (point, state) {
  271. var markerOptions = this.options.marker, fillOpacity = markerOptions.fillOpacity, attr = Series.prototype.pointAttribs.call(this, point, state);
  272. if (fillOpacity !== 1) {
  273. attr.fill = color(attr.fill)
  274. .setOpacity(fillOpacity)
  275. .get('rgba');
  276. }
  277. return attr;
  278. },
  279. /**
  280. * Get the radius for each point based on the minSize, maxSize and each
  281. * point's Z value. This must be done prior to Series.translate because
  282. * the axis needs to add padding in accordance with the point sizes.
  283. * @private
  284. */
  285. getRadii: function (zMin, zMax, series) {
  286. var len, i, zData = this.zData, yData = this.yData, minSize = series.minPxSize, maxSize = series.maxPxSize, radii = [], value;
  287. // Set the shape type and arguments to be picked up in drawPoints
  288. for (i = 0, len = zData.length; i < len; i++) {
  289. value = zData[i];
  290. // Separate method to get individual radius for bubbleLegend
  291. radii.push(this.getRadius(zMin, zMax, minSize, maxSize, value, yData[i]));
  292. }
  293. this.radii = radii;
  294. },
  295. /**
  296. * Get the individual radius for one point.
  297. * @private
  298. */
  299. getRadius: function (zMin, zMax, minSize, maxSize, value, yValue) {
  300. var options = this.options, sizeByArea = options.sizeBy !== 'width', zThreshold = options.zThreshold, zRange = zMax - zMin, pos = 0.5;
  301. // #8608 - bubble should be visible when z is undefined
  302. if (yValue === null || value === null) {
  303. return null;
  304. }
  305. if (isNumber(value)) {
  306. // When sizing by threshold, the absolute value of z determines
  307. // the size of the bubble.
  308. if (options.sizeByAbsoluteValue) {
  309. value = Math.abs(value - zThreshold);
  310. zMax = zRange = Math.max(zMax - zThreshold, Math.abs(zMin - zThreshold));
  311. zMin = 0;
  312. }
  313. // Issue #4419 - if value is less than zMin, push a radius that's
  314. // always smaller than the minimum size
  315. if (value < zMin) {
  316. return minSize / 2 - 1;
  317. }
  318. // Relative size, a number between 0 and 1
  319. if (zRange > 0) {
  320. pos = (value - zMin) / zRange;
  321. }
  322. }
  323. if (sizeByArea && pos >= 0) {
  324. pos = Math.sqrt(pos);
  325. }
  326. return Math.ceil(minSize + pos * (maxSize - minSize)) / 2;
  327. },
  328. /**
  329. * Perform animation on the bubbles
  330. * @private
  331. */
  332. animate: function (init) {
  333. if (!init &&
  334. this.points.length < this.options.animationLimit // #8099
  335. ) {
  336. this.points.forEach(function (point) {
  337. var graphic = point.graphic;
  338. if (graphic && graphic.width) { // URL symbols don't have width
  339. // Start values
  340. if (!this.hasRendered) {
  341. graphic.attr({
  342. x: point.plotX,
  343. y: point.plotY,
  344. width: 1,
  345. height: 1
  346. });
  347. }
  348. // Run animation
  349. graphic.animate(this.markerAttribs(point), this.options.animation);
  350. }
  351. }, this);
  352. }
  353. },
  354. /**
  355. * Define hasData function for non-cartesian series.
  356. * Returns true if the series has points at all.
  357. * @private
  358. */
  359. hasData: function () {
  360. return !!this.processedXData.length; // != 0
  361. },
  362. /**
  363. * Extend the base translate method to handle bubble size
  364. * @private
  365. */
  366. translate: function () {
  367. var i, data = this.data, point, radius, radii = this.radii;
  368. // Run the parent method
  369. seriesTypes.scatter.prototype.translate.call(this);
  370. // Set the shape type and arguments to be picked up in drawPoints
  371. i = data.length;
  372. while (i--) {
  373. point = data[i];
  374. radius = radii ? radii[i] : 0; // #1737
  375. if (isNumber(radius) && radius >= this.minPxSize / 2) {
  376. // Shape arguments
  377. point.marker = extend(point.marker, {
  378. radius: radius,
  379. width: 2 * radius,
  380. height: 2 * radius
  381. });
  382. // Alignment box for the data label
  383. point.dlBox = {
  384. x: point.plotX - radius,
  385. y: point.plotY - radius,
  386. width: 2 * radius,
  387. height: 2 * radius
  388. };
  389. }
  390. else { // below zThreshold
  391. // #1691
  392. point.shapeArgs = point.plotY = point.dlBox = void 0;
  393. }
  394. }
  395. },
  396. alignDataLabel: seriesTypes.column.prototype.alignDataLabel,
  397. buildKDTree: noop,
  398. applyZones: noop
  399. // Point class
  400. }, {
  401. /**
  402. * @private
  403. */
  404. haloPath: function (size) {
  405. return Point.prototype.haloPath.call(this,
  406. // #6067
  407. size === 0 ? 0 : (this.marker ? this.marker.radius || 0 : 0) + size);
  408. },
  409. ttBelow: false
  410. });
  411. // Add logic to pad each axis with the amount of pixels necessary to avoid the
  412. // bubbles to overflow.
  413. Axis.prototype.beforePadding = function () {
  414. var axis = this, axisLength = this.len, chart = this.chart, pxMin = 0, pxMax = axisLength, isXAxis = this.isXAxis, dataKey = isXAxis ? 'xData' : 'yData', min = this.min, extremes = {}, smallestSize = Math.min(chart.plotWidth, chart.plotHeight), zMin = Number.MAX_VALUE, zMax = -Number.MAX_VALUE, range = this.max - min, transA = axisLength / range, activeSeries = [];
  415. // Handle padding on the second pass, or on redraw
  416. this.series.forEach(function (series) {
  417. var seriesOptions = series.options, zData;
  418. if (series.bubblePadding &&
  419. (series.visible || !chart.options.chart.ignoreHiddenSeries)) {
  420. // Correction for #1673
  421. axis.allowZoomOutside = true;
  422. // Cache it
  423. activeSeries.push(series);
  424. if (isXAxis) { // because X axis is evaluated first
  425. // For each series, translate the size extremes to pixel values
  426. ['minSize', 'maxSize'].forEach(function (prop) {
  427. var length = seriesOptions[prop], isPercent = /%$/.test(length);
  428. length = pInt(length);
  429. extremes[prop] = isPercent ?
  430. smallestSize * length / 100 :
  431. length;
  432. });
  433. series.minPxSize = extremes.minSize;
  434. // Prioritize min size if conflict to make sure bubbles are
  435. // always visible. #5873
  436. series.maxPxSize = Math.max(extremes.maxSize, extremes.minSize);
  437. // Find the min and max Z
  438. zData = series.zData.filter(isNumber);
  439. if (zData.length) { // #1735
  440. zMin = pick(seriesOptions.zMin, clamp(arrayMin(zData), seriesOptions.displayNegative === false ?
  441. seriesOptions.zThreshold :
  442. -Number.MAX_VALUE, zMin));
  443. zMax = pick(seriesOptions.zMax, Math.max(zMax, arrayMax(zData)));
  444. }
  445. }
  446. }
  447. });
  448. activeSeries.forEach(function (series) {
  449. var data = series[dataKey], i = data.length, radius;
  450. if (isXAxis) {
  451. series.getRadii(zMin, zMax, series);
  452. }
  453. if (range > 0) {
  454. while (i--) {
  455. if (isNumber(data[i]) &&
  456. axis.dataMin <= data[i] &&
  457. data[i] <= axis.max) {
  458. radius = series.radii ? series.radii[i] : 0;
  459. pxMin = Math.min(((data[i] - min) * transA) - radius, pxMin);
  460. pxMax = Math.max(((data[i] - min) * transA) + radius, pxMax);
  461. }
  462. }
  463. }
  464. });
  465. // Apply the padding to the min and max properties
  466. if (activeSeries.length && range > 0 && !this.logarithmic) {
  467. pxMax -= axisLength;
  468. transA *= (axisLength +
  469. Math.max(0, pxMin) - // #8901
  470. Math.min(pxMax, axisLength)) / axisLength;
  471. [
  472. ['min', 'userMin', pxMin],
  473. ['max', 'userMax', pxMax]
  474. ].forEach(function (keys) {
  475. if (typeof pick(axis.options[keys[0]], axis[keys[1]]) === 'undefined') {
  476. axis[keys[0]] += keys[2] / transA;
  477. }
  478. });
  479. }
  480. /* eslint-enable valid-jsdoc */
  481. };
  482. /**
  483. * A `bubble` series. If the [type](#series.bubble.type) option is
  484. * not specified, it is inherited from [chart.type](#chart.type).
  485. *
  486. * @extends series,plotOptions.bubble
  487. * @excluding dataParser, dataURL, stack
  488. * @product highcharts highstock
  489. * @requires highcharts-more
  490. * @apioption series.bubble
  491. */
  492. /**
  493. * An array of data points for the series. For the `bubble` series type,
  494. * points can be given in the following ways:
  495. *
  496. * 1. An array of arrays with 3 or 2 values. In this case, the values correspond
  497. * to `x,y,z`. If the first value is a string, it is applied as the name of
  498. * the point, and the `x` value is inferred. The `x` value can also be
  499. * omitted, in which case the inner arrays should be of length 2\. Then the
  500. * `x` value is automatically calculated, either starting at 0 and
  501. * incremented by 1, or from `pointStart` and `pointInterval` given in the
  502. * series options.
  503. * ```js
  504. * data: [
  505. * [0, 1, 2],
  506. * [1, 5, 5],
  507. * [2, 0, 2]
  508. * ]
  509. * ```
  510. *
  511. * 2. An array of objects with named values. The following snippet shows only a
  512. * few settings, see the complete options set below. If the total number of
  513. * data points exceeds the series'
  514. * [turboThreshold](#series.bubble.turboThreshold), this option is not
  515. * available.
  516. * ```js
  517. * data: [{
  518. * x: 1,
  519. * y: 1,
  520. * z: 1,
  521. * name: "Point2",
  522. * color: "#00FF00"
  523. * }, {
  524. * x: 1,
  525. * y: 5,
  526. * z: 4,
  527. * name: "Point1",
  528. * color: "#FF00FF"
  529. * }]
  530. * ```
  531. *
  532. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  533. * Arrays of numeric x and y
  534. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  535. * Arrays of datetime x and y
  536. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  537. * Arrays of point.name and y
  538. * @sample {highcharts} highcharts/series/data-array-of-objects/
  539. * Config objects
  540. *
  541. * @type {Array<Array<(number|string),number>|Array<(number|string),number,number>|*>}
  542. * @extends series.line.data
  543. * @product highcharts
  544. * @apioption series.bubble.data
  545. */
  546. /**
  547. * @extends series.line.data.marker
  548. * @excluding enabledThreshold, height, radius, width
  549. * @product highcharts
  550. * @apioption series.bubble.data.marker
  551. */
  552. /**
  553. * The size value for each bubble. The bubbles' diameters are computed
  554. * based on the `z`, and controlled by series options like `minSize`,
  555. * `maxSize`, `sizeBy`, `zMin` and `zMax`.
  556. *
  557. * @type {number|null}
  558. * @product highcharts
  559. * @apioption series.bubble.data.z
  560. */
  561. /**
  562. * @excluding enabled, enabledThreshold, height, radius, width
  563. * @apioption series.bubble.marker
  564. */
  565. ''; // adds doclets above to transpiled file