variwide.src.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* *
  2. *
  3. * Highcharts variwide module
  4. *
  5. * (c) 2010-2019 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 '../parts/Globals.js';
  14. import U from '../parts/Utilities.js';
  15. var isNumber = U.isNumber, pick = U.pick, wrap = U.wrap;
  16. import '../parts/AreaSeries.js';
  17. var addEvent = H.addEvent, seriesType = H.seriesType, 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
  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.stacks[(series.negStacks &&
  163. point.y < (options.startFromThreshold ?
  164. 0 :
  165. options.threshold) ?
  166. '-' :
  167. '') + series.stackKey];
  168. pointStack = stack[xValue];
  169. if (stack && pointStack && !point.isNull) {
  170. pointStack.setOffset(-(pointWidth / 2) || 0, pointWidth || 0, void 0, void 0, point.plotX);
  171. }
  172. });
  173. }
  174. // Point functions
  175. }, {
  176. isValid: function () {
  177. return isNumber(this.y) && isNumber(this.z);
  178. }
  179. });
  180. H.Tick.prototype.postTranslate = function (xy, xOrY, index) {
  181. var axis = this.axis, pos = xy[xOrY] - axis.pos;
  182. if (!axis.horiz) {
  183. pos = axis.len - pos;
  184. }
  185. pos = axis.series[0].postTranslate(index, pos);
  186. if (!axis.horiz) {
  187. pos = axis.len - pos;
  188. }
  189. xy[xOrY] = axis.pos + pos;
  190. };
  191. /* eslint-disable no-invalid-this */
  192. // Same width as the category (#8083)
  193. addEvent(H.Axis, 'afterDrawCrosshair', function (e) {
  194. if (this.variwide && this.cross) {
  195. this.cross.attr('stroke-width', (e.point && e.point.crosshairWidth));
  196. }
  197. });
  198. // On a vertical axis, apply anti-collision logic to the labels.
  199. addEvent(H.Axis, 'afterRender', function () {
  200. var axis = this;
  201. if (!this.horiz && this.variwide) {
  202. this.chart.labelCollectors.push(function () {
  203. return axis.tickPositions
  204. .filter(function (pos) {
  205. return axis.ticks[pos].label;
  206. })
  207. .map(function (pos, i) {
  208. var label = axis.ticks[pos].label;
  209. label.labelrank = axis.zData[i];
  210. return label;
  211. });
  212. });
  213. }
  214. });
  215. addEvent(H.Tick, 'afterGetPosition', function (e) {
  216. var axis = this.axis, xOrY = axis.horiz ? 'x' : 'y';
  217. if (axis.variwide) {
  218. this[xOrY + 'Orig'] = e.pos[xOrY];
  219. this.postTranslate(e.pos, xOrY, this.pos);
  220. }
  221. });
  222. wrap(H.Tick.prototype, 'getLabelPosition', function (proceed, x, y, label, horiz, labelOptions, tickmarkOffset, index) {
  223. var args = Array.prototype.slice.call(arguments, 1), xy, xOrY = horiz ? 'x' : 'y';
  224. // Replace the x with the original x
  225. if (this.axis.variwide &&
  226. typeof this[xOrY + 'Orig'] === 'number') {
  227. args[horiz ? 0 : 1] = this[xOrY + 'Orig'];
  228. }
  229. xy = proceed.apply(this, args);
  230. // Post-translate
  231. if (this.axis.variwide && this.axis.categories) {
  232. this.postTranslate(xy, xOrY, index);
  233. }
  234. return xy;
  235. });
  236. /**
  237. * A `variwide` series. If the [type](#series.variwide.type) option is not
  238. * specified, it is inherited from [chart.type](#chart.type).
  239. *
  240. * @extends series,plotOptions.variwide
  241. * @product highcharts
  242. * @requires modules/variwide
  243. * @apioption series.variwide
  244. */
  245. /**
  246. * An array of data points for the series. For the `variwide` series type,
  247. * points can be given in the following ways:
  248. *
  249. * 1. An array of arrays with 3 or 2 values. In this case, the values correspond
  250. * to `x,y,z`. If the first value is a string, it is applied as the name of
  251. * the point, and the `x` value is inferred. The `x` value can also be
  252. * omitted, in which case the inner arrays should be of length 2. Then the
  253. * `x` value is automatically calculated, either starting at 0 and
  254. * incremented by 1, or from `pointStart` and `pointInterval` given in the
  255. * series options.
  256. * ```js
  257. * data: [
  258. * [0, 1, 2],
  259. * [1, 5, 5],
  260. * [2, 0, 2]
  261. * ]
  262. * ```
  263. *
  264. * 2. An array of objects with named values. The following snippet shows only a
  265. * few settings, see the complete options set below. If the total number of
  266. * data points exceeds the series'
  267. * [turboThreshold](#series.variwide.turboThreshold), this option is not
  268. * available.
  269. * ```js
  270. * data: [{
  271. * x: 1,
  272. * y: 1,
  273. * z: 1,
  274. * name: "Point2",
  275. * color: "#00FF00"
  276. * }, {
  277. * x: 1,
  278. * y: 5,
  279. * z: 4,
  280. * name: "Point1",
  281. * color: "#FF00FF"
  282. * }]
  283. * ```
  284. *
  285. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  286. * Arrays of numeric x and y
  287. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  288. * Arrays of datetime x and y
  289. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  290. * Arrays of point.name and y
  291. * @sample {highcharts} highcharts/series/data-array-of-objects/
  292. * Config objects
  293. *
  294. * @type {Array<Array<(number|string),number>|Array<(number|string),number,number>|*>}
  295. * @extends series.line.data
  296. * @excluding marker
  297. * @product highcharts
  298. * @apioption series.variwide.data
  299. */
  300. /**
  301. * The relative width for each column. On a category axis, the widths are
  302. * distributed so they sum up to the X axis length. On linear and datetime axes,
  303. * the columns will be laid out from the X value and Z units along the axis.
  304. *
  305. * @type {number}
  306. * @product highcharts
  307. * @apioption series.variwide.data.z
  308. */
  309. ''; // adds doclets above to transpiled file