WordcloudSeries.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /* *
  2. *
  3. * Experimental Highcharts module which enables visualization of a word cloud.
  4. *
  5. * (c) 2016-2020 Highsoft AS
  6. * Authors: Jon Arild Nygard
  7. *
  8. * License: www.highcharts.com/license
  9. *
  10. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  11. * */
  12. import BaseSeries from '../Core/Series/Series.js';
  13. import DrawPointMixin from '../Mixins/DrawPoint.js';
  14. var drawPoint = DrawPointMixin.drawPoint;
  15. import H from '../Core/Globals.js';
  16. var noop = H.noop;
  17. import PolygonMixin from '../Mixins/Polygon.js';
  18. var getBoundingBoxFromPolygon = PolygonMixin.getBoundingBoxFromPolygon, getPolygon = PolygonMixin.getPolygon, isPolygonsColliding = PolygonMixin.isPolygonsColliding, movePolygon = PolygonMixin.movePolygon, rotate2DToOrigin = PolygonMixin.rotate2DToOrigin, rotate2DToPoint = PolygonMixin.rotate2DToPoint;
  19. import U from '../Core/Utilities.js';
  20. var extend = U.extend, find = U.find, isArray = U.isArray, isNumber = U.isNumber, isObject = U.isObject, merge = U.merge;
  21. import './ColumnSeries.js';
  22. var Series = H.Series;
  23. /**
  24. * Detects if there is a collision between two rectangles.
  25. *
  26. * @private
  27. * @function isRectanglesIntersecting
  28. *
  29. * @param {Highcharts.PolygonBoxObject} r1
  30. * First rectangle.
  31. *
  32. * @param {Highcharts.PolygonBoxObject} r2
  33. * Second rectangle.
  34. *
  35. * @return {boolean}
  36. * Returns true if the rectangles overlap.
  37. */
  38. function isRectanglesIntersecting(r1, r2) {
  39. return !(r2.left > r1.right ||
  40. r2.right < r1.left ||
  41. r2.top > r1.bottom ||
  42. r2.bottom < r1.top);
  43. }
  44. /**
  45. * Detects if a word collides with any previously placed words.
  46. *
  47. * @private
  48. * @function intersectsAnyWord
  49. *
  50. * @param {Highcharts.Point} point
  51. * Point which the word is connected to.
  52. *
  53. * @param {Array<Highcharts.Point>} points
  54. * Previously placed points to check against.
  55. *
  56. * @return {boolean}
  57. * Returns true if there is collision.
  58. */
  59. function intersectsAnyWord(point, points) {
  60. var intersects = false, rect = point.rect, polygon = point.polygon, lastCollidedWith = point.lastCollidedWith, isIntersecting = function (p) {
  61. var result = isRectanglesIntersecting(rect, p.rect);
  62. if (result &&
  63. (point.rotation % 90 || p.rotation % 90)) {
  64. result = isPolygonsColliding(polygon, p.polygon);
  65. }
  66. return result;
  67. };
  68. // If the point has already intersected a different point, chances are they
  69. // are still intersecting. So as an enhancement we check this first.
  70. if (lastCollidedWith) {
  71. intersects = isIntersecting(lastCollidedWith);
  72. // If they no longer intersects, remove the cache from the point.
  73. if (!intersects) {
  74. delete point.lastCollidedWith;
  75. }
  76. }
  77. // If not already found, then check if we can find a point that is
  78. // intersecting.
  79. if (!intersects) {
  80. intersects = !!find(points, function (p) {
  81. var result = isIntersecting(p);
  82. if (result) {
  83. point.lastCollidedWith = p;
  84. }
  85. return result;
  86. });
  87. }
  88. return intersects;
  89. }
  90. /**
  91. * Gives a set of cordinates for an Archimedian Spiral.
  92. *
  93. * @private
  94. * @function archimedeanSpiral
  95. *
  96. * @param {number} attempt
  97. * How far along the spiral we have traversed.
  98. *
  99. * @param {Highcharts.WordcloudSpiralParamsObject} [params]
  100. * Additional parameters.
  101. *
  102. * @return {boolean|Highcharts.PositionObject}
  103. * Resulting coordinates, x and y. False if the word should be dropped from the
  104. * visualization.
  105. */
  106. function archimedeanSpiral(attempt, params) {
  107. var field = params.field, result = false, maxDelta = (field.width * field.width) + (field.height * field.height), t = attempt * 0.8; // 0.2 * 4 = 0.8. Enlarging the spiral.
  108. // Emergency brake. TODO make spiralling logic more foolproof.
  109. if (attempt <= 10000) {
  110. result = {
  111. x: t * Math.cos(t),
  112. y: t * Math.sin(t)
  113. };
  114. if (!(Math.min(Math.abs(result.x), Math.abs(result.y)) < maxDelta)) {
  115. result = false;
  116. }
  117. }
  118. return result;
  119. }
  120. /**
  121. * Gives a set of cordinates for an rectangular spiral.
  122. *
  123. * @private
  124. * @function squareSpiral
  125. *
  126. * @param {number} attempt
  127. * How far along the spiral we have traversed.
  128. *
  129. * @param {Highcharts.WordcloudSpiralParamsObject} [params]
  130. * Additional parameters.
  131. *
  132. * @return {boolean|Highcharts.PositionObject}
  133. * Resulting coordinates, x and y. False if the word should be dropped from the
  134. * visualization.
  135. */
  136. function squareSpiral(attempt, params) {
  137. var a = attempt * 4, k = Math.ceil((Math.sqrt(a) - 1) / 2), t = 2 * k + 1, m = Math.pow(t, 2), isBoolean = function (x) {
  138. return typeof x === 'boolean';
  139. }, result = false;
  140. t -= 1;
  141. if (attempt <= 10000) {
  142. if (isBoolean(result) && a >= m - t) {
  143. result = {
  144. x: k - (m - a),
  145. y: -k
  146. };
  147. }
  148. m -= t;
  149. if (isBoolean(result) && a >= m - t) {
  150. result = {
  151. x: -k,
  152. y: -k + (m - a)
  153. };
  154. }
  155. m -= t;
  156. if (isBoolean(result)) {
  157. if (a >= m - t) {
  158. result = {
  159. x: -k + (m - a),
  160. y: k
  161. };
  162. }
  163. else {
  164. result = {
  165. x: k,
  166. y: k - (m - a - t)
  167. };
  168. }
  169. }
  170. result.x *= 5;
  171. result.y *= 5;
  172. }
  173. return result;
  174. }
  175. /**
  176. * Gives a set of cordinates for an rectangular spiral.
  177. *
  178. * @private
  179. * @function rectangularSpiral
  180. *
  181. * @param {number} attempt
  182. * How far along the spiral we have traversed.
  183. *
  184. * @param {Highcharts.WordcloudSpiralParamsObject} [params]
  185. * Additional parameters.
  186. *
  187. * @return {boolean|Higcharts.PositionObject}
  188. * Resulting coordinates, x and y. False if the word should be dropped from the
  189. * visualization.
  190. */
  191. function rectangularSpiral(attempt, params) {
  192. var result = squareSpiral(attempt, params), field = params.field;
  193. if (result) {
  194. result.x *= field.ratioX;
  195. result.y *= field.ratioY;
  196. }
  197. return result;
  198. }
  199. /**
  200. * @private
  201. * @function getRandomPosition
  202. *
  203. * @param {number} size
  204. * Random factor.
  205. *
  206. * @return {number}
  207. * Random position.
  208. */
  209. function getRandomPosition(size) {
  210. return Math.round((size * (Math.random() + 0.5)) / 2);
  211. }
  212. /**
  213. * Calculates the proper scale to fit the cloud inside the plotting area.
  214. *
  215. * @private
  216. * @function getScale
  217. *
  218. * @param {number} targetWidth
  219. * Width of target area.
  220. *
  221. * @param {number} targetHeight
  222. * Height of target area.
  223. *
  224. * @param {object} field
  225. * The playing field.
  226. *
  227. * @param {Highcharts.Series} series
  228. * Series object.
  229. *
  230. * @return {number}
  231. * Returns the value to scale the playing field up to the size of the target
  232. * area.
  233. */
  234. function getScale(targetWidth, targetHeight, field) {
  235. var height = Math.max(Math.abs(field.top), Math.abs(field.bottom)) * 2, width = Math.max(Math.abs(field.left), Math.abs(field.right)) * 2, scaleX = width > 0 ? 1 / width * targetWidth : 1, scaleY = height > 0 ? 1 / height * targetHeight : 1;
  236. return Math.min(scaleX, scaleY);
  237. }
  238. /**
  239. * Calculates what is called the playing field. The field is the area which all
  240. * the words are allowed to be positioned within. The area is proportioned to
  241. * match the target aspect ratio.
  242. *
  243. * @private
  244. * @function getPlayingField
  245. *
  246. * @param {number} targetWidth
  247. * Width of the target area.
  248. *
  249. * @param {number} targetHeight
  250. * Height of the target area.
  251. *
  252. * @param {Array<Highcharts.Point>} data
  253. * Array of points.
  254. *
  255. * @param {object} data.dimensions
  256. * The height and width of the word.
  257. *
  258. * @return {object}
  259. * The width and height of the playing field.
  260. */
  261. function getPlayingField(targetWidth, targetHeight, data) {
  262. var info = data.reduce(function (obj, point) {
  263. var dimensions = point.dimensions, x = Math.max(dimensions.width, dimensions.height);
  264. // Find largest height.
  265. obj.maxHeight = Math.max(obj.maxHeight, dimensions.height);
  266. // Find largest width.
  267. obj.maxWidth = Math.max(obj.maxWidth, dimensions.width);
  268. // Sum up the total maximum area of all the words.
  269. obj.area += x * x;
  270. return obj;
  271. }, {
  272. maxHeight: 0,
  273. maxWidth: 0,
  274. area: 0
  275. }),
  276. /**
  277. * Use largest width, largest height, or root of total area to give size
  278. * to the playing field.
  279. */
  280. x = Math.max(info.maxHeight, // Have enough space for the tallest word
  281. info.maxWidth, // Have enough space for the broadest word
  282. // Adjust 15% to account for close packing of words
  283. Math.sqrt(info.area) * 0.85), ratioX = targetWidth > targetHeight ? targetWidth / targetHeight : 1, ratioY = targetHeight > targetWidth ? targetHeight / targetWidth : 1;
  284. return {
  285. width: x * ratioX,
  286. height: x * ratioY,
  287. ratioX: ratioX,
  288. ratioY: ratioY
  289. };
  290. }
  291. /**
  292. * Calculates a number of degrees to rotate, based upon a number of orientations
  293. * within a range from-to.
  294. *
  295. * @private
  296. * @function getRotation
  297. *
  298. * @param {number} [orientations]
  299. * Number of orientations.
  300. *
  301. * @param {number} [index]
  302. * Index of point, used to decide orientation.
  303. *
  304. * @param {number} [from]
  305. * The smallest degree of rotation.
  306. *
  307. * @param {number} [to]
  308. * The largest degree of rotation.
  309. *
  310. * @return {boolean|number}
  311. * Returns the resulting rotation for the word. Returns false if invalid input
  312. * parameters.
  313. */
  314. function getRotation(orientations, index, from, to) {
  315. var result = false, // Default to false
  316. range, intervals, orientation;
  317. // Check if we have valid input parameters.
  318. if (isNumber(orientations) &&
  319. isNumber(index) &&
  320. isNumber(from) &&
  321. isNumber(to) &&
  322. orientations > 0 &&
  323. index > -1 &&
  324. to > from) {
  325. range = to - from;
  326. intervals = range / (orientations - 1 || 1);
  327. orientation = index % orientations;
  328. result = from + (orientation * intervals);
  329. }
  330. return result;
  331. }
  332. /**
  333. * Calculates the spiral positions and store them in scope for quick access.
  334. *
  335. * @private
  336. * @function getSpiral
  337. *
  338. * @param {Function} fn
  339. * The spiral function.
  340. *
  341. * @param {object} params
  342. * Additional parameters for the spiral.
  343. *
  344. * @return {Function}
  345. * Function with access to spiral positions.
  346. */
  347. function getSpiral(fn, params) {
  348. var length = 10000, i, arr = [];
  349. for (i = 1; i < length; i++) {
  350. arr.push(fn(i, params)); // @todo unnecessary amount of precaclulation
  351. }
  352. return function (attempt) {
  353. return attempt <= length ? arr[attempt - 1] : false;
  354. };
  355. }
  356. /**
  357. * Detects if a word is placed outside the playing field.
  358. *
  359. * @private
  360. * @function outsidePlayingField
  361. *
  362. * @param {Highcharts.PolygonBoxObject} rect
  363. * The word box.
  364. *
  365. * @param {Highcharts.WordcloudFieldObject} field
  366. * The width and height of the playing field.
  367. *
  368. * @return {boolean}
  369. * Returns true if the word is placed outside the field.
  370. */
  371. function outsidePlayingField(rect, field) {
  372. var playingField = {
  373. left: -(field.width / 2),
  374. right: field.width / 2,
  375. top: -(field.height / 2),
  376. bottom: field.height / 2
  377. };
  378. return !(playingField.left < rect.left &&
  379. playingField.right > rect.right &&
  380. playingField.top < rect.top &&
  381. playingField.bottom > rect.bottom);
  382. }
  383. /**
  384. * Check if a point intersects with previously placed words, or if it goes
  385. * outside the field boundaries. If a collision, then try to adjusts the
  386. * position.
  387. *
  388. * @private
  389. * @function intersectionTesting
  390. *
  391. * @param {Highcharts.Point} point
  392. * Point to test for intersections.
  393. *
  394. * @param {Highcharts.WordcloudTestOptionsObject} options
  395. * Options object.
  396. *
  397. * @return {boolean|Highcharts.PositionObject}
  398. * Returns an object with how much to correct the positions. Returns false if
  399. * the word should not be placed at all.
  400. */
  401. function intersectionTesting(point, options) {
  402. var placed = options.placed, field = options.field, rectangle = options.rectangle, polygon = options.polygon, spiral = options.spiral, attempt = 1, delta = {
  403. x: 0,
  404. y: 0
  405. },
  406. // Make a copy to update values during intersection testing.
  407. rect = point.rect = extend({}, rectangle);
  408. point.polygon = polygon;
  409. point.rotation = options.rotation;
  410. /* while w intersects any previously placed words:
  411. do {
  412. move w a little bit along a spiral path
  413. } while any part of w is outside the playing field and
  414. the spiral radius is still smallish */
  415. while (delta !== false &&
  416. (intersectsAnyWord(point, placed) ||
  417. outsidePlayingField(rect, field))) {
  418. delta = spiral(attempt);
  419. if (isObject(delta)) {
  420. // Update the DOMRect with new positions.
  421. rect.left = rectangle.left + delta.x;
  422. rect.right = rectangle.right + delta.x;
  423. rect.top = rectangle.top + delta.y;
  424. rect.bottom = rectangle.bottom + delta.y;
  425. point.polygon = movePolygon(delta.x, delta.y, polygon);
  426. }
  427. attempt++;
  428. }
  429. return delta;
  430. }
  431. /**
  432. * Extends the playing field to have enough space to fit a given word.
  433. *
  434. * @private
  435. * @function extendPlayingField
  436. *
  437. * @param {Highcharts.WordcloudFieldObject} field
  438. * The width, height and ratios of a playing field.
  439. *
  440. * @param {Highcharts.PolygonBoxObject} rectangle
  441. * The bounding box of the word to add space for.
  442. *
  443. * @return {Highcharts.WordcloudFieldObject}
  444. * Returns the extended playing field with updated height and width.
  445. */
  446. function extendPlayingField(field, rectangle) {
  447. var height, width, ratioX, ratioY, x, extendWidth, extendHeight, result;
  448. if (isObject(field) && isObject(rectangle)) {
  449. height = (rectangle.bottom - rectangle.top);
  450. width = (rectangle.right - rectangle.left);
  451. ratioX = field.ratioX;
  452. ratioY = field.ratioY;
  453. // Use the same variable to extend both the height and width.
  454. x = ((width * ratioX) > (height * ratioY)) ? width : height;
  455. // Multiply variable with ratios to preserve aspect ratio.
  456. extendWidth = x * ratioX;
  457. extendHeight = x * ratioY;
  458. // Calculate the size of the new field after adding space for the word.
  459. result = merge(field, {
  460. // Add space on the left and right.
  461. width: field.width + (extendWidth * 2),
  462. // Add space on the top and bottom.
  463. height: field.height + (extendHeight * 2)
  464. });
  465. }
  466. else {
  467. result = field;
  468. }
  469. // Return the new extended field.
  470. return result;
  471. }
  472. /**
  473. * If a rectangle is outside a give field, then the boundaries of the field is
  474. * adjusted accordingly. Modifies the field object which is passed as the first
  475. * parameter.
  476. *
  477. * @private
  478. * @function updateFieldBoundaries
  479. *
  480. * @param {Highcharts.WordcloudFieldObject} field
  481. * The bounding box of a playing field.
  482. *
  483. * @param {Highcharts.PolygonBoxObject} rectangle
  484. * The bounding box for a placed point.
  485. *
  486. * @return {Highcharts.WordcloudFieldObject}
  487. * Returns a modified field object.
  488. */
  489. function updateFieldBoundaries(field, rectangle) {
  490. // @todo improve type checking.
  491. if (!isNumber(field.left) || field.left > rectangle.left) {
  492. field.left = rectangle.left;
  493. }
  494. if (!isNumber(field.right) || field.right < rectangle.right) {
  495. field.right = rectangle.right;
  496. }
  497. if (!isNumber(field.top) || field.top > rectangle.top) {
  498. field.top = rectangle.top;
  499. }
  500. if (!isNumber(field.bottom) || field.bottom < rectangle.bottom) {
  501. field.bottom = rectangle.bottom;
  502. }
  503. return field;
  504. }
  505. /**
  506. * A word cloud is a visualization of a set of words, where the size and
  507. * placement of a word is determined by how it is weighted.
  508. *
  509. * @sample highcharts/demo/wordcloud
  510. * Word Cloud chart
  511. *
  512. * @extends plotOptions.column
  513. * @excluding allAreas, boostThreshold, clip, colorAxis, compare,
  514. * compareBase, crisp, cropTreshold, dataGrouping, dataLabels,
  515. * depth, dragDrop, edgeColor, findNearestPointBy,
  516. * getExtremesFromAll, grouping, groupPadding, groupZPadding,
  517. * joinBy, maxPointWidth, minPointLength, navigatorOptions,
  518. * negativeColor, pointInterval, pointIntervalUnit, pointPadding,
  519. * pointPlacement, pointRange, pointStart, pointWidth, pointStart,
  520. * pointWidth, shadow, showCheckbox, showInNavigator,
  521. * softThreshold, stacking, threshold, zoneAxis, zones,
  522. * dataSorting, boostBlending
  523. * @product highcharts
  524. * @since 6.0.0
  525. * @requires modules/wordcloud
  526. * @optionparent plotOptions.wordcloud
  527. */
  528. var wordCloudOptions = {
  529. /**
  530. * If there is no space for a word on the playing field, then this option
  531. * will allow the playing field to be extended to fit the word. If false
  532. * then the word will be dropped from the visualization.
  533. *
  534. * NB! This option is currently not decided to be published in the API, and
  535. * is therefore marked as private.
  536. *
  537. * @private
  538. */
  539. allowExtendPlayingField: true,
  540. animation: {
  541. /** @internal */
  542. duration: 500
  543. },
  544. borderWidth: 0,
  545. clip: false,
  546. colorByPoint: true,
  547. /**
  548. * A threshold determining the minimum font size that can be applied to a
  549. * word.
  550. */
  551. minFontSize: 1,
  552. /**
  553. * The word with the largest weight will have a font size equal to this
  554. * value. The font size of a word is the ratio between its weight and the
  555. * largest occuring weight, multiplied with the value of maxFontSize.
  556. */
  557. maxFontSize: 25,
  558. /**
  559. * This option decides which algorithm is used for placement, and rotation
  560. * of a word. The choice of algorith is therefore a crucial part of the
  561. * resulting layout of the wordcloud. It is possible for users to add their
  562. * own custom placement strategies for use in word cloud. Read more about it
  563. * in our
  564. * [documentation](https://www.highcharts.com/docs/chart-and-series-types/word-cloud-series#custom-placement-strategies)
  565. *
  566. * @validvalue: ["center", "random"]
  567. */
  568. placementStrategy: 'center',
  569. /**
  570. * Rotation options for the words in the wordcloud.
  571. *
  572. * @sample highcharts/plotoptions/wordcloud-rotation
  573. * Word cloud with rotation
  574. */
  575. rotation: {
  576. /**
  577. * The smallest degree of rotation for a word.
  578. */
  579. from: 0,
  580. /**
  581. * The number of possible orientations for a word, within the range of
  582. * `rotation.from` and `rotation.to`. Must be a number larger than 0.
  583. */
  584. orientations: 2,
  585. /**
  586. * The largest degree of rotation for a word.
  587. */
  588. to: 90
  589. },
  590. showInLegend: false,
  591. /**
  592. * Spiral used for placing a word after the initial position experienced a
  593. * collision with either another word or the borders.
  594. * It is possible for users to add their own custom spiralling algorithms
  595. * for use in word cloud. Read more about it in our
  596. * [documentation](https://www.highcharts.com/docs/chart-and-series-types/word-cloud-series#custom-spiralling-algorithm)
  597. *
  598. * @validvalue: ["archimedean", "rectangular", "square"]
  599. */
  600. spiral: 'rectangular',
  601. /**
  602. * CSS styles for the words.
  603. *
  604. * @type {Highcharts.CSSObject}
  605. * @default {"fontFamily":"sans-serif", "fontWeight": "900"}
  606. */
  607. style: {
  608. /** @ignore-option */
  609. fontFamily: 'sans-serif',
  610. /** @ignore-option */
  611. fontWeight: '900',
  612. /** @ignore-option */
  613. whiteSpace: 'nowrap'
  614. },
  615. tooltip: {
  616. followPointer: true,
  617. pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.weight}</b><br/>'
  618. }
  619. };
  620. // Properties of the WordCloud series.
  621. var wordCloudSeries = {
  622. animate: Series.prototype.animate,
  623. animateDrilldown: noop,
  624. animateDrillupFrom: noop,
  625. setClip: noop,
  626. bindAxes: function () {
  627. var wordcloudAxis = {
  628. endOnTick: false,
  629. gridLineWidth: 0,
  630. lineWidth: 0,
  631. maxPadding: 0,
  632. startOnTick: false,
  633. title: null,
  634. tickPositions: []
  635. };
  636. Series.prototype.bindAxes.call(this);
  637. extend(this.yAxis.options, wordcloudAxis);
  638. extend(this.xAxis.options, wordcloudAxis);
  639. },
  640. pointAttribs: function (point, state) {
  641. var attribs = H.seriesTypes.column.prototype
  642. .pointAttribs.call(this, point, state);
  643. delete attribs.stroke;
  644. delete attribs['stroke-width'];
  645. return attribs;
  646. },
  647. /**
  648. * Calculates the fontSize of a word based on its weight.
  649. *
  650. * @private
  651. * @function Highcharts.Series#deriveFontSize
  652. *
  653. * @param {number} [relativeWeight=0]
  654. * The weight of the word, on a scale 0-1.
  655. *
  656. * @param {number} [maxFontSize=1]
  657. * The maximum font size of a word.
  658. *
  659. * @param {number} [minFontSize=1]
  660. * The minimum font size of a word.
  661. *
  662. * @return {number}
  663. * Returns the resulting fontSize of a word. If minFontSize is larger then
  664. * maxFontSize the result will equal minFontSize.
  665. */
  666. deriveFontSize: function deriveFontSize(relativeWeight, maxFontSize, minFontSize) {
  667. var weight = isNumber(relativeWeight) ? relativeWeight : 0, max = isNumber(maxFontSize) ? maxFontSize : 1, min = isNumber(minFontSize) ? minFontSize : 1;
  668. return Math.floor(Math.max(min, weight * max));
  669. },
  670. drawPoints: function () {
  671. var series = this, hasRendered = series.hasRendered, xAxis = series.xAxis, yAxis = series.yAxis, chart = series.chart, group = series.group, options = series.options, animation = options.animation, allowExtendPlayingField = options.allowExtendPlayingField, renderer = chart.renderer, testElement = renderer.text().add(group), placed = [], placementStrategy = series.placementStrategy[options.placementStrategy], spiral, rotation = options.rotation, scale, weights = series.points.map(function (p) {
  672. return p.weight;
  673. }), maxWeight = Math.max.apply(null, weights),
  674. // concat() prevents from sorting the original array.
  675. data = series.points.concat().sort(function (a, b) {
  676. return b.weight - a.weight; // Sort descending
  677. }), field;
  678. // Reset the scale before finding the dimensions (#11993).
  679. // SVGGRaphicsElement.getBBox() (used in SVGElement.getBBox(boolean))
  680. // returns slightly different values for the same element depending on
  681. // whether it is rendered in a group which has already defined scale
  682. // (e.g. 6) or in the group without a scale (scale = 1).
  683. series.group.attr({
  684. scaleX: 1,
  685. scaleY: 1
  686. });
  687. // Get the dimensions for each word.
  688. // Used in calculating the playing field.
  689. data.forEach(function (point) {
  690. var relativeWeight = 1 / maxWeight * point.weight, fontSize = series.deriveFontSize(relativeWeight, options.maxFontSize, options.minFontSize), css = extend({
  691. fontSize: fontSize + 'px'
  692. }, options.style), bBox;
  693. testElement.css(css).attr({
  694. x: 0,
  695. y: 0,
  696. text: point.name
  697. });
  698. bBox = testElement.getBBox(true);
  699. point.dimensions = {
  700. height: bBox.height,
  701. width: bBox.width
  702. };
  703. });
  704. // Calculate the playing field.
  705. field = getPlayingField(xAxis.len, yAxis.len, data);
  706. spiral = getSpiral(series.spirals[options.spiral], {
  707. field: field
  708. });
  709. // Draw all the points.
  710. data.forEach(function (point) {
  711. var relativeWeight = 1 / maxWeight * point.weight, fontSize = series.deriveFontSize(relativeWeight, options.maxFontSize, options.minFontSize), css = extend({
  712. fontSize: fontSize + 'px'
  713. }, options.style), placement = placementStrategy(point, {
  714. data: data,
  715. field: field,
  716. placed: placed,
  717. rotation: rotation
  718. }), attr = extend(series.pointAttribs(point, (point.selected && 'select')), {
  719. align: 'center',
  720. 'alignment-baseline': 'middle',
  721. x: placement.x,
  722. y: placement.y,
  723. text: point.name,
  724. rotation: placement.rotation
  725. }), polygon = getPolygon(placement.x, placement.y, point.dimensions.width, point.dimensions.height, placement.rotation), rectangle = getBoundingBoxFromPolygon(polygon), delta = intersectionTesting(point, {
  726. rectangle: rectangle,
  727. polygon: polygon,
  728. field: field,
  729. placed: placed,
  730. spiral: spiral,
  731. rotation: placement.rotation
  732. }), animate;
  733. // If there is no space for the word, extend the playing field.
  734. if (!delta && allowExtendPlayingField) {
  735. // Extend the playing field to fit the word.
  736. field = extendPlayingField(field, rectangle);
  737. // Run intersection testing one more time to place the word.
  738. delta = intersectionTesting(point, {
  739. rectangle: rectangle,
  740. polygon: polygon,
  741. field: field,
  742. placed: placed,
  743. spiral: spiral,
  744. rotation: placement.rotation
  745. });
  746. }
  747. // Check if point was placed, if so delete it, otherwise place it on
  748. // the correct positions.
  749. if (isObject(delta)) {
  750. attr.x += delta.x;
  751. attr.y += delta.y;
  752. rectangle.left += delta.x;
  753. rectangle.right += delta.x;
  754. rectangle.top += delta.y;
  755. rectangle.bottom += delta.y;
  756. field = updateFieldBoundaries(field, rectangle);
  757. placed.push(point);
  758. point.isNull = false;
  759. }
  760. else {
  761. point.isNull = true;
  762. }
  763. if (animation) {
  764. // Animate to new positions
  765. animate = {
  766. x: attr.x,
  767. y: attr.y
  768. };
  769. // Animate from center of chart
  770. if (!hasRendered) {
  771. attr.x = 0;
  772. attr.y = 0;
  773. // or animate from previous position
  774. }
  775. else {
  776. delete attr.x;
  777. delete attr.y;
  778. }
  779. }
  780. point.draw({
  781. animatableAttribs: animate,
  782. attribs: attr,
  783. css: css,
  784. group: group,
  785. renderer: renderer,
  786. shapeArgs: void 0,
  787. shapeType: 'text'
  788. });
  789. });
  790. // Destroy the element after use.
  791. testElement = testElement.destroy();
  792. // Scale the series group to fit within the plotArea.
  793. scale = getScale(xAxis.len, yAxis.len, field);
  794. series.group.attr({
  795. scaleX: scale,
  796. scaleY: scale
  797. });
  798. },
  799. hasData: function () {
  800. var series = this;
  801. return (isObject(series) &&
  802. series.visible === true &&
  803. isArray(series.points) &&
  804. series.points.length > 0);
  805. },
  806. // Strategies used for deciding rotation and initial position of a word. To
  807. // implement a custom strategy, have a look at the function random for
  808. // example.
  809. placementStrategy: {
  810. random: function (point, options) {
  811. var field = options.field, r = options.rotation;
  812. return {
  813. x: getRandomPosition(field.width) - (field.width / 2),
  814. y: getRandomPosition(field.height) - (field.height / 2),
  815. rotation: getRotation(r.orientations, point.index, r.from, r.to)
  816. };
  817. },
  818. center: function (point, options) {
  819. var r = options.rotation;
  820. return {
  821. x: 0,
  822. y: 0,
  823. rotation: getRotation(r.orientations, point.index, r.from, r.to)
  824. };
  825. }
  826. },
  827. pointArrayMap: ['weight'],
  828. // Spirals used for placing a word after the initial position experienced a
  829. // collision with either another word or the borders. To implement a custom
  830. // spiral, look at the function archimedeanSpiral for example.
  831. spirals: {
  832. 'archimedean': archimedeanSpiral,
  833. 'rectangular': rectangularSpiral,
  834. 'square': squareSpiral
  835. },
  836. utils: {
  837. extendPlayingField: extendPlayingField,
  838. getRotation: getRotation,
  839. isPolygonsColliding: isPolygonsColliding,
  840. rotate2DToOrigin: rotate2DToOrigin,
  841. rotate2DToPoint: rotate2DToPoint
  842. },
  843. getPlotBox: function () {
  844. var series = this, chart = series.chart, inverted = chart.inverted,
  845. // Swap axes for inverted (#2339)
  846. xAxis = series[(inverted ? 'yAxis' : 'xAxis')], yAxis = series[(inverted ? 'xAxis' : 'yAxis')], width = xAxis ? xAxis.len : chart.plotWidth, height = yAxis ? yAxis.len : chart.plotHeight, x = xAxis ? xAxis.left : chart.plotLeft, y = yAxis ? yAxis.top : chart.plotTop;
  847. return {
  848. translateX: x + (width / 2),
  849. translateY: y + (height / 2),
  850. scaleX: 1,
  851. scaleY: 1
  852. };
  853. }
  854. };
  855. // Properties of the Sunburst series.
  856. var wordCloudPoint = {
  857. draw: drawPoint,
  858. shouldDraw: function shouldDraw() {
  859. var point = this;
  860. return !point.isNull;
  861. },
  862. isValid: function isValid() {
  863. return true;
  864. },
  865. weight: 1
  866. };
  867. /**
  868. * A `wordcloud` series. If the [type](#series.wordcloud.type) option is not
  869. * specified, it is inherited from [chart.type](#chart.type).
  870. *
  871. * @extends series,plotOptions.wordcloud
  872. * @exclude dataSorting, boostThreshold, boostBlending
  873. * @product highcharts
  874. * @requires modules/wordcloud
  875. * @apioption series.wordcloud
  876. */
  877. /**
  878. * An array of data points for the series. For the `wordcloud` series type,
  879. * points can be given in the following ways:
  880. *
  881. * 1. An array of arrays with 2 values. In this case, the values correspond to
  882. * `name,weight`.
  883. * ```js
  884. * data: [
  885. * ['Lorem', 4],
  886. * ['Ipsum', 1]
  887. * ]
  888. * ```
  889. *
  890. * 2. An array of objects with named values. The following snippet shows only a
  891. * few settings, see the complete options set below. If the total number of
  892. * data points exceeds the series'
  893. * [turboThreshold](#series.arearange.turboThreshold), this option is not
  894. * available.
  895. * ```js
  896. * data: [{
  897. * name: "Lorem",
  898. * weight: 4
  899. * }, {
  900. * name: "Ipsum",
  901. * weight: 1
  902. * }]
  903. * ```
  904. *
  905. * @type {Array<Array<string,number>|*>}
  906. * @extends series.line.data
  907. * @excluding drilldown, marker, x, y
  908. * @product highcharts
  909. * @apioption series.wordcloud.data
  910. */
  911. /**
  912. * The name decides the text for a word.
  913. *
  914. * @type {string}
  915. * @since 6.0.0
  916. * @product highcharts
  917. * @apioption series.sunburst.data.name
  918. */
  919. /**
  920. * The weighting of a word. The weight decides the relative size of a word
  921. * compared to the rest of the collection.
  922. *
  923. * @type {number}
  924. * @since 6.0.0
  925. * @product highcharts
  926. * @apioption series.sunburst.data.weight
  927. */
  928. ''; // detach doclets above
  929. /**
  930. * @private
  931. * @class
  932. * @name Highcharts.seriesTypes.wordcloud
  933. *
  934. * @augments Highcharts.Series
  935. */
  936. BaseSeries.seriesType('wordcloud', 'column', wordCloudOptions, wordCloudSeries, wordCloudPoint);