VariwideSeries.js 11 KB

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