variwide.src.js 14 KB

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