VariwideSeries.js 11 KB

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