variwide.src.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * @license Highcharts JS v8.0.0 (2019-12-10)
  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-2019 Torstein Honsi
  37. *
  38. * License: www.highcharts.com/license
  39. *
  40. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  41. *
  42. * */
  43. var isNumber = U.isNumber, pick = U.pick, wrap = U.wrap;
  44. var addEvent = H.addEvent, seriesType = H.seriesType, 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.stacks[(series.negStacks &&
  190. point.y < (options.startFromThreshold ?
  191. 0 :
  192. options.threshold) ?
  193. '-' :
  194. '') + series.stackKey];
  195. pointStack = stack[xValue];
  196. if (stack && pointStack && !point.isNull) {
  197. pointStack.setOffset(-(pointWidth / 2) || 0, pointWidth || 0, void 0, void 0, point.plotX);
  198. }
  199. });
  200. }
  201. // Point functions
  202. }, {
  203. isValid: function () {
  204. return isNumber(this.y) && isNumber(this.z);
  205. }
  206. });
  207. H.Tick.prototype.postTranslate = function (xy, xOrY, index) {
  208. var axis = this.axis, pos = xy[xOrY] - axis.pos;
  209. if (!axis.horiz) {
  210. pos = axis.len - pos;
  211. }
  212. pos = axis.series[0].postTranslate(index, pos);
  213. if (!axis.horiz) {
  214. pos = axis.len - pos;
  215. }
  216. xy[xOrY] = axis.pos + pos;
  217. };
  218. /* eslint-disable no-invalid-this */
  219. // Same width as the category (#8083)
  220. addEvent(H.Axis, 'afterDrawCrosshair', function (e) {
  221. if (this.variwide && this.cross) {
  222. this.cross.attr('stroke-width', (e.point && e.point.crosshairWidth));
  223. }
  224. });
  225. // On a vertical axis, apply anti-collision logic to the labels.
  226. addEvent(H.Axis, 'afterRender', function () {
  227. var axis = this;
  228. if (!this.horiz && this.variwide) {
  229. this.chart.labelCollectors.push(function () {
  230. return axis.tickPositions
  231. .filter(function (pos) {
  232. return axis.ticks[pos].label;
  233. })
  234. .map(function (pos, i) {
  235. var label = axis.ticks[pos].label;
  236. label.labelrank = axis.zData[i];
  237. return label;
  238. });
  239. });
  240. }
  241. });
  242. addEvent(H.Tick, 'afterGetPosition', function (e) {
  243. var axis = this.axis, xOrY = axis.horiz ? 'x' : 'y';
  244. if (axis.variwide) {
  245. this[xOrY + 'Orig'] = e.pos[xOrY];
  246. this.postTranslate(e.pos, xOrY, this.pos);
  247. }
  248. });
  249. wrap(H.Tick.prototype, 'getLabelPosition', function (proceed, x, y, label, horiz, labelOptions, tickmarkOffset, index) {
  250. var args = Array.prototype.slice.call(arguments, 1), xy, xOrY = horiz ? 'x' : 'y';
  251. // Replace the x with the original x
  252. if (this.axis.variwide &&
  253. typeof this[xOrY + 'Orig'] === 'number') {
  254. args[horiz ? 0 : 1] = this[xOrY + 'Orig'];
  255. }
  256. xy = proceed.apply(this, args);
  257. // Post-translate
  258. if (this.axis.variwide && this.axis.categories) {
  259. this.postTranslate(xy, xOrY, index);
  260. }
  261. return xy;
  262. });
  263. /**
  264. * A `variwide` series. If the [type](#series.variwide.type) option is not
  265. * specified, it is inherited from [chart.type](#chart.type).
  266. *
  267. * @extends series,plotOptions.variwide
  268. * @product highcharts
  269. * @requires modules/variwide
  270. * @apioption series.variwide
  271. */
  272. /**
  273. * An array of data points for the series. For the `variwide` series type,
  274. * points can be given in the following ways:
  275. *
  276. * 1. An array of arrays with 3 or 2 values. In this case, the values correspond
  277. * to `x,y,z`. If the first value is a string, it is applied as the name of
  278. * the point, and the `x` value is inferred. The `x` value can also be
  279. * omitted, in which case the inner arrays should be of length 2. Then the
  280. * `x` value is automatically calculated, either starting at 0 and
  281. * incremented by 1, or from `pointStart` and `pointInterval` given in the
  282. * series options.
  283. * ```js
  284. * data: [
  285. * [0, 1, 2],
  286. * [1, 5, 5],
  287. * [2, 0, 2]
  288. * ]
  289. * ```
  290. *
  291. * 2. An array of objects with named values. The following snippet shows only a
  292. * few settings, see the complete options set below. If the total number of
  293. * data points exceeds the series'
  294. * [turboThreshold](#series.variwide.turboThreshold), this option is not
  295. * available.
  296. * ```js
  297. * data: [{
  298. * x: 1,
  299. * y: 1,
  300. * z: 1,
  301. * name: "Point2",
  302. * color: "#00FF00"
  303. * }, {
  304. * x: 1,
  305. * y: 5,
  306. * z: 4,
  307. * name: "Point1",
  308. * color: "#FF00FF"
  309. * }]
  310. * ```
  311. *
  312. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  313. * Arrays of numeric x and y
  314. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  315. * Arrays of datetime x and y
  316. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  317. * Arrays of point.name and y
  318. * @sample {highcharts} highcharts/series/data-array-of-objects/
  319. * Config objects
  320. *
  321. * @type {Array<Array<(number|string),number>|Array<(number|string),number,number>|*>}
  322. * @extends series.line.data
  323. * @excluding marker
  324. * @product highcharts
  325. * @apioption series.variwide.data
  326. */
  327. /**
  328. * The relative width for each column. On a category axis, the widths are
  329. * distributed so they sum up to the X axis length. On linear and datetime axes,
  330. * the columns will be laid out from the X value and Z units along the axis.
  331. *
  332. * @type {number}
  333. * @product highcharts
  334. * @apioption series.variwide.data.z
  335. */
  336. ''; // adds doclets above to transpiled file
  337. });
  338. _registerModule(_modules, 'masters/modules/variwide.src.js', [], function () {
  339. });
  340. }));