ScatterSeries.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* *
  2. *
  3. * (c) 2010-2021 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. var __extends = (this && this.__extends) || (function () {
  12. var extendStatics = function (d, b) {
  13. extendStatics = Object.setPrototypeOf ||
  14. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  15. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  16. return extendStatics(d, b);
  17. };
  18. return function (d, b) {
  19. extendStatics(d, b);
  20. function __() { this.constructor = d; }
  21. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  22. };
  23. })();
  24. import ColumnSeries from '../Column/ColumnSeries.js';
  25. import LineSeries from '../Line/LineSeries.js';
  26. import SeriesRegistry from '../../Core/Series/SeriesRegistry.js';
  27. import U from '../../Core/Utilities.js';
  28. var addEvent = U.addEvent, extend = U.extend, merge = U.merge;
  29. /* *
  30. *
  31. * Class
  32. *
  33. * */
  34. /**
  35. * Scatter series type.
  36. *
  37. * @private
  38. */
  39. var ScatterSeries = /** @class */ (function (_super) {
  40. __extends(ScatterSeries, _super);
  41. function ScatterSeries() {
  42. var _this = _super !== null && _super.apply(this, arguments) || this;
  43. /* *
  44. *
  45. * Properties
  46. *
  47. * */
  48. _this.data = void 0;
  49. _this.options = void 0;
  50. _this.points = void 0;
  51. return _this;
  52. /* eslint-enable valid-jsdoc */
  53. }
  54. /* *
  55. *
  56. * Functions
  57. *
  58. * */
  59. /* eslint-disable valid-jsdoc */
  60. /**
  61. * Optionally add the jitter effect.
  62. * @private
  63. */
  64. ScatterSeries.prototype.applyJitter = function () {
  65. var series = this, jitter = this.options.jitter, len = this.points.length;
  66. /**
  67. * Return a repeatable, pseudo-random number based on an integer
  68. * seed.
  69. * @private
  70. */
  71. function unrandom(seed) {
  72. var rand = Math.sin(seed) * 10000;
  73. return rand - Math.floor(rand);
  74. }
  75. if (jitter) {
  76. this.points.forEach(function (point, i) {
  77. ['x', 'y'].forEach(function (dim, j) {
  78. var axis, plotProp = 'plot' + dim.toUpperCase(), min, max, translatedJitter;
  79. if (jitter[dim] && !point.isNull) {
  80. axis = series[dim + 'Axis'];
  81. translatedJitter =
  82. jitter[dim] * axis.transA;
  83. if (axis && !axis.isLog) {
  84. // Identify the outer bounds of the jitter range
  85. min = Math.max(0, point[plotProp] - translatedJitter);
  86. max = Math.min(axis.len, point[plotProp] + translatedJitter);
  87. // Find a random position within this range
  88. point[plotProp] = min +
  89. (max - min) * unrandom(i + j * len);
  90. // Update clientX for the tooltip k-d-tree
  91. if (dim === 'x') {
  92. point.clientX = point.plotX;
  93. }
  94. }
  95. }
  96. });
  97. });
  98. }
  99. };
  100. /**
  101. * @private
  102. * @function Highcharts.seriesTypes.scatter#drawGraph
  103. */
  104. ScatterSeries.prototype.drawGraph = function () {
  105. if (this.options.lineWidth ||
  106. // In case we have a graph from before and we update the line
  107. // width to 0 (#13816)
  108. (this.options.lineWidth === 0 &&
  109. this.graph &&
  110. this.graph.strokeWidth())) {
  111. _super.prototype.drawGraph.call(this);
  112. }
  113. };
  114. /**
  115. * A scatter plot uses cartesian coordinates to display values for two
  116. * variables for a set of data.
  117. *
  118. * @sample {highcharts} highcharts/demo/scatter/
  119. * Scatter plot
  120. *
  121. * @extends plotOptions.line
  122. * @excluding cropThreshold, pointPlacement, shadow, useOhlcData
  123. * @product highcharts highstock
  124. * @optionparent plotOptions.scatter
  125. */
  126. ScatterSeries.defaultOptions = merge(LineSeries.defaultOptions, {
  127. /**
  128. * The width of the line connecting the data points.
  129. *
  130. * @sample {highcharts} highcharts/plotoptions/scatter-linewidth-none/
  131. * 0 by default
  132. * @sample {highcharts} highcharts/plotoptions/scatter-linewidth-1/
  133. * 1px
  134. *
  135. * @product highcharts highstock
  136. */
  137. lineWidth: 0,
  138. findNearestPointBy: 'xy',
  139. /**
  140. * Apply a jitter effect for the rendered markers. When plotting
  141. * discrete values, a little random noise may help telling the points
  142. * apart. The jitter setting applies a random displacement of up to `n`
  143. * axis units in either direction. So for example on a horizontal X
  144. * axis, setting the `jitter.x` to 0.24 will render the point in a
  145. * random position between 0.24 units to the left and 0.24 units to the
  146. * right of the true axis position. On a category axis, setting it to
  147. * 0.5 will fill up the bin and make the data appear continuous.
  148. *
  149. * When rendered on top of a box plot or a column series, a jitter value
  150. * of 0.24 will correspond to the underlying series' default
  151. * [groupPadding](
  152. * https://api.highcharts.com/highcharts/plotOptions.column.groupPadding)
  153. * and [pointPadding](
  154. * https://api.highcharts.com/highcharts/plotOptions.column.pointPadding)
  155. * settings.
  156. *
  157. * @sample {highcharts} highcharts/series-scatter/jitter
  158. * Jitter on a scatter plot
  159. *
  160. * @sample {highcharts} highcharts/series-scatter/jitter-boxplot
  161. * Jittered scatter plot on top of a box plot
  162. *
  163. * @product highcharts highstock
  164. * @since 7.0.2
  165. */
  166. jitter: {
  167. /**
  168. * The maximal X offset for the random jitter effect.
  169. */
  170. x: 0,
  171. /**
  172. * The maximal Y offset for the random jitter effect.
  173. */
  174. y: 0
  175. },
  176. marker: {
  177. enabled: true // Overrides auto-enabling in line series (#3647)
  178. },
  179. /**
  180. * Sticky tracking of mouse events. When true, the `mouseOut` event
  181. * on a series isn't triggered until the mouse moves over another
  182. * series, or out of the plot area. When false, the `mouseOut` event on
  183. * a series is triggered when the mouse leaves the area around the
  184. * series' graph or markers. This also implies the tooltip. When
  185. * `stickyTracking` is false and `tooltip.shared` is false, the tooltip
  186. * will be hidden when moving the mouse between series.
  187. *
  188. * @type {boolean}
  189. * @default false
  190. * @product highcharts highstock
  191. * @apioption plotOptions.scatter.stickyTracking
  192. */
  193. /**
  194. * A configuration object for the tooltip rendering of each single
  195. * series. Properties are inherited from [tooltip](#tooltip).
  196. * Overridable properties are `headerFormat`, `pointFormat`,
  197. * `yDecimals`, `xDateFormat`, `yPrefix` and `ySuffix`. Unlike other
  198. * series, in a scatter plot the series.name by default shows in the
  199. * headerFormat and point.x and point.y in the pointFormat.
  200. *
  201. * @product highcharts highstock
  202. */
  203. tooltip: {
  204. headerFormat: '<span style="color:{point.color}">\u25CF</span> ' +
  205. '<span style="font-size: 10px"> {series.name}</span><br/>',
  206. pointFormat: 'x: <b>{point.x}</b><br/>y: <b>{point.y}</b><br/>'
  207. }
  208. });
  209. return ScatterSeries;
  210. }(LineSeries));
  211. extend(ScatterSeries.prototype, {
  212. drawTracker: ColumnSeries.prototype.drawTracker,
  213. sorted: false,
  214. requireSorting: false,
  215. noSharedTooltip: true,
  216. trackerGroups: ['group', 'markerGroup', 'dataLabelsGroup'],
  217. takeOrdinalPosition: false // #2342
  218. });
  219. /* *
  220. *
  221. * Events
  222. *
  223. * */
  224. /* eslint-disable no-invalid-this */
  225. addEvent(ScatterSeries, 'afterTranslate', function () {
  226. this.applyJitter();
  227. });
  228. SeriesRegistry.registerSeriesType('scatter', ScatterSeries);
  229. /* *
  230. *
  231. * Default Export
  232. *
  233. * */
  234. export default ScatterSeries;
  235. /* *
  236. *
  237. * API Options
  238. *
  239. * */
  240. /**
  241. * A `scatter` series. If the [type](#series.scatter.type) option is
  242. * not specified, it is inherited from [chart.type](#chart.type).
  243. *
  244. * @extends series,plotOptions.scatter
  245. * @excluding cropThreshold, dataParser, dataURL, useOhlcData
  246. * @product highcharts highstock
  247. * @apioption series.scatter
  248. */
  249. /**
  250. * An array of data points for the series. For the `scatter` series
  251. * type, points can be given in the following ways:
  252. *
  253. * 1. An array of numerical values. In this case, the numerical values will be
  254. * interpreted as `y` options. The `x` values will be automatically
  255. * calculated, either starting at 0 and incremented by 1, or from
  256. * `pointStart` and `pointInterval` given in the series options. If the axis
  257. * has categories, these will be used. Example:
  258. * ```js
  259. * data: [0, 5, 3, 5]
  260. * ```
  261. *
  262. * 2. An array of arrays with 2 values. In this case, the values correspond to
  263. * `x,y`. If the first value is a string, it is applied as the name of the
  264. * point, and the `x` value is inferred.
  265. * ```js
  266. * data: [
  267. * [0, 0],
  268. * [1, 8],
  269. * [2, 9]
  270. * ]
  271. * ```
  272. *
  273. * 3. An array of objects with named values. The following snippet shows only a
  274. * few settings, see the complete options set below. If the total number of
  275. * data points exceeds the series'
  276. * [turboThreshold](#series.scatter.turboThreshold), this option is not
  277. * available.
  278. * ```js
  279. * data: [{
  280. * x: 1,
  281. * y: 2,
  282. * name: "Point2",
  283. * color: "#00FF00"
  284. * }, {
  285. * x: 1,
  286. * y: 4,
  287. * name: "Point1",
  288. * color: "#FF00FF"
  289. * }]
  290. * ```
  291. *
  292. * @sample {highcharts} highcharts/chart/reflow-true/
  293. * Numerical values
  294. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  295. * Arrays of numeric x and y
  296. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  297. * Arrays of datetime x and y
  298. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  299. * Arrays of point.name and y
  300. * @sample {highcharts} highcharts/series/data-array-of-objects/
  301. * Config objects
  302. *
  303. * @type {Array<number|Array<(number|string),(number|null)>|null|*>}
  304. * @extends series.line.data
  305. * @product highcharts highstock
  306. * @apioption series.scatter.data
  307. */
  308. ''; // adds doclets above to transpilat