SplineSeries.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. 'use strict';
  11. import BaseSeries from '../Core/Series/Series.js';
  12. import U from '../Core/Utilities.js';
  13. var pick = U.pick;
  14. import '../Core/Options.js';
  15. import '../Series/LineSeries.js';
  16. import '../Series/LineSeries.js';
  17. /**
  18. * Spline series type.
  19. *
  20. * @private
  21. * @class
  22. * @name Highcharts.seriesTypes.spline
  23. *
  24. * @augments Highcarts.Series
  25. */
  26. BaseSeries.seriesType('spline', 'line',
  27. /**
  28. * A spline series is a special type of line series, where the segments
  29. * between the data points are smoothed.
  30. *
  31. * @sample {highcharts} highcharts/demo/spline-irregular-time/
  32. * Spline chart
  33. * @sample {highstock} stock/demo/spline/
  34. * Spline chart
  35. *
  36. * @extends plotOptions.series
  37. * @excluding step, boostThreshold, boostBlending
  38. * @product highcharts highstock
  39. * @optionparent plotOptions.spline
  40. */
  41. {},
  42. /**
  43. * @lends seriesTypes.spline.prototype
  44. */
  45. {
  46. /* eslint-disable valid-jsdoc */
  47. /**
  48. * Get the spline segment from a given point's previous neighbour to the
  49. * given point.
  50. *
  51. * @private
  52. * @function Highcharts.seriesTypes.spline#getPointSpline
  53. *
  54. * @param {Array<Highcharts.Point>}
  55. *
  56. * @param {Highcharts.Point} point
  57. *
  58. * @param {number} i
  59. *
  60. * @return {Highcharts.SVGPathArray}
  61. */
  62. getPointSpline: function (points, point, i) {
  63. var
  64. // 1 means control points midway between points, 2 means 1/3
  65. // from the point, 3 is 1/4 etc
  66. smoothing = 1.5, denom = smoothing + 1, plotX = point.plotX || 0, plotY = point.plotY || 0, lastPoint = points[i - 1], nextPoint = points[i + 1], leftContX, leftContY, rightContX, rightContY, ret;
  67. /**
  68. * @private
  69. */
  70. function doCurve(otherPoint) {
  71. return otherPoint &&
  72. !otherPoint.isNull &&
  73. otherPoint.doCurve !== false &&
  74. // #6387, area splines next to null:
  75. !point.isCliff;
  76. }
  77. // Find control points
  78. if (doCurve(lastPoint) && doCurve(nextPoint)) {
  79. var lastX = lastPoint.plotX || 0, lastY = lastPoint.plotY || 0, nextX = nextPoint.plotX || 0, nextY = nextPoint.plotY || 0, correction = 0;
  80. leftContX = (smoothing * plotX + lastX) / denom;
  81. leftContY = (smoothing * plotY + lastY) / denom;
  82. rightContX = (smoothing * plotX + nextX) / denom;
  83. rightContY = (smoothing * plotY + nextY) / denom;
  84. // Have the two control points make a straight line through main
  85. // point
  86. if (rightContX !== leftContX) { // #5016, division by zero
  87. correction = (((rightContY - leftContY) *
  88. (rightContX - plotX)) /
  89. (rightContX - leftContX) + plotY - rightContY);
  90. }
  91. leftContY += correction;
  92. rightContY += correction;
  93. // to prevent false extremes, check that control points are
  94. // between neighbouring points' y values
  95. if (leftContY > lastY && leftContY > plotY) {
  96. leftContY = Math.max(lastY, plotY);
  97. // mirror of left control point
  98. rightContY = 2 * plotY - leftContY;
  99. }
  100. else if (leftContY < lastY && leftContY < plotY) {
  101. leftContY = Math.min(lastY, plotY);
  102. rightContY = 2 * plotY - leftContY;
  103. }
  104. if (rightContY > nextY && rightContY > plotY) {
  105. rightContY = Math.max(nextY, plotY);
  106. leftContY = 2 * plotY - rightContY;
  107. }
  108. else if (rightContY < nextY && rightContY < plotY) {
  109. rightContY = Math.min(nextY, plotY);
  110. leftContY = 2 * plotY - rightContY;
  111. }
  112. // record for drawing in next point
  113. point.rightContX = rightContX;
  114. point.rightContY = rightContY;
  115. }
  116. // Visualize control points for debugging
  117. /*
  118. if (leftContX) {
  119. this.chart.renderer.circle(
  120. leftContX + this.chart.plotLeft,
  121. leftContY + this.chart.plotTop,
  122. 2
  123. )
  124. .attr({
  125. stroke: 'red',
  126. 'stroke-width': 2,
  127. fill: 'none',
  128. zIndex: 9
  129. })
  130. .add();
  131. this.chart.renderer.path(['M', leftContX + this.chart.plotLeft,
  132. leftContY + this.chart.plotTop,
  133. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  134. .attr({
  135. stroke: 'red',
  136. 'stroke-width': 2,
  137. zIndex: 9
  138. })
  139. .add();
  140. }
  141. if (rightContX) {
  142. this.chart.renderer.circle(
  143. rightContX + this.chart.plotLeft,
  144. rightContY + this.chart.plotTop,
  145. 2
  146. )
  147. .attr({
  148. stroke: 'green',
  149. 'stroke-width': 2,
  150. fill: 'none',
  151. zIndex: 9
  152. })
  153. .add();
  154. this.chart.renderer.path(['M', rightContX + this.chart.plotLeft,
  155. rightContY + this.chart.plotTop,
  156. 'L', plotX + this.chart.plotLeft, plotY + this.chart.plotTop])
  157. .attr({
  158. stroke: 'green',
  159. 'stroke-width': 2,
  160. zIndex: 9
  161. })
  162. .add();
  163. }
  164. // */
  165. ret = [
  166. 'C',
  167. pick(lastPoint.rightContX, lastPoint.plotX, 0),
  168. pick(lastPoint.rightContY, lastPoint.plotY, 0),
  169. pick(leftContX, plotX, 0),
  170. pick(leftContY, plotY, 0),
  171. plotX,
  172. plotY
  173. ];
  174. // reset for updating series later
  175. lastPoint.rightContX = lastPoint.rightContY = void 0;
  176. return ret;
  177. }
  178. /* eslint-enable valid-jsdoc */
  179. });
  180. /**
  181. * A `spline` series. If the [type](#series.spline.type) option is
  182. * not specified, it is inherited from [chart.type](#chart.type).
  183. *
  184. * @extends series,plotOptions.spline
  185. * @excluding dataParser, dataURL, step, boostThreshold, boostBlending
  186. * @product highcharts highstock
  187. * @apioption series.spline
  188. */
  189. /**
  190. * An array of data points for the series. For the `spline` series type,
  191. * points can be given in the following ways:
  192. *
  193. * 1. An array of numerical values. In this case, the numerical values will be
  194. * interpreted as `y` options. The `x` values will be automatically
  195. * calculated, either starting at 0 and incremented by 1, or from
  196. * `pointStart` and `pointInterval` given in the series options. If the axis
  197. * has categories, these will be used. Example:
  198. * ```js
  199. * data: [0, 5, 3, 5]
  200. * ```
  201. *
  202. * 2. An array of arrays with 2 values. In this case, the values correspond to
  203. * `x,y`. If the first value is a string, it is applied as the name of the
  204. * point, and the `x` value is inferred.
  205. * ```js
  206. * data: [
  207. * [0, 9],
  208. * [1, 2],
  209. * [2, 8]
  210. * ]
  211. * ```
  212. *
  213. * 3. An array of objects with named values. The following snippet shows only a
  214. * few settings, see the complete options set below. If the total number of
  215. * data points exceeds the series'
  216. * [turboThreshold](#series.spline.turboThreshold),
  217. * this option is not available.
  218. * ```js
  219. * data: [{
  220. * x: 1,
  221. * y: 9,
  222. * name: "Point2",
  223. * color: "#00FF00"
  224. * }, {
  225. * x: 1,
  226. * y: 0,
  227. * name: "Point1",
  228. * color: "#FF00FF"
  229. * }]
  230. * ```
  231. *
  232. * @sample {highcharts} highcharts/chart/reflow-true/
  233. * Numerical values
  234. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  235. * Arrays of numeric x and y
  236. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  237. * Arrays of datetime x and y
  238. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  239. * Arrays of point.name and y
  240. * @sample {highcharts} highcharts/series/data-array-of-objects/
  241. * Config objects
  242. *
  243. * @type {Array<number|Array<(number|string),(number|null)>|null|*>}
  244. * @extends series.line.data
  245. * @product highcharts highstock
  246. * @apioption series.spline.data
  247. */
  248. ''; // adds doclets above intro transpilat