ScatterSeries.js 9.0 KB

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