variwide.src.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * @license Highcharts JS v8.2.2 (2020-10-22)
  3. *
  4. * Highcharts variwide module
  5. *
  6. * (c) 2010-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/variwide', ['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/VariwideSeries.js', [_modules['Core/Axis/Axis.js'], _modules['Core/Series/Series.js'], _modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (Axis, BaseSeries, H, U) {
  32. /* *
  33. *
  34. * Highcharts variwide module
  35. *
  36. * (c) 2010-2020 Torstein Honsi
  37. *
  38. * License: www.highcharts.com/license
  39. *
  40. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  41. *
  42. * */
  43. var seriesTypes = BaseSeries.seriesTypes;
  44. var addEvent = U.addEvent,
  45. isNumber = U.isNumber,
  46. pick = U.pick,
  47. wrap = U.wrap;
  48. /**
  49. * @private
  50. * @class
  51. * @name Highcharts.seriesTypes.variwide
  52. *
  53. * @augments Highcharts.Series
  54. */
  55. BaseSeries.seriesType('variwide', 'column'
  56. /**
  57. * A variwide chart (related to marimekko chart) is a column chart with a
  58. * variable width expressing a third dimension.
  59. *
  60. * @sample {highcharts} highcharts/demo/variwide/
  61. * Variwide chart
  62. * @sample {highcharts} highcharts/series-variwide/inverted/
  63. * Inverted variwide chart
  64. * @sample {highcharts} highcharts/series-variwide/datetime/
  65. * Variwide columns on a datetime axis
  66. *
  67. * @extends plotOptions.column
  68. * @since 6.0.0
  69. * @product highcharts
  70. * @excluding boostThreshold, crisp, depth, edgeColor, edgeWidth,
  71. * groupZPadding, boostBlending
  72. * @requires modules/variwide
  73. * @optionparent plotOptions.variwide
  74. */
  75. , {
  76. /**
  77. * In a variwide chart, the point padding is 0 in order to express the
  78. * horizontal stacking of items.
  79. */
  80. pointPadding: 0,
  81. /**
  82. * In a variwide chart, the group padding is 0 in order to express the
  83. * horizontal stacking of items.
  84. */
  85. groupPadding: 0
  86. }, {
  87. irregularWidths: true,
  88. pointArrayMap: ['y', 'z'],
  89. parallelArrays: ['x', 'y', 'z'],
  90. processData: function (force) {
  91. this.totalZ = 0;
  92. this.relZ = [];
  93. seriesTypes.column.prototype.processData.call(this, force);
  94. (this.xAxis.reversed ?
  95. this.zData.slice().reverse() :
  96. this.zData).forEach(function (z, i) {
  97. this.relZ[i] = this.totalZ;
  98. this.totalZ += z;
  99. }, this);
  100. if (this.xAxis.categories) {
  101. this.xAxis.variwide = true;
  102. this.xAxis.zData = this.zData; // Used for label rank
  103. }
  104. return;
  105. },
  106. /* eslint-disable valid-jsdoc */
  107. /**
  108. * Translate an x value inside a given category index into the distorted
  109. * axis translation.
  110. *
  111. * @private
  112. * @function Highcharts.Series#postTranslate
  113. *
  114. * @param {number} index
  115. * The category index
  116. *
  117. * @param {number} x
  118. * The X pixel position in undistorted axis pixels
  119. *
  120. * @param {Highcharts.Point} point
  121. * For crosshairWidth for every point
  122. *
  123. * @return {number}
  124. * Distorted X position
  125. */
  126. postTranslate: function (index, x, point) {
  127. var axis = this.xAxis,
  128. relZ = this.relZ,
  129. i = axis.reversed ? relZ.length - index : index,
  130. goRight = axis.reversed ? -1 : 1,
  131. len = axis.len,
  132. totalZ = this.totalZ,
  133. linearSlotLeft = i / relZ.length * len,
  134. linearSlotRight = (i + goRight) / relZ.length * len,
  135. slotLeft = (pick(relZ[i],
  136. totalZ) / totalZ) * len,
  137. slotRight = (pick(relZ[i + goRight],
  138. totalZ) / totalZ) * len,
  139. xInsideLinearSlot = x - linearSlotLeft,
  140. ret;
  141. // Set crosshairWidth for every point (#8173)
  142. if (point) {
  143. point.crosshairWidth = slotRight - slotLeft;
  144. }
  145. ret = slotLeft +
  146. xInsideLinearSlot * (slotRight - slotLeft) /
  147. (linearSlotRight - linearSlotLeft);
  148. return ret;
  149. },
  150. /* eslint-enable valid-jsdoc */
  151. // Extend translation by distoring X position based on Z.
  152. translate: function () {
  153. // Temporarily disable crisping when computing original shapeArgs
  154. var crispOption = this.options.crisp,
  155. xAxis = this.xAxis;
  156. this.options.crisp = false;
  157. seriesTypes.column.prototype.translate.call(this);
  158. // Reset option
  159. this.options.crisp = crispOption;
  160. var inverted = this.chart.inverted,
  161. crisp = this.borderWidth % 2 / 2;
  162. // Distort the points to reflect z dimension
  163. this.points.forEach(function (point, i) {
  164. var left,
  165. right;
  166. if (xAxis.variwide) {
  167. left = this.postTranslate(i, point.shapeArgs.x, point);
  168. right = this.postTranslate(i, point.shapeArgs.x +
  169. point.shapeArgs.width);
  170. // For linear or datetime axes, the variwide column should
  171. // start with X and extend Z units, without modifying the
  172. // axis.
  173. }
  174. else {
  175. left = point.plotX;
  176. right = xAxis.translate(point.x + point.z, 0, 0, 0, 1);
  177. }
  178. if (this.options.crisp) {
  179. left = Math.round(left) - crisp;
  180. right = Math.round(right) - crisp;
  181. }
  182. point.shapeArgs.x = left;
  183. point.shapeArgs.width = Math.max(right - left, 1);
  184. // Crosshair position (#8083)
  185. point.plotX = (left + right) / 2;
  186. // Adjust the tooltip position
  187. if (!inverted) {
  188. point.tooltipPos[0] =
  189. point.shapeArgs.x +
  190. point.shapeArgs.width / 2;
  191. }
  192. else {
  193. point.tooltipPos[1] =
  194. xAxis.len - point.shapeArgs.x -
  195. point.shapeArgs.width / 2;
  196. }
  197. }, this);
  198. if (this.options.stacking) {
  199. this.correctStackLabels();
  200. }
  201. },
  202. // Function that corrects stack labels positions
  203. correctStackLabels: function () {
  204. var series = this,
  205. options = series.options,
  206. yAxis = series.yAxis,
  207. pointStack,
  208. pointWidth,
  209. stack,
  210. xValue;
  211. series.points.forEach(function (point) {
  212. xValue = point.x;
  213. pointWidth = point.shapeArgs.width;
  214. stack = yAxis.stacking.stacks[(series.negStacks &&
  215. point.y < (options.startFromThreshold ?
  216. 0 :
  217. options.threshold) ?
  218. '-' :
  219. '') + series.stackKey];
  220. if (stack) {
  221. pointStack = stack[xValue];
  222. if (pointStack && !point.isNull) {
  223. pointStack.setOffset(-(pointWidth / 2) || 0, pointWidth || 0, void 0, void 0, point.plotX);
  224. }
  225. }
  226. });
  227. }
  228. // Point functions
  229. }, {
  230. isValid: function () {
  231. return isNumber(this.y) && isNumber(this.z);
  232. }
  233. });
  234. H.Tick.prototype.postTranslate = function (xy, xOrY, index) {
  235. var axis = this.axis,
  236. pos = xy[xOrY] - axis.pos;
  237. if (!axis.horiz) {
  238. pos = axis.len - pos;
  239. }
  240. pos = axis.series[0].postTranslate(index, pos);
  241. if (!axis.horiz) {
  242. pos = axis.len - pos;
  243. }
  244. xy[xOrY] = axis.pos + pos;
  245. };
  246. /* eslint-disable no-invalid-this */
  247. // Same width as the category (#8083)
  248. addEvent(Axis, 'afterDrawCrosshair', function (e) {
  249. if (this.variwide && this.cross) {
  250. this.cross.attr('stroke-width', (e.point && e.point.crosshairWidth));
  251. }
  252. });
  253. // On a vertical axis, apply anti-collision logic to the labels.
  254. addEvent(Axis, 'afterRender', function () {
  255. var axis = this;
  256. if (!this.horiz && this.variwide) {
  257. this.chart.labelCollectors.push(function () {
  258. return axis.tickPositions
  259. .filter(function (pos) {
  260. return axis.ticks[pos].label;
  261. })
  262. .map(function (pos, i) {
  263. var label = axis.ticks[pos].label;
  264. label.labelrank = axis.zData[i];
  265. return label;
  266. });
  267. });
  268. }
  269. });
  270. addEvent(H.Tick, 'afterGetPosition', function (e) {
  271. var axis = this.axis,
  272. xOrY = axis.horiz ? 'x' : 'y';
  273. if (axis.variwide) {
  274. this[xOrY + 'Orig'] = e.pos[xOrY];
  275. this.postTranslate(e.pos, xOrY, this.pos);
  276. }
  277. });
  278. wrap(H.Tick.prototype, 'getLabelPosition', function (proceed, x, y, label, horiz, labelOptions, tickmarkOffset, index) {
  279. var args = Array.prototype.slice.call(arguments, 1),
  280. xy,
  281. xOrY = horiz ? 'x' : 'y';
  282. // Replace the x with the original x
  283. if (this.axis.variwide &&
  284. typeof this[xOrY + 'Orig'] === 'number') {
  285. args[horiz ? 0 : 1] = this[xOrY + 'Orig'];
  286. }
  287. xy = proceed.apply(this, args);
  288. // Post-translate
  289. if (this.axis.variwide && this.axis.categories) {
  290. this.postTranslate(xy, xOrY, index);
  291. }
  292. return xy;
  293. });
  294. /**
  295. * A `variwide` series. If the [type](#series.variwide.type) option is not
  296. * specified, it is inherited from [chart.type](#chart.type).
  297. *
  298. * @extends series,plotOptions.variwide
  299. * @excluding boostThreshold, boostBlending
  300. * @product highcharts
  301. * @requires modules/variwide
  302. * @apioption series.variwide
  303. */
  304. /**
  305. * An array of data points for the series. For the `variwide` series type,
  306. * points can be given in the following ways:
  307. *
  308. * 1. An array of arrays with 3 or 2 values. In this case, the values correspond
  309. * to `x,y,z`. If the first value is a string, it is applied as the name of
  310. * the point, and the `x` value is inferred. The `x` value can also be
  311. * omitted, in which case the inner arrays should be of length 2. Then the
  312. * `x` value is automatically calculated, either starting at 0 and
  313. * incremented by 1, or from `pointStart` and `pointInterval` given in the
  314. * series options.
  315. * ```js
  316. * data: [
  317. * [0, 1, 2],
  318. * [1, 5, 5],
  319. * [2, 0, 2]
  320. * ]
  321. * ```
  322. *
  323. * 2. An array of objects with named values. The following snippet shows only a
  324. * few settings, see the complete options set below. If the total number of
  325. * data points exceeds the series'
  326. * [turboThreshold](#series.variwide.turboThreshold), this option is not
  327. * available.
  328. * ```js
  329. * data: [{
  330. * x: 1,
  331. * y: 1,
  332. * z: 1,
  333. * name: "Point2",
  334. * color: "#00FF00"
  335. * }, {
  336. * x: 1,
  337. * y: 5,
  338. * z: 4,
  339. * name: "Point1",
  340. * color: "#FF00FF"
  341. * }]
  342. * ```
  343. *
  344. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  345. * Arrays of numeric x and y
  346. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  347. * Arrays of datetime x and y
  348. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  349. * Arrays of point.name and y
  350. * @sample {highcharts} highcharts/series/data-array-of-objects/
  351. * Config objects
  352. *
  353. * @type {Array<Array<(number|string),number>|Array<(number|string),number,number>|*>}
  354. * @extends series.line.data
  355. * @excluding marker
  356. * @product highcharts
  357. * @apioption series.variwide.data
  358. */
  359. /**
  360. * The relative width for each column. On a category axis, the widths are
  361. * distributed so they sum up to the X axis length. On linear and datetime axes,
  362. * the columns will be laid out from the X value and Z units along the axis.
  363. *
  364. * @type {number}
  365. * @product highcharts
  366. * @apioption series.variwide.data.z
  367. */
  368. ''; // adds doclets above to transpiled file
  369. });
  370. _registerModule(_modules, 'masters/modules/variwide.src.js', [], function () {
  371. });
  372. }));