ScatterSeries.js 9.3 KB

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