SunburstSeries.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /* *
  2. *
  3. * This module implements sunburst charts in Highcharts.
  4. *
  5. * (c) 2016-2020 Highsoft AS
  6. *
  7. * Authors: Jon Arild Nygard
  8. *
  9. * License: www.highcharts.com/license
  10. *
  11. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  12. *
  13. * */
  14. import BaseSeries from '../Core/Series/Series.js';
  15. var seriesTypes = BaseSeries.seriesTypes;
  16. import CenteredSeriesMixin from '../Mixins/CenteredSeries.js';
  17. var getCenter = CenteredSeriesMixin.getCenter, getStartAndEndRadians = CenteredSeriesMixin.getStartAndEndRadians;
  18. import DrawPointMixin from '../Mixins/DrawPoint.js';
  19. var drawPoint = DrawPointMixin.drawPoint;
  20. import H from '../Core/Globals.js';
  21. var noop = H.noop;
  22. import TreeSeriesMixin from '../Mixins/TreeSeries.js';
  23. var getColor = TreeSeriesMixin.getColor, getLevelOptions = TreeSeriesMixin.getLevelOptions, setTreeValues = TreeSeriesMixin.setTreeValues, updateRootId = TreeSeriesMixin.updateRootId;
  24. import U from '../Core/Utilities.js';
  25. var correctFloat = U.correctFloat, error = U.error, extend = U.extend, isNumber = U.isNumber, isObject = U.isObject, isString = U.isString, merge = U.merge, splat = U.splat;
  26. import '../Series/LineSeries.js';
  27. import './TreemapSeries.js';
  28. var Series = H.Series, isBoolean = function (x) {
  29. return typeof x === 'boolean';
  30. }, rad2deg = 180 / Math.PI;
  31. // TODO introduce step, which should default to 1.
  32. var range = function range(from, to) {
  33. var result = [], i;
  34. if (isNumber(from) && isNumber(to) && from <= to) {
  35. for (i = from; i <= to; i++) {
  36. result.push(i);
  37. }
  38. }
  39. return result;
  40. };
  41. /**
  42. * @private
  43. * @function calculateLevelSizes
  44. *
  45. * @param {object} levelOptions
  46. * Map of level to its options.
  47. *
  48. * @param {Highcharts.Dictionary<number>} params
  49. * Object containing number parameters `innerRadius` and `outerRadius`.
  50. *
  51. * @return {Highcharts.SunburstSeriesLevelsOptions|undefined}
  52. * Returns the modified options, or undefined.
  53. */
  54. var calculateLevelSizes = function calculateLevelSizes(levelOptions, params) {
  55. var result, p = isObject(params) ? params : {}, totalWeight = 0, diffRadius, levels, levelsNotIncluded, remainingSize, from, to;
  56. if (isObject(levelOptions)) {
  57. result = merge({}, levelOptions);
  58. from = isNumber(p.from) ? p.from : 0;
  59. to = isNumber(p.to) ? p.to : 0;
  60. levels = range(from, to);
  61. levelsNotIncluded = Object.keys(result).filter(function (k) {
  62. return levels.indexOf(+k) === -1;
  63. });
  64. diffRadius = remainingSize = isNumber(p.diffRadius) ? p.diffRadius : 0;
  65. // Convert percentage to pixels.
  66. // Calculate the remaining size to divide between "weight" levels.
  67. // Calculate total weight to use in convertion from weight to pixels.
  68. levels.forEach(function (level) {
  69. var options = result[level], unit = options.levelSize.unit, value = options.levelSize.value;
  70. if (unit === 'weight') {
  71. totalWeight += value;
  72. }
  73. else if (unit === 'percentage') {
  74. options.levelSize = {
  75. unit: 'pixels',
  76. value: (value / 100) * diffRadius
  77. };
  78. remainingSize -= options.levelSize.value;
  79. }
  80. else if (unit === 'pixels') {
  81. remainingSize -= value;
  82. }
  83. });
  84. // Convert weight to pixels.
  85. levels.forEach(function (level) {
  86. var options = result[level], weight;
  87. if (options.levelSize.unit === 'weight') {
  88. weight = options.levelSize.value;
  89. result[level].levelSize = {
  90. unit: 'pixels',
  91. value: (weight / totalWeight) * remainingSize
  92. };
  93. }
  94. });
  95. // Set all levels not included in interval [from,to] to have 0 pixels.
  96. levelsNotIncluded.forEach(function (level) {
  97. result[level].levelSize = {
  98. value: 0,
  99. unit: 'pixels'
  100. };
  101. });
  102. }
  103. return result;
  104. };
  105. /**
  106. * Find a set of coordinates given a start coordinates, an angle, and a
  107. * distance.
  108. *
  109. * @private
  110. * @function getEndPoint
  111. *
  112. * @param {number} x
  113. * Start coordinate x
  114. *
  115. * @param {number} y
  116. * Start coordinate y
  117. *
  118. * @param {number} angle
  119. * Angle in radians
  120. *
  121. * @param {number} distance
  122. * Distance from start to end coordinates
  123. *
  124. * @return {Highcharts.SVGAttributes}
  125. * Returns the end coordinates, x and y.
  126. */
  127. var getEndPoint = function getEndPoint(x, y, angle, distance) {
  128. return {
  129. x: x + (Math.cos(angle) * distance),
  130. y: y + (Math.sin(angle) * distance)
  131. };
  132. };
  133. var layoutAlgorithm = function layoutAlgorithm(parent, children, options) {
  134. var startAngle = parent.start, range = parent.end - startAngle, total = parent.val, x = parent.x, y = parent.y, radius = ((options &&
  135. isObject(options.levelSize) &&
  136. isNumber(options.levelSize.value)) ?
  137. options.levelSize.value :
  138. 0), innerRadius = parent.r, outerRadius = innerRadius + radius, slicedOffset = options && isNumber(options.slicedOffset) ?
  139. options.slicedOffset :
  140. 0;
  141. return (children || []).reduce(function (arr, child) {
  142. var percentage = (1 / total) * child.val, radians = percentage * range, radiansCenter = startAngle + (radians / 2), offsetPosition = getEndPoint(x, y, radiansCenter, slicedOffset), values = {
  143. x: child.sliced ? offsetPosition.x : x,
  144. y: child.sliced ? offsetPosition.y : y,
  145. innerR: innerRadius,
  146. r: outerRadius,
  147. radius: radius,
  148. start: startAngle,
  149. end: startAngle + radians
  150. };
  151. arr.push(values);
  152. startAngle = values.end;
  153. return arr;
  154. }, []);
  155. };
  156. var getDlOptions = function getDlOptions(params) {
  157. // Set options to new object to avoid problems with scope
  158. var point = params.point, shape = isObject(params.shapeArgs) ? params.shapeArgs : {}, optionsPoint = (isObject(params.optionsPoint) ?
  159. params.optionsPoint.dataLabels :
  160. {}),
  161. // The splat was used because levels dataLabels
  162. // options doesn't work as an array
  163. optionsLevel = splat(isObject(params.level) ?
  164. params.level.dataLabels :
  165. {})[0], options = merge({
  166. style: {}
  167. }, optionsLevel, optionsPoint), rotationRad, rotation, rotationMode = options.rotationMode;
  168. if (!isNumber(options.rotation)) {
  169. if (rotationMode === 'auto' || rotationMode === 'circular') {
  170. if (point.innerArcLength < 1 &&
  171. point.outerArcLength > shape.radius) {
  172. rotationRad = 0;
  173. // Triger setTextPath function to get textOutline etc.
  174. if (point.dataLabelPath && rotationMode === 'circular') {
  175. options.textPath = {
  176. enabled: true
  177. };
  178. }
  179. }
  180. else if (point.innerArcLength > 1 &&
  181. point.outerArcLength > 1.5 * shape.radius) {
  182. if (rotationMode === 'circular') {
  183. options.textPath = {
  184. enabled: true,
  185. attributes: {
  186. dy: 5
  187. }
  188. };
  189. }
  190. else {
  191. rotationMode = 'parallel';
  192. }
  193. }
  194. else {
  195. // Trigger the destroyTextPath function
  196. if (point.dataLabel &&
  197. point.dataLabel.textPathWrapper &&
  198. rotationMode === 'circular') {
  199. options.textPath = {
  200. enabled: false
  201. };
  202. }
  203. rotationMode = 'perpendicular';
  204. }
  205. }
  206. if (rotationMode !== 'auto' && rotationMode !== 'circular') {
  207. rotationRad = (shape.end -
  208. (shape.end - shape.start) / 2);
  209. }
  210. if (rotationMode === 'parallel') {
  211. options.style.width = Math.min(shape.radius * 2.5, (point.outerArcLength + point.innerArcLength) / 2);
  212. }
  213. else {
  214. options.style.width = shape.radius;
  215. }
  216. if (rotationMode === 'perpendicular' &&
  217. point.series.chart.renderer.fontMetrics(options.style.fontSize).h > point.outerArcLength) {
  218. options.style.width = 1;
  219. }
  220. // Apply padding (#8515)
  221. options.style.width = Math.max(options.style.width - 2 * (options.padding || 0), 1);
  222. rotation = (rotationRad * rad2deg) % 180;
  223. if (rotationMode === 'parallel') {
  224. rotation -= 90;
  225. }
  226. // Prevent text from rotating upside down
  227. if (rotation > 90) {
  228. rotation -= 180;
  229. }
  230. else if (rotation < -90) {
  231. rotation += 180;
  232. }
  233. options.rotation = rotation;
  234. }
  235. if (options.textPath) {
  236. if (point.shapeExisting.innerR === 0 &&
  237. options.textPath.enabled) {
  238. // Enable rotation to render text
  239. options.rotation = 0;
  240. // Center dataLabel - disable textPath
  241. options.textPath.enabled = false;
  242. // Setting width and padding
  243. options.style.width = Math.max((point.shapeExisting.r * 2) -
  244. 2 * (options.padding || 0), 1);
  245. }
  246. else if (point.dlOptions &&
  247. point.dlOptions.textPath &&
  248. !point.dlOptions.textPath.enabled &&
  249. (rotationMode === 'circular')) {
  250. // Bring dataLabel back if was a center dataLabel
  251. options.textPath.enabled = true;
  252. }
  253. if (options.textPath.enabled) {
  254. // Enable rotation to render text
  255. options.rotation = 0;
  256. // Setting width and padding
  257. options.style.width = Math.max((point.outerArcLength +
  258. point.innerArcLength) / 2 -
  259. 2 * (options.padding || 0), 1);
  260. }
  261. }
  262. // NOTE: alignDataLabel positions the data label differntly when rotation is
  263. // 0. Avoiding this by setting rotation to a small number.
  264. if (options.rotation === 0) {
  265. options.rotation = 0.001;
  266. }
  267. return options;
  268. };
  269. var getAnimation = function getAnimation(shape, params) {
  270. var point = params.point, radians = params.radians, innerR = params.innerR, idRoot = params.idRoot, idPreviousRoot = params.idPreviousRoot, shapeExisting = params.shapeExisting, shapeRoot = params.shapeRoot, shapePreviousRoot = params.shapePreviousRoot, visible = params.visible, from = {}, to = {
  271. end: shape.end,
  272. start: shape.start,
  273. innerR: shape.innerR,
  274. r: shape.r,
  275. x: shape.x,
  276. y: shape.y
  277. };
  278. if (visible) {
  279. // Animate points in
  280. if (!point.graphic && shapePreviousRoot) {
  281. if (idRoot === point.id) {
  282. from = {
  283. start: radians.start,
  284. end: radians.end
  285. };
  286. }
  287. else {
  288. from = (shapePreviousRoot.end <= shape.start) ? {
  289. start: radians.end,
  290. end: radians.end
  291. } : {
  292. start: radians.start,
  293. end: radians.start
  294. };
  295. }
  296. // Animate from center and outwards.
  297. from.innerR = from.r = innerR;
  298. }
  299. }
  300. else {
  301. // Animate points out
  302. if (point.graphic) {
  303. if (idPreviousRoot === point.id) {
  304. to = {
  305. innerR: innerR,
  306. r: innerR
  307. };
  308. }
  309. else if (shapeRoot) {
  310. to = (shapeRoot.end <= shapeExisting.start) ?
  311. {
  312. innerR: innerR,
  313. r: innerR,
  314. start: radians.end,
  315. end: radians.end
  316. } : {
  317. innerR: innerR,
  318. r: innerR,
  319. start: radians.start,
  320. end: radians.start
  321. };
  322. }
  323. }
  324. }
  325. return {
  326. from: from,
  327. to: to
  328. };
  329. };
  330. var getDrillId = function getDrillId(point, idRoot, mapIdToNode) {
  331. var drillId, node = point.node, nodeRoot;
  332. if (!node.isLeaf) {
  333. // When it is the root node, the drillId should be set to parent.
  334. if (idRoot === point.id) {
  335. nodeRoot = mapIdToNode[idRoot];
  336. drillId = nodeRoot.parent;
  337. }
  338. else {
  339. drillId = point.id;
  340. }
  341. }
  342. return drillId;
  343. };
  344. var getLevelFromAndTo = function getLevelFromAndTo(_a) {
  345. var level = _a.level, height = _a.height;
  346. // Never displays level below 1
  347. var from = level > 0 ? level : 1;
  348. var to = level + height;
  349. return { from: from, to: to };
  350. };
  351. var cbSetTreeValuesBefore = function before(node, options) {
  352. var mapIdToNode = options.mapIdToNode, nodeParent = mapIdToNode[node.parent], series = options.series, chart = series.chart, points = series.points, point = points[node.i], colors = (series.options.colors || chart && chart.options.colors), colorInfo = getColor(node, {
  353. colors: colors,
  354. colorIndex: series.colorIndex,
  355. index: options.index,
  356. mapOptionsToLevel: options.mapOptionsToLevel,
  357. parentColor: nodeParent && nodeParent.color,
  358. parentColorIndex: nodeParent && nodeParent.colorIndex,
  359. series: options.series,
  360. siblings: options.siblings
  361. });
  362. node.color = colorInfo.color;
  363. node.colorIndex = colorInfo.colorIndex;
  364. if (point) {
  365. point.color = node.color;
  366. point.colorIndex = node.colorIndex;
  367. // Set slicing on node, but avoid slicing the top node.
  368. node.sliced = (node.id !== options.idRoot) ? point.sliced : false;
  369. }
  370. return node;
  371. };
  372. /**
  373. * A Sunburst displays hierarchical data, where a level in the hierarchy is
  374. * represented by a circle. The center represents the root node of the tree.
  375. * The visualization bears a resemblance to both treemap and pie charts.
  376. *
  377. * @sample highcharts/demo/sunburst
  378. * Sunburst chart
  379. *
  380. * @extends plotOptions.pie
  381. * @excluding allAreas, clip, colorAxis, colorKey, compare, compareBase,
  382. * dataGrouping, depth, dragDrop, endAngle, gapSize, gapUnit,
  383. * ignoreHiddenPoint, innerSize, joinBy, legendType, linecap,
  384. * minSize, navigatorOptions, pointRange
  385. * @product highcharts
  386. * @requires modules/sunburst.js
  387. * @optionparent plotOptions.sunburst
  388. * @private
  389. */
  390. var sunburstOptions = {
  391. /**
  392. * Set options on specific levels. Takes precedence over series options,
  393. * but not point options.
  394. *
  395. * @sample highcharts/demo/sunburst
  396. * Sunburst chart
  397. *
  398. * @type {Array<*>}
  399. * @apioption plotOptions.sunburst.levels
  400. */
  401. /**
  402. * Can set a `borderColor` on all points which lies on the same level.
  403. *
  404. * @type {Highcharts.ColorString}
  405. * @apioption plotOptions.sunburst.levels.borderColor
  406. */
  407. /**
  408. * Can set a `borderWidth` on all points which lies on the same level.
  409. *
  410. * @type {number}
  411. * @apioption plotOptions.sunburst.levels.borderWidth
  412. */
  413. /**
  414. * Can set a `borderDashStyle` on all points which lies on the same level.
  415. *
  416. * @type {Highcharts.DashStyleValue}
  417. * @apioption plotOptions.sunburst.levels.borderDashStyle
  418. */
  419. /**
  420. * Can set a `color` on all points which lies on the same level.
  421. *
  422. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  423. * @apioption plotOptions.sunburst.levels.color
  424. */
  425. /**
  426. * Can set a `colorVariation` on all points which lies on the same level.
  427. *
  428. * @apioption plotOptions.sunburst.levels.colorVariation
  429. */
  430. /**
  431. * The key of a color variation. Currently supports `brightness` only.
  432. *
  433. * @type {string}
  434. * @apioption plotOptions.sunburst.levels.colorVariation.key
  435. */
  436. /**
  437. * The ending value of a color variation. The last sibling will receive this
  438. * value.
  439. *
  440. * @type {number}
  441. * @apioption plotOptions.sunburst.levels.colorVariation.to
  442. */
  443. /**
  444. * Can set `dataLabels` on all points which lies on the same level.
  445. *
  446. * @extends plotOptions.sunburst.dataLabels
  447. * @apioption plotOptions.sunburst.levels.dataLabels
  448. */
  449. /**
  450. * Can set a `levelSize` on all points which lies on the same level.
  451. *
  452. * @type {object}
  453. * @apioption plotOptions.sunburst.levels.levelSize
  454. */
  455. /**
  456. * Can set a `rotation` on all points which lies on the same level.
  457. *
  458. * @type {number}
  459. * @apioption plotOptions.sunburst.levels.rotation
  460. */
  461. /**
  462. * Can set a `rotationMode` on all points which lies on the same level.
  463. *
  464. * @type {string}
  465. * @apioption plotOptions.sunburst.levels.rotationMode
  466. */
  467. /**
  468. * When enabled the user can click on a point which is a parent and
  469. * zoom in on its children. Deprecated and replaced by
  470. * [allowTraversingTree](#plotOptions.sunburst.allowTraversingTree).
  471. *
  472. * @deprecated
  473. * @type {boolean}
  474. * @default false
  475. * @since 6.0.0
  476. * @product highcharts
  477. * @apioption plotOptions.sunburst.allowDrillToNode
  478. */
  479. /**
  480. * When enabled the user can click on a point which is a parent and
  481. * zoom in on its children.
  482. *
  483. * @type {boolean}
  484. * @default false
  485. * @since 7.0.3
  486. * @product highcharts
  487. * @apioption plotOptions.sunburst.allowTraversingTree
  488. */
  489. /**
  490. * The center of the sunburst chart relative to the plot area. Can be
  491. * percentages or pixel values.
  492. *
  493. * @sample {highcharts} highcharts/plotoptions/pie-center/
  494. * Centered at 100, 100
  495. *
  496. * @type {Array<number|string>}
  497. * @default ["50%", "50%"]
  498. * @product highcharts
  499. */
  500. center: ['50%', '50%'],
  501. colorByPoint: false,
  502. /**
  503. * Disable inherited opacity from Treemap series.
  504. *
  505. * @ignore-option
  506. */
  507. opacity: 1,
  508. /**
  509. * @declare Highcharts.SeriesSunburstDataLabelsOptionsObject
  510. */
  511. dataLabels: {
  512. allowOverlap: true,
  513. defer: true,
  514. /**
  515. * Decides how the data label will be rotated relative to the perimeter
  516. * of the sunburst. Valid values are `auto`, `circular`, `parallel` and
  517. * `perpendicular`. When `auto`, the best fit will be
  518. * computed for the point. The `circular` option works similiar
  519. * to `auto`, but uses the `textPath` feature - labels are curved,
  520. * resulting in a better layout, however multiple lines and
  521. * `textOutline` are not supported.
  522. *
  523. * The `series.rotation` option takes precedence over `rotationMode`.
  524. *
  525. * @type {string}
  526. * @sample {highcharts} highcharts/plotoptions/sunburst-datalabels-rotationmode-circular/
  527. * Circular rotation mode
  528. * @validvalue ["auto", "perpendicular", "parallel", "circular"]
  529. * @since 6.0.0
  530. */
  531. rotationMode: 'auto',
  532. style: {
  533. /** @internal */
  534. textOverflow: 'ellipsis'
  535. }
  536. },
  537. /**
  538. * Which point to use as a root in the visualization.
  539. *
  540. * @type {string}
  541. */
  542. rootId: void 0,
  543. /**
  544. * Used together with the levels and `allowDrillToNode` options. When
  545. * set to false the first level visible when drilling is considered
  546. * to be level one. Otherwise the level will be the same as the tree
  547. * structure.
  548. */
  549. levelIsConstant: true,
  550. /**
  551. * Determines the width of the ring per level.
  552. *
  553. * @sample {highcharts} highcharts/plotoptions/sunburst-levelsize/
  554. * Sunburst with various sizes per level
  555. *
  556. * @since 6.0.5
  557. */
  558. levelSize: {
  559. /**
  560. * The value used for calculating the width of the ring. Its' affect is
  561. * determined by `levelSize.unit`.
  562. *
  563. * @sample {highcharts} highcharts/plotoptions/sunburst-levelsize/
  564. * Sunburst with various sizes per level
  565. */
  566. value: 1,
  567. /**
  568. * How to interpret `levelSize.value`.
  569. *
  570. * - `percentage` gives a width relative to result of outer radius minus
  571. * inner radius.
  572. *
  573. * - `pixels` gives the ring a fixed width in pixels.
  574. *
  575. * - `weight` takes the remaining width after percentage and pixels, and
  576. * distributes it accross all "weighted" levels. The value relative to
  577. * the sum of all weights determines the width.
  578. *
  579. * @sample {highcharts} highcharts/plotoptions/sunburst-levelsize/
  580. * Sunburst with various sizes per level
  581. *
  582. * @validvalue ["percentage", "pixels", "weight"]
  583. */
  584. unit: 'weight'
  585. },
  586. /**
  587. * Options for the button appearing when traversing down in a treemap.
  588. *
  589. * @extends plotOptions.treemap.traverseUpButton
  590. * @since 6.0.0
  591. * @apioption plotOptions.sunburst.traverseUpButton
  592. */
  593. /**
  594. * If a point is sliced, moved out from the center, how many pixels
  595. * should it be moved?.
  596. *
  597. * @sample highcharts/plotoptions/sunburst-sliced
  598. * Sliced sunburst
  599. *
  600. * @since 6.0.4
  601. */
  602. slicedOffset: 10
  603. };
  604. // Properties of the Sunburst series.
  605. var sunburstSeries = {
  606. drawDataLabels: noop,
  607. drawPoints: function drawPoints() {
  608. var series = this, mapOptionsToLevel = series.mapOptionsToLevel, shapeRoot = series.shapeRoot, group = series.group, hasRendered = series.hasRendered, idRoot = series.rootNode, idPreviousRoot = series.idPreviousRoot, nodeMap = series.nodeMap, nodePreviousRoot = nodeMap[idPreviousRoot], shapePreviousRoot = nodePreviousRoot && nodePreviousRoot.shapeArgs, points = series.points, radians = series.startAndEndRadians, chart = series.chart, optionsChart = chart && chart.options && chart.options.chart || {}, animation = (isBoolean(optionsChart.animation) ?
  609. optionsChart.animation :
  610. true), positions = series.center, center = {
  611. x: positions[0],
  612. y: positions[1]
  613. }, innerR = positions[3] / 2, renderer = series.chart.renderer, animateLabels, animateLabelsCalled = false, addedHack = false, hackDataLabelAnimation = !!(animation &&
  614. hasRendered &&
  615. idRoot !== idPreviousRoot &&
  616. series.dataLabelsGroup);
  617. if (hackDataLabelAnimation) {
  618. series.dataLabelsGroup.attr({ opacity: 0 });
  619. animateLabels = function () {
  620. var s = series;
  621. animateLabelsCalled = true;
  622. if (s.dataLabelsGroup) {
  623. s.dataLabelsGroup.animate({
  624. opacity: 1,
  625. visibility: 'visible'
  626. });
  627. }
  628. };
  629. }
  630. points.forEach(function (point) {
  631. var node = point.node, level = mapOptionsToLevel[node.level], shapeExisting = point.shapeExisting || {}, shape = node.shapeArgs || {}, animationInfo, onComplete, visible = !!(node.visible && node.shapeArgs);
  632. if (hasRendered && animation) {
  633. animationInfo = getAnimation(shape, {
  634. center: center,
  635. point: point,
  636. radians: radians,
  637. innerR: innerR,
  638. idRoot: idRoot,
  639. idPreviousRoot: idPreviousRoot,
  640. shapeExisting: shapeExisting,
  641. shapeRoot: shapeRoot,
  642. shapePreviousRoot: shapePreviousRoot,
  643. visible: visible
  644. });
  645. }
  646. else {
  647. // When animation is disabled, attr is called from animation.
  648. animationInfo = {
  649. to: shape,
  650. from: {}
  651. };
  652. }
  653. extend(point, {
  654. shapeExisting: shape,
  655. tooltipPos: [shape.plotX, shape.plotY],
  656. drillId: getDrillId(point, idRoot, nodeMap),
  657. name: '' + (point.name || point.id || point.index),
  658. plotX: shape.plotX,
  659. plotY: shape.plotY,
  660. value: node.val,
  661. isNull: !visible // used for dataLabels & point.draw
  662. });
  663. point.dlOptions = getDlOptions({
  664. point: point,
  665. level: level,
  666. optionsPoint: point.options,
  667. shapeArgs: shape
  668. });
  669. if (!addedHack && visible) {
  670. addedHack = true;
  671. onComplete = animateLabels;
  672. }
  673. point.draw({
  674. animatableAttribs: animationInfo.to,
  675. attribs: extend(animationInfo.from, (!chart.styledMode && series.pointAttribs(point, (point.selected && 'select')))),
  676. onComplete: onComplete,
  677. group: group,
  678. renderer: renderer,
  679. shapeType: 'arc',
  680. shapeArgs: shape
  681. });
  682. });
  683. // Draw data labels after points
  684. // TODO draw labels one by one to avoid addtional looping
  685. if (hackDataLabelAnimation && addedHack) {
  686. series.hasRendered = false;
  687. series.options.dataLabels.defer = true;
  688. Series.prototype.drawDataLabels.call(series);
  689. series.hasRendered = true;
  690. // If animateLabels is called before labels were hidden, then call
  691. // it again.
  692. if (animateLabelsCalled) {
  693. animateLabels();
  694. }
  695. }
  696. else {
  697. Series.prototype.drawDataLabels.call(series);
  698. }
  699. },
  700. pointAttribs: seriesTypes.column.prototype.pointAttribs,
  701. // The layout algorithm for the levels
  702. layoutAlgorithm: layoutAlgorithm,
  703. // Set the shape arguments on the nodes. Recursive from root down.
  704. setShapeArgs: function (parent, parentValues, mapOptionsToLevel) {
  705. var childrenValues = [], level = parent.level + 1, options = mapOptionsToLevel[level],
  706. // Collect all children which should be included
  707. children = parent.children.filter(function (n) {
  708. return n.visible;
  709. }), twoPi = 6.28; // Two times Pi.
  710. childrenValues = this.layoutAlgorithm(parentValues, children, options);
  711. children.forEach(function (child, index) {
  712. var values = childrenValues[index], angle = values.start + ((values.end - values.start) / 2), radius = values.innerR + ((values.r - values.innerR) / 2), radians = (values.end - values.start), isCircle = (values.innerR === 0 && radians > twoPi), center = (isCircle ?
  713. { x: values.x, y: values.y } :
  714. getEndPoint(values.x, values.y, angle, radius)), val = (child.val ?
  715. (child.childrenTotal > child.val ?
  716. child.childrenTotal :
  717. child.val) :
  718. child.childrenTotal);
  719. // The inner arc length is a convenience for data label filters.
  720. if (this.points[child.i]) {
  721. this.points[child.i].innerArcLength = radians * values.innerR;
  722. this.points[child.i].outerArcLength = radians * values.r;
  723. }
  724. child.shapeArgs = merge(values, {
  725. plotX: center.x,
  726. plotY: center.y + 4 * Math.abs(Math.cos(angle))
  727. });
  728. child.values = merge(values, {
  729. val: val
  730. });
  731. // If node has children, then call method recursively
  732. if (child.children.length) {
  733. this.setShapeArgs(child, child.values, mapOptionsToLevel);
  734. }
  735. }, this);
  736. },
  737. translate: function translate() {
  738. var series = this, options = series.options, positions = series.center = getCenter.call(series), radians = series.startAndEndRadians = getStartAndEndRadians(options.startAngle, options.endAngle), innerRadius = positions[3] / 2, outerRadius = positions[2] / 2, diffRadius = outerRadius - innerRadius,
  739. // NOTE: updateRootId modifies series.
  740. rootId = updateRootId(series), mapIdToNode = series.nodeMap, mapOptionsToLevel, idTop, nodeRoot = mapIdToNode && mapIdToNode[rootId], nodeTop, tree, values, nodeIds = {};
  741. series.shapeRoot = nodeRoot && nodeRoot.shapeArgs;
  742. // Call prototype function
  743. Series.prototype.translate.call(series);
  744. // @todo Only if series.isDirtyData is true
  745. tree = series.tree = series.getTree();
  746. // Render traverseUpButton, after series.nodeMap i calculated.
  747. series.renderTraverseUpButton(rootId);
  748. mapIdToNode = series.nodeMap;
  749. nodeRoot = mapIdToNode[rootId];
  750. idTop = isString(nodeRoot.parent) ? nodeRoot.parent : '';
  751. nodeTop = mapIdToNode[idTop];
  752. var _a = getLevelFromAndTo(nodeRoot), from = _a.from, to = _a.to;
  753. mapOptionsToLevel = getLevelOptions({
  754. from: from,
  755. levels: series.options.levels,
  756. to: to,
  757. defaults: {
  758. colorByPoint: options.colorByPoint,
  759. dataLabels: options.dataLabels,
  760. levelIsConstant: options.levelIsConstant,
  761. levelSize: options.levelSize,
  762. slicedOffset: options.slicedOffset
  763. }
  764. });
  765. // NOTE consider doing calculateLevelSizes in a callback to
  766. // getLevelOptions
  767. mapOptionsToLevel = calculateLevelSizes(mapOptionsToLevel, {
  768. diffRadius: diffRadius,
  769. from: from,
  770. to: to
  771. });
  772. // TODO Try to combine setTreeValues & setColorRecursive to avoid
  773. // unnecessary looping.
  774. setTreeValues(tree, {
  775. before: cbSetTreeValuesBefore,
  776. idRoot: rootId,
  777. levelIsConstant: options.levelIsConstant,
  778. mapOptionsToLevel: mapOptionsToLevel,
  779. mapIdToNode: mapIdToNode,
  780. points: series.points,
  781. series: series
  782. });
  783. values = mapIdToNode[''].shapeArgs = {
  784. end: radians.end,
  785. r: innerRadius,
  786. start: radians.start,
  787. val: nodeRoot.val,
  788. x: positions[0],
  789. y: positions[1]
  790. };
  791. this.setShapeArgs(nodeTop, values, mapOptionsToLevel);
  792. // Set mapOptionsToLevel on series for use in drawPoints.
  793. series.mapOptionsToLevel = mapOptionsToLevel;
  794. // #10669 - verify if all nodes have unique ids
  795. series.data.forEach(function (child) {
  796. if (nodeIds[child.id]) {
  797. error(31, false, series.chart);
  798. }
  799. // map
  800. nodeIds[child.id] = true;
  801. });
  802. // reset object
  803. nodeIds = {};
  804. },
  805. alignDataLabel: function (point, dataLabel, labelOptions) {
  806. if (labelOptions.textPath && labelOptions.textPath.enabled) {
  807. return;
  808. }
  809. return seriesTypes.treemap.prototype.alignDataLabel
  810. .apply(this, arguments);
  811. },
  812. // Animate the slices in. Similar to the animation of polar charts.
  813. animate: function (init) {
  814. var chart = this.chart, center = [
  815. chart.plotWidth / 2,
  816. chart.plotHeight / 2
  817. ], plotLeft = chart.plotLeft, plotTop = chart.plotTop, attribs, group = this.group;
  818. // Initialize the animation
  819. if (init) {
  820. // Scale down the group and place it in the center
  821. attribs = {
  822. translateX: center[0] + plotLeft,
  823. translateY: center[1] + plotTop,
  824. scaleX: 0.001,
  825. scaleY: 0.001,
  826. rotation: 10,
  827. opacity: 0.01
  828. };
  829. group.attr(attribs);
  830. // Run the animation
  831. }
  832. else {
  833. attribs = {
  834. translateX: plotLeft,
  835. translateY: plotTop,
  836. scaleX: 1,
  837. scaleY: 1,
  838. rotation: 0,
  839. opacity: 1
  840. };
  841. group.animate(attribs, this.options.animation);
  842. }
  843. },
  844. utils: {
  845. calculateLevelSizes: calculateLevelSizes,
  846. getLevelFromAndTo: getLevelFromAndTo,
  847. range: range
  848. }
  849. };
  850. // Properties of the Sunburst series.
  851. var sunburstPoint = {
  852. draw: drawPoint,
  853. shouldDraw: function shouldDraw() {
  854. return !this.isNull;
  855. },
  856. isValid: function isValid() {
  857. return true;
  858. },
  859. getDataLabelPath: function (label) {
  860. var renderer = this.series.chart.renderer, shapeArgs = this.shapeExisting, start = shapeArgs.start, end = shapeArgs.end, angle = start + (end - start) / 2, // arc middle value
  861. upperHalf = angle < 0 &&
  862. angle > -Math.PI ||
  863. angle > Math.PI, r = (shapeArgs.r + (label.options.distance || 0)), moreThanHalf;
  864. // Check if point is a full circle
  865. if (start === -Math.PI / 2 &&
  866. correctFloat(end) === correctFloat(Math.PI * 1.5)) {
  867. start = -Math.PI + Math.PI / 360;
  868. end = -Math.PI / 360;
  869. upperHalf = true;
  870. }
  871. // Check if dataLabels should be render in the
  872. // upper half of the circle
  873. if (end - start > Math.PI) {
  874. upperHalf = false;
  875. moreThanHalf = true;
  876. }
  877. if (this.dataLabelPath) {
  878. this.dataLabelPath = this.dataLabelPath.destroy();
  879. }
  880. this.dataLabelPath = renderer
  881. .arc({
  882. open: true,
  883. longArc: moreThanHalf ? 1 : 0
  884. })
  885. // Add it inside the data label group so it gets destroyed
  886. // with the label
  887. .add(label);
  888. this.dataLabelPath.attr({
  889. start: (upperHalf ? start : end),
  890. end: (upperHalf ? end : start),
  891. clockwise: +upperHalf,
  892. x: shapeArgs.x,
  893. y: shapeArgs.y,
  894. r: (r + shapeArgs.innerR) / 2
  895. });
  896. return this.dataLabelPath;
  897. }
  898. };
  899. /**
  900. * A `sunburst` series. If the [type](#series.sunburst.type) option is
  901. * not specified, it is inherited from [chart.type](#chart.type).
  902. *
  903. * @extends series,plotOptions.sunburst
  904. * @excluding dataParser, dataURL, stack, dataSorting, boostThreshold,
  905. * boostBlending
  906. * @product highcharts
  907. * @requires modules/sunburst.js
  908. * @apioption series.sunburst
  909. */
  910. /**
  911. * @type {Array<number|null|*>}
  912. * @extends series.treemap.data
  913. * @excluding x, y
  914. * @product highcharts
  915. * @apioption series.sunburst.data
  916. */
  917. /**
  918. * @type {Highcharts.SeriesSunburstDataLabelsOptionsObject|Array<Highcharts.SeriesSunburstDataLabelsOptionsObject>}
  919. * @product highcharts
  920. * @apioption series.sunburst.data.dataLabels
  921. */
  922. /**
  923. * The value of the point, resulting in a relative area of the point
  924. * in the sunburst.
  925. *
  926. * @type {number|null}
  927. * @since 6.0.0
  928. * @product highcharts
  929. * @apioption series.sunburst.data.value
  930. */
  931. /**
  932. * Use this option to build a tree structure. The value should be the id of the
  933. * point which is the parent. If no points has a matching id, or this option is
  934. * undefined, then the parent will be set to the root.
  935. *
  936. * @type {string}
  937. * @since 6.0.0
  938. * @product highcharts
  939. * @apioption series.sunburst.data.parent
  940. */
  941. /**
  942. * Whether to display a slice offset from the center. When a sunburst point is
  943. * sliced, its children are also offset.
  944. *
  945. * @sample highcharts/plotoptions/sunburst-sliced
  946. * Sliced sunburst
  947. *
  948. * @type {boolean}
  949. * @default false
  950. * @since 6.0.4
  951. * @product highcharts
  952. * @apioption series.sunburst.data.sliced
  953. */
  954. ''; // detach doclets above
  955. /**
  956. * @private
  957. * @class
  958. * @name Highcharts.seriesTypes.sunburst
  959. *
  960. * @augments Highcharts.Series
  961. */
  962. BaseSeries.seriesType('sunburst', 'treemap', sunburstOptions, sunburstSeries, sunburstPoint);