item-series.src.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * @license Highcharts JS v8.2.2 (2020-10-22)
  3. *
  4. * Item series type for Highcharts
  5. *
  6. * (c) 2019 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/item-series', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Series/ItemSeries.js', [_modules['Core/Series/Series.js'], _modules['Core/Globals.js'], _modules['Core/Options.js'], _modules['Core/Utilities.js']], function (BaseSeries, H, O, U) {
  32. /* *
  33. *
  34. * (c) 2020 Torstein Honsi
  35. *
  36. * Item series type for Highcharts
  37. *
  38. * License: www.highcharts.com/license
  39. *
  40. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  41. *
  42. * */
  43. var defaultOptions = O.defaultOptions;
  44. var defined = U.defined,
  45. extend = U.extend,
  46. fireEvent = U.fireEvent,
  47. isNumber = U.isNumber,
  48. merge = U.merge,
  49. objectEach = U.objectEach,
  50. pick = U.pick;
  51. var piePoint = BaseSeries.seriesTypes.pie.prototype.pointClass.prototype;
  52. /**
  53. * The item series type.
  54. *
  55. * @requires module:modules/item-series
  56. *
  57. * @private
  58. * @class
  59. * @name Highcharts.seriesTypes.item
  60. *
  61. * @augments Highcharts.seriesTypes.pie
  62. */
  63. BaseSeries.seriesType('item',
  64. // Inherits pie as the most tested non-cartesian series with individual
  65. // point legend, tooltips etc. Only downside is we need to re-enable
  66. // marker options.
  67. 'pie',
  68. /**
  69. * An item chart is an infographic chart where a number of items are laid
  70. * out in either a rectangular or circular pattern. It can be used to
  71. * visualize counts within a group, or for the circular pattern, typically
  72. * a parliament.
  73. *
  74. * The circular layout has much in common with a pie chart. Many of the item
  75. * series options, like `center`, `size` and data label positioning, are
  76. * inherited from the pie series and don't apply for rectangular layouts.
  77. *
  78. * @sample highcharts/demo/parliament-chart
  79. * Parliament chart (circular item chart)
  80. * @sample highcharts/series-item/rectangular
  81. * Rectangular item chart
  82. * @sample highcharts/series-item/symbols
  83. * Infographic with symbols
  84. *
  85. * @extends plotOptions.pie
  86. * @since 7.1.0
  87. * @product highcharts
  88. * @excluding borderColor, borderWidth, depth, linecap, shadow,
  89. * slicedOffset
  90. * @requires modules/item-series
  91. * @optionparent plotOptions.item
  92. */
  93. {
  94. /**
  95. * In circular view, the end angle of the item layout, in degrees where
  96. * 0 is up.
  97. *
  98. * @sample highcharts/demo/parliament-chart
  99. * Parliament chart
  100. * @type {undefined|number}
  101. */
  102. endAngle: void 0,
  103. /**
  104. * In circular view, the size of the inner diameter of the circle. Can
  105. * be a percentage or pixel value. Percentages are relative to the outer
  106. * perimeter. Pixel values are given as integers.
  107. *
  108. * If the `rows` option is set, it overrides the `innerSize` setting.
  109. *
  110. * @sample highcharts/demo/parliament-chart
  111. * Parliament chart
  112. * @type {string|number}
  113. */
  114. innerSize: '40%',
  115. /**
  116. * The padding between the items, given in relative size where the size
  117. * of the item is 1.
  118. * @type {number}
  119. */
  120. itemPadding: 0.1,
  121. /**
  122. * The layout of the items in rectangular view. Can be either
  123. * `horizontal` or `vertical`.
  124. * @sample highcharts/series-item/symbols
  125. * Horizontal layout
  126. * @type {string}
  127. */
  128. layout: 'vertical',
  129. /**
  130. * @extends plotOptions.series.marker
  131. */
  132. marker: merge(defaultOptions.plotOptions.line.marker, {
  133. radius: null
  134. }),
  135. /**
  136. * The number of rows to display in the rectangular or circular view. If
  137. * the `innerSize` is set, it will be overridden by the `rows` setting.
  138. *
  139. * @sample highcharts/series-item/rows-columns
  140. * Fixed row count
  141. * @type {number}
  142. */
  143. rows: void 0,
  144. crisp: false,
  145. showInLegend: true,
  146. /**
  147. * In circular view, the start angle of the item layout, in degrees
  148. * where 0 is up.
  149. *
  150. * @sample highcharts/demo/parliament-chart
  151. * Parliament chart
  152. * @type {undefined|number}
  153. */
  154. startAngle: void 0
  155. },
  156. // Prototype members
  157. {
  158. markerAttribs: void 0,
  159. translate: function (positions) {
  160. // Initialize chart without setting data, #13379.
  161. if (this.total === 0) {
  162. this.center = this.getCenter();
  163. }
  164. if (!this.slots) {
  165. this.slots = [];
  166. }
  167. if (isNumber(this.options.startAngle) &&
  168. isNumber(this.options.endAngle)) {
  169. H.seriesTypes.pie.prototype.translate.apply(this, arguments);
  170. this.slots = this.getSlots();
  171. }
  172. else {
  173. this.generatePoints();
  174. fireEvent(this, 'afterTranslate');
  175. }
  176. },
  177. // Get the semi-circular slots
  178. getSlots: function () {
  179. var center = this.center,
  180. diameter = center[2],
  181. innerSize = center[3],
  182. row,
  183. slots = this.slots,
  184. x,
  185. y,
  186. rowRadius,
  187. rowLength,
  188. colCount,
  189. increment,
  190. angle,
  191. col,
  192. itemSize = 0,
  193. rowCount,
  194. fullAngle = (this.endAngleRad - this.startAngleRad),
  195. itemCount = Number.MAX_VALUE,
  196. finalItemCount,
  197. rows,
  198. testRows,
  199. rowsOption = this.options.rows,
  200. // How many rows (arcs) should be used
  201. rowFraction = (diameter - innerSize) / diameter,
  202. isCircle = fullAngle % (2 * Math.PI) === 0;
  203. // Increase the itemSize until we find the best fit
  204. while (itemCount > this.total + (rows && isCircle ? rows.length : 0)) {
  205. finalItemCount = itemCount;
  206. // Reset
  207. slots.length = 0;
  208. itemCount = 0;
  209. // Now rows is the last successful run
  210. rows = testRows;
  211. testRows = [];
  212. itemSize++;
  213. // Total number of rows (arcs) from the center to the
  214. // perimeter
  215. rowCount = diameter / itemSize / 2;
  216. if (rowsOption) {
  217. innerSize = ((rowCount - rowsOption) / rowCount) * diameter;
  218. if (innerSize >= 0) {
  219. rowCount = rowsOption;
  220. // If innerSize is negative, we are trying to set too
  221. // many rows in the rows option, so fall back to
  222. // treating it as innerSize 0
  223. }
  224. else {
  225. innerSize = 0;
  226. rowFraction = 1;
  227. }
  228. }
  229. else {
  230. rowCount = Math.floor(rowCount * rowFraction);
  231. }
  232. for (row = rowCount; row > 0; row--) {
  233. rowRadius = (innerSize + (row / rowCount) *
  234. (diameter - innerSize - itemSize)) / 2;
  235. rowLength = fullAngle * rowRadius;
  236. colCount = Math.ceil(rowLength / itemSize);
  237. testRows.push({
  238. rowRadius: rowRadius,
  239. rowLength: rowLength,
  240. colCount: colCount
  241. });
  242. itemCount += colCount + 1;
  243. }
  244. }
  245. if (!rows) {
  246. return;
  247. }
  248. // We now have more slots than we have total items. Loop over
  249. // the rows and remove the last slot until the count is correct.
  250. // For each iteration we sort the last slot by the angle, and
  251. // remove those with the highest angles.
  252. var overshoot = finalItemCount - this.total -
  253. (isCircle ? rows.length : 0);
  254. /**
  255. * @private
  256. * @param {Highcharts.ItemRowContainerObject} item
  257. * Wrapped object with angle and row
  258. * @return {void}
  259. */
  260. function cutOffRow(item) {
  261. if (overshoot > 0) {
  262. item.row.colCount--;
  263. overshoot--;
  264. }
  265. }
  266. while (overshoot > 0) {
  267. rows
  268. // Return a simplified representation of the angle of
  269. // the last slot within each row.
  270. .map(function (row) {
  271. return {
  272. angle: row.colCount / row.rowLength,
  273. row: row
  274. };
  275. })
  276. // Sort by the angles...
  277. .sort(function (a, b) {
  278. return b.angle - a.angle;
  279. })
  280. // ...so that we can ignore the items with the lowest
  281. // angles...
  282. .slice(0, Math.min(overshoot, Math.ceil(rows.length / 2)))
  283. // ...and remove the ones with the highest angles
  284. .forEach(cutOffRow);
  285. }
  286. rows.forEach(function (row) {
  287. var rowRadius = row.rowRadius,
  288. colCount = row.colCount;
  289. increment = colCount ? fullAngle / colCount : 0;
  290. for (col = 0; col <= colCount; col += 1) {
  291. angle = this.startAngleRad + col * increment;
  292. x = center[0] + Math.cos(angle) * rowRadius;
  293. y = center[1] + Math.sin(angle) * rowRadius;
  294. slots.push({ x: x, y: y, angle: angle });
  295. }
  296. }, this);
  297. // Sort by angle
  298. slots.sort(function (a, b) {
  299. return a.angle - b.angle;
  300. });
  301. this.itemSize = itemSize;
  302. return slots;
  303. },
  304. getRows: function () {
  305. var rows = this.options.rows,
  306. cols,
  307. ratio;
  308. // Get the row count that gives the most square cells
  309. if (!rows) {
  310. ratio = this.chart.plotWidth / this.chart.plotHeight;
  311. rows = Math.sqrt(this.total);
  312. if (ratio > 1) {
  313. rows = Math.ceil(rows);
  314. while (rows > 0) {
  315. cols = this.total / rows;
  316. if (cols / rows > ratio) {
  317. break;
  318. }
  319. rows--;
  320. }
  321. }
  322. else {
  323. rows = Math.floor(rows);
  324. while (rows < this.total) {
  325. cols = this.total / rows;
  326. if (cols / rows < ratio) {
  327. break;
  328. }
  329. rows++;
  330. }
  331. }
  332. }
  333. return rows;
  334. },
  335. drawPoints: function () {
  336. var series = this,
  337. options = this.options,
  338. renderer = series.chart.renderer,
  339. seriesMarkerOptions = options.marker,
  340. borderWidth = this.borderWidth,
  341. crisp = borderWidth % 2 ? 0.5 : 1,
  342. i = 0,
  343. rows = this.getRows(),
  344. cols = Math.ceil(this.total / rows),
  345. cellWidth = this.chart.plotWidth / cols,
  346. cellHeight = this.chart.plotHeight / rows,
  347. itemSize = this.itemSize || Math.min(cellWidth,
  348. cellHeight);
  349. /*
  350. this.slots.forEach(slot => {
  351. this.chart.renderer.circle(slot.x, slot.y, 6)
  352. .attr({
  353. fill: 'silver'
  354. })
  355. .add(this.group);
  356. });
  357. //*/
  358. this.points.forEach(function (point) {
  359. var attr,
  360. graphics,
  361. pointAttr,
  362. pointMarkerOptions = point.marker || {},
  363. symbol = (pointMarkerOptions.symbol ||
  364. seriesMarkerOptions.symbol),
  365. r = pick(pointMarkerOptions.radius,
  366. seriesMarkerOptions.radius),
  367. size = defined(r) ? 2 * r : itemSize,
  368. padding = size * options.itemPadding,
  369. x,
  370. y,
  371. width,
  372. height;
  373. point.graphics = graphics = point.graphics || {};
  374. if (!series.chart.styledMode) {
  375. pointAttr = series.pointAttribs(point, point.selected && 'select');
  376. }
  377. if (!point.isNull && point.visible) {
  378. if (!point.graphic) {
  379. point.graphic = renderer.g('point')
  380. .add(series.group);
  381. }
  382. for (var val = 0; val < point.y; val++) {
  383. // Semi-circle
  384. if (series.center && series.slots) {
  385. // Fill up the slots from left to right
  386. var slot = series.slots.shift();
  387. x = slot.x - itemSize / 2;
  388. y = slot.y - itemSize / 2;
  389. }
  390. else if (options.layout === 'horizontal') {
  391. x = cellWidth * (i % cols);
  392. y = cellHeight * Math.floor(i / cols);
  393. }
  394. else {
  395. x = cellWidth * Math.floor(i / rows);
  396. y = cellHeight * (i % rows);
  397. }
  398. x += padding;
  399. y += padding;
  400. width = Math.round(size - 2 * padding);
  401. height = width;
  402. if (series.options.crisp) {
  403. x = Math.round(x) - crisp;
  404. y = Math.round(y) + crisp;
  405. }
  406. attr = {
  407. x: x,
  408. y: y,
  409. width: width,
  410. height: height
  411. };
  412. if (typeof r !== 'undefined') {
  413. attr.r = r;
  414. }
  415. if (graphics[val]) {
  416. graphics[val].animate(attr);
  417. }
  418. else {
  419. graphics[val] = renderer
  420. .symbol(symbol, null, null, null, null, {
  421. backgroundSize: 'within'
  422. })
  423. .attr(extend(attr, pointAttr))
  424. .add(point.graphic);
  425. }
  426. graphics[val].isActive = true;
  427. i++;
  428. }
  429. }
  430. objectEach(graphics, function (graphic, key) {
  431. if (!graphic.isActive) {
  432. graphic.destroy();
  433. delete graphics[key];
  434. }
  435. else {
  436. graphic.isActive = false;
  437. }
  438. });
  439. });
  440. },
  441. drawDataLabels: function () {
  442. if (this.center && this.slots) {
  443. H.seriesTypes.pie.prototype.drawDataLabels.call(this);
  444. // else, it's just a dot chart with no natural place to put the
  445. // data labels
  446. }
  447. else {
  448. this.points.forEach(function (point) {
  449. point.destroyElements({ dataLabel: 1 });
  450. });
  451. }
  452. },
  453. // Fade in the whole chart
  454. animate: function (init) {
  455. if (init) {
  456. this.group.attr({
  457. opacity: 0
  458. });
  459. }
  460. else {
  461. this.group.animate({
  462. opacity: 1
  463. }, this.options.animation);
  464. }
  465. }
  466. },
  467. // Point class
  468. {
  469. connectorShapes: piePoint.connectorShapes,
  470. getConnectorPath: piePoint.getConnectorPath,
  471. setVisible: piePoint.setVisible,
  472. getTranslate: piePoint.getTranslate
  473. });
  474. /**
  475. * An `item` series. If the [type](#series.item.type) option is not specified,
  476. * it is inherited from [chart.type](#chart.type).
  477. *
  478. * @extends series,plotOptions.item
  479. * @excluding dataParser, dataURL, stack, xAxis, yAxis, dataSorting,
  480. * boostThreshold, boostBlending
  481. * @product highcharts
  482. * @requires modules/item-series
  483. * @apioption series.item
  484. */
  485. /**
  486. * An array of data points for the series. For the `item` series type,
  487. * points can be given in the following ways:
  488. *
  489. * 1. An array of numerical values. In this case, the numerical values will be
  490. * interpreted as `y` options. Example:
  491. * ```js
  492. * data: [0, 5, 3, 5]
  493. * ```
  494. *
  495. * 2. An array of objects with named values. The following snippet shows only a
  496. * few settings, see the complete options set below. If the total number of
  497. * data points exceeds the series'
  498. * [turboThreshold](#series.item.turboThreshold),
  499. * this option is not available.
  500. * ```js
  501. * data: [{
  502. * y: 1,
  503. * name: "Point2",
  504. * color: "#00FF00"
  505. * }, {
  506. * y: 7,
  507. * name: "Point1",
  508. * color: "#FF00FF"
  509. * }]
  510. * ```
  511. *
  512. * @sample {highcharts} highcharts/chart/reflow-true/
  513. * Numerical values
  514. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  515. * Arrays of numeric x and y
  516. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  517. * Arrays of datetime x and y
  518. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  519. * Arrays of point.name and y
  520. * @sample {highcharts} highcharts/series/data-array-of-objects/
  521. * Config objects
  522. *
  523. * @type {Array<number|Array<string,(number|null)>|null|*>}
  524. * @extends series.pie.data
  525. * @excludes sliced
  526. * @product highcharts
  527. * @apioption series.item.data
  528. */
  529. /**
  530. * The sequential index of the data point in the legend.
  531. *
  532. * @type {number}
  533. * @product highcharts
  534. * @apioption series.pie.data.legendIndex
  535. */
  536. ''; // adds the doclets above to the transpiled file
  537. });
  538. _registerModule(_modules, 'masters/modules/item-series.src.js', [], function () {
  539. });
  540. }));