VariwideSeries.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* *
  2. *
  3. * Highcharts variwide module
  4. *
  5. * (c) 2010-2021 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. var __extends = (this && this.__extends) || (function () {
  14. var extendStatics = function (d, b) {
  15. extendStatics = Object.setPrototypeOf ||
  16. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  17. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  18. return extendStatics(d, b);
  19. };
  20. return function (d, b) {
  21. extendStatics(d, b);
  22. function __() { this.constructor = d; }
  23. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  24. };
  25. })();
  26. import SeriesRegistry from '../../Core/Series/SeriesRegistry.js';
  27. var ColumnSeries = SeriesRegistry.seriesTypes.column;
  28. import VariwidePoint from './VariwidePoint.js';
  29. import U from '../../Core/Utilities.js';
  30. var extend = U.extend, merge = U.merge, pick = U.pick;
  31. import './VariwideComposition.js';
  32. /* *
  33. *
  34. * Class
  35. *
  36. * */
  37. /**
  38. * @private
  39. * @class
  40. * @name Highcharts.seriesTypes.variwide
  41. *
  42. * @augments Highcharts.Series
  43. */
  44. var VariwideSeries = /** @class */ (function (_super) {
  45. __extends(VariwideSeries, _super);
  46. function VariwideSeries() {
  47. var _this = _super !== null && _super.apply(this, arguments) || this;
  48. /* *
  49. *
  50. * Properties
  51. *
  52. * */
  53. _this.data = void 0;
  54. _this.options = void 0;
  55. _this.points = void 0;
  56. _this.relZ = void 0;
  57. _this.totalZ = void 0;
  58. _this.zData = void 0;
  59. return _this;
  60. }
  61. /* *
  62. *
  63. * Functions
  64. *
  65. * */
  66. VariwideSeries.prototype.processData = function (force) {
  67. this.totalZ = 0;
  68. this.relZ = [];
  69. SeriesRegistry.seriesTypes.column.prototype.processData.call(this, force);
  70. (this.xAxis.reversed ?
  71. this.zData.slice().reverse() :
  72. this.zData).forEach(function (z, i) {
  73. this.relZ[i] = this.totalZ;
  74. this.totalZ += z;
  75. }, this);
  76. if (this.xAxis.categories) {
  77. this.xAxis.variwide = true;
  78. this.xAxis.zData = this.zData; // Used for label rank
  79. }
  80. return;
  81. };
  82. /* eslint-disable valid-jsdoc */
  83. /**
  84. * Translate an x value inside a given category index into the distorted
  85. * axis translation.
  86. *
  87. * @private
  88. * @function Highcharts.Series#postTranslate
  89. *
  90. * @param {number} index
  91. * The category index
  92. *
  93. * @param {number} x
  94. * The X pixel position in undistorted axis pixels
  95. *
  96. * @param {Highcharts.Point} point
  97. * For crosshairWidth for every point
  98. *
  99. * @return {number}
  100. * Distorted X position
  101. */
  102. VariwideSeries.prototype.postTranslate = function (index, x, point) {
  103. var axis = this.xAxis, relZ = this.relZ, i = axis.reversed ? relZ.length - index : index, goRight = axis.reversed ? -1 : 1, minPx = axis.toPixels(axis.reversed ? (axis.dataMax || 0) + axis.pointRange : (axis.dataMin || 0)), maxPx = axis.toPixels(axis.reversed ? (axis.dataMin || 0) : (axis.dataMax || 0) + axis.pointRange), len = Math.abs(maxPx - minPx), totalZ = this.totalZ, left = this.chart.inverted ?
  104. maxPx - (this.chart.plotTop - goRight * axis.minPixelPadding) :
  105. minPx - this.chart.plotLeft - goRight * axis.minPixelPadding, 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 - (left + linearSlotLeft));
  106. // Set crosshairWidth for every point (#8173)
  107. if (point) {
  108. point.crosshairWidth = slotRight - slotLeft;
  109. }
  110. return left + slotLeft +
  111. xInsideLinearSlot * (slotRight - slotLeft) /
  112. (linearSlotRight - linearSlotLeft);
  113. };
  114. /* eslint-enable valid-jsdoc */
  115. // Extend translation by distoring X position based on Z.
  116. VariwideSeries.prototype.translate = function () {
  117. // Temporarily disable crisping when computing original shapeArgs
  118. var crispOption = this.options.crisp, xAxis = this.xAxis;
  119. this.options.crisp = false;
  120. SeriesRegistry.seriesTypes.column.prototype.translate.call(this);
  121. // Reset option
  122. this.options.crisp = crispOption;
  123. var inverted = this.chart.inverted, crisp = this.borderWidth % 2 / 2;
  124. // Distort the points to reflect z dimension
  125. this.points.forEach(function (point, i) {
  126. var left, right;
  127. if (xAxis.variwide) {
  128. left = this.postTranslate(i, point.shapeArgs.x, point);
  129. right = this.postTranslate(i, point.shapeArgs.x +
  130. point.shapeArgs.width);
  131. // For linear or datetime axes, the variwide column should
  132. // start with X and extend Z units, without modifying the
  133. // axis.
  134. }
  135. else {
  136. left = point.plotX;
  137. right = xAxis.translate(point.x + point.z, 0, 0, 0, 1);
  138. }
  139. if (this.options.crisp) {
  140. left = Math.round(left) - crisp;
  141. right = Math.round(right) - crisp;
  142. }
  143. point.shapeArgs.x = left;
  144. point.shapeArgs.width = Math.max(right - left, 1);
  145. // Crosshair position (#8083)
  146. point.plotX = (left + right) / 2;
  147. // Adjust the tooltip position
  148. if (!inverted) {
  149. point.tooltipPos[0] =
  150. point.shapeArgs.x +
  151. point.shapeArgs.width / 2;
  152. }
  153. else {
  154. point.tooltipPos[1] =
  155. xAxis.len - point.shapeArgs.x -
  156. point.shapeArgs.width / 2;
  157. }
  158. }, this);
  159. if (this.options.stacking) {
  160. this.correctStackLabels();
  161. }
  162. };
  163. // Function that corrects stack labels positions
  164. VariwideSeries.prototype.correctStackLabels = function () {
  165. var series = this, options = series.options, yAxis = series.yAxis, pointStack, pointWidth, stack, xValue;
  166. series.points.forEach(function (point) {
  167. xValue = point.x;
  168. pointWidth = point.shapeArgs.width;
  169. stack = yAxis.stacking.stacks[(series.negStacks &&
  170. point.y < (options.startFromThreshold ?
  171. 0 :
  172. options.threshold) ?
  173. '-' :
  174. '') + series.stackKey];
  175. if (stack) {
  176. pointStack = stack[xValue];
  177. if (pointStack && !point.isNull) {
  178. pointStack.setOffset(-(pointWidth / 2) || 0, pointWidth || 0, void 0, void 0, point.plotX);
  179. }
  180. }
  181. });
  182. };
  183. /* *
  184. *
  185. * Static properties
  186. *
  187. * */
  188. /**
  189. * A variwide chart (related to marimekko chart) is a column chart with a
  190. * variable width expressing a third dimension.
  191. *
  192. * @sample {highcharts} highcharts/demo/variwide/
  193. * Variwide chart
  194. * @sample {highcharts} highcharts/series-variwide/inverted/
  195. * Inverted variwide chart
  196. * @sample {highcharts} highcharts/series-variwide/datetime/
  197. * Variwide columns on a datetime axis
  198. *
  199. * @extends plotOptions.column
  200. * @since 6.0.0
  201. * @product highcharts
  202. * @excluding boostThreshold, crisp, depth, edgeColor, edgeWidth,
  203. * groupZPadding, boostBlending
  204. * @requires modules/variwide
  205. * @optionparent plotOptions.variwide
  206. */
  207. VariwideSeries.defaultOptions = merge(ColumnSeries.defaultOptions, {
  208. /**
  209. * In a variwide chart, the point padding is 0 in order to express the
  210. * horizontal stacking of items.
  211. */
  212. pointPadding: 0,
  213. /**
  214. * In a variwide chart, the group padding is 0 in order to express the
  215. * horizontal stacking of items.
  216. */
  217. groupPadding: 0
  218. });
  219. return VariwideSeries;
  220. }(ColumnSeries));
  221. extend(VariwideSeries.prototype, {
  222. irregularWidths: true,
  223. pointArrayMap: ['y', 'z'],
  224. parallelArrays: ['x', 'y', 'z'],
  225. pointClass: VariwidePoint
  226. });
  227. SeriesRegistry.registerSeriesType('variwide', VariwideSeries);
  228. /* *
  229. *
  230. * Default export
  231. *
  232. * */
  233. export default VariwideSeries;
  234. /* *
  235. *
  236. * API Options
  237. *
  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