broken-axis.src.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * @license Highcharts JS v6.1.0 (2018-04-13)
  3. *
  4. * (c) 2009-2017 Torstein Honsi
  5. *
  6. * License: www.highcharts.com/license
  7. */
  8. 'use strict';
  9. (function (factory) {
  10. if (typeof module === 'object' && module.exports) {
  11. module.exports = factory;
  12. } else {
  13. factory(Highcharts);
  14. }
  15. }(function (Highcharts) {
  16. (function (H) {
  17. /**
  18. * (c) 2009-2017 Torstein Honsi
  19. *
  20. * License: www.highcharts.com/license
  21. */
  22. var addEvent = H.addEvent,
  23. pick = H.pick,
  24. wrap = H.wrap,
  25. each = H.each,
  26. extend = H.extend,
  27. isArray = H.isArray,
  28. fireEvent = H.fireEvent,
  29. Axis = H.Axis,
  30. Series = H.Series;
  31. function stripArguments() {
  32. return Array.prototype.slice.call(arguments, 1);
  33. }
  34. extend(Axis.prototype, {
  35. isInBreak: function (brk, val) {
  36. var ret,
  37. repeat = brk.repeat || Infinity,
  38. from = brk.from,
  39. length = brk.to - brk.from,
  40. test = (
  41. val >= from ?
  42. (val - from) % repeat :
  43. repeat - ((from - val) % repeat)
  44. );
  45. if (!brk.inclusive) {
  46. ret = test < length && test !== 0;
  47. } else {
  48. ret = test <= length;
  49. }
  50. return ret;
  51. },
  52. isInAnyBreak: function (val, testKeep) {
  53. var breaks = this.options.breaks,
  54. i = breaks && breaks.length,
  55. inbrk,
  56. keep,
  57. ret;
  58. if (i) {
  59. while (i--) {
  60. if (this.isInBreak(breaks[i], val)) {
  61. inbrk = true;
  62. if (!keep) {
  63. keep = pick(
  64. breaks[i].showPoints,
  65. this.isXAxis ? false : true
  66. );
  67. }
  68. }
  69. }
  70. if (inbrk && testKeep) {
  71. ret = inbrk && !keep;
  72. } else {
  73. ret = inbrk;
  74. }
  75. }
  76. return ret;
  77. }
  78. });
  79. addEvent(Axis, 'afterSetTickPositions', function () {
  80. if (this.options.breaks) {
  81. var axis = this,
  82. tickPositions = this.tickPositions,
  83. info = this.tickPositions.info,
  84. newPositions = [],
  85. i;
  86. for (i = 0; i < tickPositions.length; i++) {
  87. if (!axis.isInAnyBreak(tickPositions[i])) {
  88. newPositions.push(tickPositions[i]);
  89. }
  90. }
  91. this.tickPositions = newPositions;
  92. this.tickPositions.info = info;
  93. }
  94. });
  95. // Force Axis to be not-ordinal when breaks are defined
  96. addEvent(Axis, 'afterSetOptions', function () {
  97. if (this.options.breaks && this.options.breaks.length) {
  98. this.options.ordinal = false;
  99. }
  100. });
  101. addEvent(Axis, 'afterInit', function () {
  102. var axis = this,
  103. breaks;
  104. breaks = this.options.breaks;
  105. axis.isBroken = (isArray(breaks) && !!breaks.length);
  106. if (axis.isBroken) {
  107. axis.val2lin = function (val) {
  108. var nval = val,
  109. brk,
  110. i;
  111. for (i = 0; i < axis.breakArray.length; i++) {
  112. brk = axis.breakArray[i];
  113. if (brk.to <= val) {
  114. nval -= brk.len;
  115. } else if (brk.from >= val) {
  116. break;
  117. } else if (axis.isInBreak(brk, val)) {
  118. nval -= (val - brk.from);
  119. break;
  120. }
  121. }
  122. return nval;
  123. };
  124. axis.lin2val = function (val) {
  125. var nval = val,
  126. brk,
  127. i;
  128. for (i = 0; i < axis.breakArray.length; i++) {
  129. brk = axis.breakArray[i];
  130. if (brk.from >= nval) {
  131. break;
  132. } else if (brk.to < nval) {
  133. nval += brk.len;
  134. } else if (axis.isInBreak(brk, nval)) {
  135. nval += brk.len;
  136. }
  137. }
  138. return nval;
  139. };
  140. axis.setExtremes = function (
  141. newMin,
  142. newMax,
  143. redraw,
  144. animation,
  145. eventArguments
  146. ) {
  147. // If trying to set extremes inside a break, extend it to before and
  148. // after the break ( #3857 )
  149. while (this.isInAnyBreak(newMin)) {
  150. newMin -= this.closestPointRange;
  151. }
  152. while (this.isInAnyBreak(newMax)) {
  153. newMax -= this.closestPointRange;
  154. }
  155. Axis.prototype.setExtremes.call(
  156. this,
  157. newMin,
  158. newMax,
  159. redraw,
  160. animation,
  161. eventArguments
  162. );
  163. };
  164. axis.setAxisTranslation = function (saveOld) {
  165. Axis.prototype.setAxisTranslation.call(this, saveOld);
  166. var breaks = axis.options.breaks,
  167. breakArrayT = [], // Temporary one
  168. breakArray = [],
  169. length = 0,
  170. inBrk,
  171. repeat,
  172. min = axis.userMin || axis.min,
  173. max = axis.userMax || axis.max,
  174. pointRangePadding = pick(axis.pointRangePadding, 0),
  175. start,
  176. i;
  177. // Min & max check (#4247)
  178. each(breaks, function (brk) {
  179. repeat = brk.repeat || Infinity;
  180. if (axis.isInBreak(brk, min)) {
  181. min += (brk.to % repeat) - (min % repeat);
  182. }
  183. if (axis.isInBreak(brk, max)) {
  184. max -= (max % repeat) - (brk.from % repeat);
  185. }
  186. });
  187. // Construct an array holding all breaks in the axis
  188. each(breaks, function (brk) {
  189. start = brk.from;
  190. repeat = brk.repeat || Infinity;
  191. while (start - repeat > min) {
  192. start -= repeat;
  193. }
  194. while (start < min) {
  195. start += repeat;
  196. }
  197. for (i = start; i < max; i += repeat) {
  198. breakArrayT.push({
  199. value: i,
  200. move: 'in'
  201. });
  202. breakArrayT.push({
  203. value: i + (brk.to - brk.from),
  204. move: 'out',
  205. size: brk.breakSize
  206. });
  207. }
  208. });
  209. breakArrayT.sort(function (a, b) {
  210. var ret;
  211. if (a.value === b.value) {
  212. ret = (a.move === 'in' ? 0 : 1) - (b.move === 'in' ? 0 : 1);
  213. } else {
  214. ret = a.value - b.value;
  215. }
  216. return ret;
  217. });
  218. // Simplify the breaks
  219. inBrk = 0;
  220. start = min;
  221. each(breakArrayT, function (brk) {
  222. inBrk += (brk.move === 'in' ? 1 : -1);
  223. if (inBrk === 1 && brk.move === 'in') {
  224. start = brk.value;
  225. }
  226. if (inBrk === 0) {
  227. breakArray.push({
  228. from: start,
  229. to: brk.value,
  230. len: brk.value - start - (brk.size || 0)
  231. });
  232. length += brk.value - start - (brk.size || 0);
  233. }
  234. });
  235. axis.breakArray = breakArray;
  236. // Used with staticScale, and below, the actual axis length when
  237. // breaks are substracted.
  238. axis.unitLength = max - min - length + pointRangePadding;
  239. fireEvent(axis, 'afterBreaks');
  240. if (axis.options.staticScale) {
  241. axis.transA = axis.options.staticScale;
  242. } else if (axis.unitLength) {
  243. axis.transA *= (max - axis.min + pointRangePadding) /
  244. axis.unitLength;
  245. }
  246. if (pointRangePadding) {
  247. axis.minPixelPadding = axis.transA * axis.minPointOffset;
  248. }
  249. axis.min = min;
  250. axis.max = max;
  251. };
  252. }
  253. });
  254. wrap(Series.prototype, 'generatePoints', function (proceed) {
  255. proceed.apply(this, stripArguments(arguments));
  256. var series = this,
  257. xAxis = series.xAxis,
  258. yAxis = series.yAxis,
  259. points = series.points,
  260. point,
  261. i = points.length,
  262. connectNulls = series.options.connectNulls,
  263. nullGap;
  264. if (xAxis && yAxis && (xAxis.options.breaks || yAxis.options.breaks)) {
  265. while (i--) {
  266. point = points[i];
  267. // Respect nulls inside the break (#4275)
  268. nullGap = point.y === null && connectNulls === false;
  269. if (
  270. !nullGap &&
  271. (
  272. xAxis.isInAnyBreak(point.x, true) ||
  273. yAxis.isInAnyBreak(point.y, true)
  274. )
  275. ) {
  276. points.splice(i, 1);
  277. if (this.data[i]) {
  278. // Removes the graphics for this point if they exist
  279. this.data[i].destroyElements();
  280. }
  281. }
  282. }
  283. }
  284. });
  285. function drawPointsWrapped(proceed) {
  286. proceed.apply(this);
  287. this.drawBreaks(this.xAxis, ['x']);
  288. this.drawBreaks(this.yAxis, pick(this.pointArrayMap, ['y']));
  289. }
  290. H.Series.prototype.drawBreaks = function (axis, keys) {
  291. var series = this,
  292. points = series.points,
  293. breaks,
  294. threshold,
  295. eventName,
  296. y;
  297. if (!axis) {
  298. return; // #5950
  299. }
  300. each(keys, function (key) {
  301. breaks = axis.breakArray || [];
  302. threshold = axis.isXAxis ?
  303. axis.min :
  304. pick(series.options.threshold, axis.min);
  305. each(points, function (point) {
  306. y = pick(point['stack' + key.toUpperCase()], point[key]);
  307. each(breaks, function (brk) {
  308. eventName = false;
  309. if (
  310. (threshold < brk.from && y > brk.to) ||
  311. (threshold > brk.from && y < brk.from)
  312. ) {
  313. eventName = 'pointBreak';
  314. } else if (
  315. (threshold < brk.from && y > brk.from && y < brk.to) ||
  316. (threshold > brk.from && y > brk.to && y < brk.from)
  317. ) {
  318. eventName = 'pointInBreak';
  319. }
  320. if (eventName) {
  321. fireEvent(axis, eventName, { point: point, brk: brk });
  322. }
  323. });
  324. });
  325. });
  326. };
  327. /**
  328. * Extend getGraphPath by identifying gaps in the data so that we can draw a gap
  329. * in the line or area. This was moved from ordinal axis module to broken axis
  330. * module as of #5045.
  331. */
  332. H.Series.prototype.gappedPath = function () {
  333. var currentDataGrouping = this.currentDataGrouping,
  334. groupingSize = currentDataGrouping && currentDataGrouping.totalRange,
  335. gapSize = this.options.gapSize,
  336. points = this.points.slice(),
  337. i = points.length - 1,
  338. yAxis = this.yAxis,
  339. xRange,
  340. stack;
  341. /**
  342. * Defines when to display a gap in the graph, together with the
  343. * [gapUnit](plotOptions.series.gapUnit) option.
  344. *
  345. * In case when `dataGrouping` is enabled, points can be grouped into a
  346. * larger time span. This can make the grouped points to have a greater
  347. * distance than the absolute value of `gapSize` property, which will result
  348. * in disappearing graph completely. To prevent this situation the mentioned
  349. * distance between grouped points is used instead of previously defined
  350. * `gapSize`.
  351. *
  352. * In practice, this option is most often used to visualize gaps in
  353. * time series. In a stock chart, intraday data is available for daytime
  354. * hours, while gaps will appear in nights and weekends.
  355. *
  356. * @type {Number}
  357. * @see [gapUnit](plotOptions.series.gapUnit) and
  358. * [xAxis.breaks](#xAxis.breaks)
  359. * @sample {highstock} stock/plotoptions/series-gapsize/
  360. * Setting the gap size to 2 introduces gaps for weekends in daily
  361. * datasets.
  362. * @default 0
  363. * @product highstock
  364. * @apioption plotOptions.series.gapSize
  365. */
  366. /**
  367. * Together with [gapSize](plotOptions.series.gapSize), this option defines
  368. * where to draw gaps in the graph.
  369. *
  370. * When the `gapUnit` is `relative` (default), a gap size of 5 means
  371. * that if the distance between two points is greater than five times
  372. * that of the two closest points, the graph will be broken.
  373. *
  374. * When the `gapUnit` is `value`, the gap is based on absolute axis values,
  375. * which on a datetime axis is milliseconds. This also applies to the
  376. * navigator series that inherits gap options from the base series.
  377. *
  378. * @type {String}
  379. * @see [gapSize](plotOptions.series.gapSize)
  380. * @default relative
  381. * @validvalue ["relative", "value"]
  382. * @since 5.0.13
  383. * @product highstock
  384. * @apioption plotOptions.series.gapUnit
  385. */
  386. if (gapSize && i > 0) { // #5008
  387. // Gap unit is relative
  388. if (this.options.gapUnit !== 'value') {
  389. gapSize *= this.closestPointRange;
  390. }
  391. // Setting a new gapSize in case dataGrouping is enabled (#7686)
  392. if (groupingSize && groupingSize > gapSize) {
  393. gapSize = groupingSize;
  394. }
  395. // extension for ordinal breaks
  396. while (i--) {
  397. if (points[i + 1].x - points[i].x > gapSize) {
  398. xRange = (points[i].x + points[i + 1].x) / 2;
  399. points.splice( // insert after this one
  400. i + 1,
  401. 0,
  402. {
  403. isNull: true,
  404. x: xRange
  405. }
  406. );
  407. // For stacked chart generate empty stack items, #6546
  408. if (this.options.stacking) {
  409. stack = yAxis.stacks[this.stackKey][xRange] =
  410. new H.StackItem(
  411. yAxis,
  412. yAxis.options.stackLabels,
  413. false,
  414. xRange,
  415. this.stack
  416. );
  417. stack.total = 0;
  418. }
  419. }
  420. }
  421. }
  422. // Call base method
  423. return this.getGraphPath(points);
  424. };
  425. wrap(H.seriesTypes.column.prototype, 'drawPoints', drawPointsWrapped);
  426. wrap(H.Series.prototype, 'drawPoints', drawPointsWrapped);
  427. }(Highcharts));
  428. }));