Polar.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* *
  2. * (c) 2010-2019 Torstein Honsi
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from '../parts/Globals.js';
  8. import '../parts/Utilities.js';
  9. import '../parts/Pointer.js';
  10. import '../parts/Series.js';
  11. import '../parts/Pointer.js';
  12. // Extensions for polar charts. Additionally, much of the geometry required for
  13. // polar charts is gathered in RadialAxes.js.
  14. var pick = H.pick,
  15. Pointer = H.Pointer,
  16. Series = H.Series,
  17. seriesTypes = H.seriesTypes,
  18. wrap = H.wrap,
  19. seriesProto = Series.prototype,
  20. pointerProto = Pointer.prototype,
  21. colProto;
  22. if (!H.polarExtended) {
  23. H.polarExtended = true;
  24. /**
  25. * Search a k-d tree by the point angle, used for shared tooltips in polar
  26. * charts
  27. */
  28. seriesProto.searchPointByAngle = function (e) {
  29. var series = this,
  30. chart = series.chart,
  31. xAxis = series.xAxis,
  32. center = xAxis.pane.center,
  33. plotX = e.chartX - center[0] - chart.plotLeft,
  34. plotY = e.chartY - center[1] - chart.plotTop;
  35. return this.searchKDTree({
  36. clientX: 180 + (Math.atan2(plotX, plotY) * (-180 / Math.PI))
  37. });
  38. };
  39. /**
  40. * #6212 Calculate connectors for spline series in polar chart.
  41. * @param {boolean} calculateNeighbours
  42. * Check if connectors should be calculated for neighbour points as
  43. * well allows short recurence
  44. */
  45. seriesProto.getConnectors = function (
  46. segment,
  47. index,
  48. calculateNeighbours,
  49. connectEnds
  50. ) {
  51. var i,
  52. prevPointInd,
  53. nextPointInd,
  54. previousPoint,
  55. nextPoint,
  56. previousX,
  57. previousY,
  58. nextX,
  59. nextY,
  60. plotX,
  61. plotY,
  62. ret,
  63. // 1 means control points midway between points, 2 means 1/3 from
  64. // the point, 3 is 1/4 etc;
  65. smoothing = 1.5,
  66. denom = smoothing + 1,
  67. leftContX,
  68. leftContY,
  69. rightContX,
  70. rightContY,
  71. dLControlPoint, // distance left control point
  72. dRControlPoint,
  73. leftContAngle,
  74. rightContAngle,
  75. jointAngle,
  76. addedNumber = connectEnds ? 1 : 0;
  77. // Calculate final index of points depending on the initial index value.
  78. // Because of calculating neighbours, index may be outisde segment
  79. // array.
  80. if (index >= 0 && index <= segment.length - 1) {
  81. i = index;
  82. } else if (index < 0) {
  83. i = segment.length - 1 + index;
  84. } else {
  85. i = 0;
  86. }
  87. prevPointInd = (i - 1 < 0) ? segment.length - (1 + addedNumber) : i - 1;
  88. nextPointInd = (i + 1 > segment.length - 1) ? addedNumber : i + 1;
  89. previousPoint = segment[prevPointInd];
  90. nextPoint = segment[nextPointInd];
  91. previousX = previousPoint.plotX;
  92. previousY = previousPoint.plotY;
  93. nextX = nextPoint.plotX;
  94. nextY = nextPoint.plotY;
  95. plotX = segment[i].plotX; // actual point
  96. plotY = segment[i].plotY;
  97. leftContX = (smoothing * plotX + previousX) / denom;
  98. leftContY = (smoothing * plotY + previousY) / denom;
  99. rightContX = (smoothing * plotX + nextX) / denom;
  100. rightContY = (smoothing * plotY + nextY) / denom;
  101. dLControlPoint = Math.sqrt(
  102. Math.pow(leftContX - plotX, 2) + Math.pow(leftContY - plotY, 2)
  103. );
  104. dRControlPoint = Math.sqrt(
  105. Math.pow(rightContX - plotX, 2) + Math.pow(rightContY - plotY, 2)
  106. );
  107. leftContAngle = Math.atan2(leftContY - plotY, leftContX - plotX);
  108. rightContAngle = Math.atan2(rightContY - plotY, rightContX - plotX);
  109. jointAngle = (Math.PI / 2) + ((leftContAngle + rightContAngle) / 2);
  110. // Ensure the right direction, jointAngle should be in the same quadrant
  111. // as leftContAngle
  112. if (Math.abs(leftContAngle - jointAngle) > Math.PI / 2) {
  113. jointAngle -= Math.PI;
  114. }
  115. // Find the corrected control points for a spline straight through the
  116. // point
  117. leftContX = plotX + Math.cos(jointAngle) * dLControlPoint;
  118. leftContY = plotY + Math.sin(jointAngle) * dLControlPoint;
  119. rightContX = plotX + Math.cos(Math.PI + jointAngle) * dRControlPoint;
  120. rightContY = plotY + Math.sin(Math.PI + jointAngle) * dRControlPoint;
  121. // push current point's connectors into returned object
  122. ret = {
  123. rightContX: rightContX,
  124. rightContY: rightContY,
  125. leftContX: leftContX,
  126. leftContY: leftContY,
  127. plotX: plotX,
  128. plotY: plotY
  129. };
  130. // calculate connectors for previous and next point and push them inside
  131. // returned object
  132. if (calculateNeighbours) {
  133. ret.prevPointCont = this.getConnectors(
  134. segment,
  135. prevPointInd,
  136. false,
  137. connectEnds
  138. );
  139. }
  140. return ret;
  141. };
  142. /**
  143. * Translate a point's plotX and plotY from the internal angle and radius
  144. * measures to true plotX, plotY coordinates
  145. */
  146. seriesProto.toXY = function (point) {
  147. var xy,
  148. chart = this.chart,
  149. plotX = point.plotX,
  150. plotY = point.plotY,
  151. clientX;
  152. // Save rectangular plotX, plotY for later computation
  153. point.rectPlotX = plotX;
  154. point.rectPlotY = plotY;
  155. // Find the polar plotX and plotY
  156. xy = this.xAxis.postTranslate(point.plotX, this.yAxis.len - plotY);
  157. point.plotX = point.polarPlotX = xy.x - chart.plotLeft;
  158. point.plotY = point.polarPlotY = xy.y - chart.plotTop;
  159. // If shared tooltip, record the angle in degrees in order to align X
  160. // points. Otherwise, use a standard k-d tree to get the nearest point
  161. // in two dimensions.
  162. if (this.kdByAngle) {
  163. clientX = (
  164. (plotX / Math.PI * 180) + this.xAxis.pane.options.startAngle
  165. ) % 360;
  166. if (clientX < 0) { // #2665
  167. clientX += 360;
  168. }
  169. point.clientX = clientX;
  170. } else {
  171. point.clientX = point.plotX;
  172. }
  173. };
  174. if (seriesTypes.spline) {
  175. /**
  176. * Overridden method for calculating a spline from one point to the next
  177. */
  178. wrap(
  179. seriesTypes.spline.prototype,
  180. 'getPointSpline',
  181. function (proceed, segment, point, i) {
  182. var ret,
  183. connectors;
  184. if (this.chart.polar) {
  185. // moveTo or lineTo
  186. if (!i) {
  187. ret = ['M', point.plotX, point.plotY];
  188. } else { // curve from last point to this
  189. connectors = this.getConnectors(
  190. segment,
  191. i,
  192. true,
  193. this.connectEnds
  194. );
  195. ret = [
  196. 'C',
  197. connectors.prevPointCont.rightContX,
  198. connectors.prevPointCont.rightContY,
  199. connectors.leftContX,
  200. connectors.leftContY,
  201. connectors.plotX,
  202. connectors.plotY
  203. ];
  204. }
  205. } else {
  206. ret = proceed.call(this, segment, point, i);
  207. }
  208. return ret;
  209. }
  210. );
  211. // #6430 Areasplinerange series use unwrapped getPointSpline method, so
  212. // we need to set this method again.
  213. if (seriesTypes.areasplinerange) {
  214. seriesTypes.areasplinerange.prototype.getPointSpline =
  215. seriesTypes.spline.prototype.getPointSpline;
  216. }
  217. }
  218. /**
  219. * Extend translate. The plotX and plotY values are computed as if the polar
  220. * chart were a cartesian plane, where plotX denotes the angle in radians
  221. * and (yAxis.len - plotY) is the pixel distance from center.
  222. */
  223. H.addEvent(Series, 'afterTranslate', function () {
  224. var chart = this.chart,
  225. points,
  226. i;
  227. if (chart.polar) {
  228. // Prepare k-d-tree handling. It searches by angle (clientX) in
  229. // case of shared tooltip, and by two dimensional distance in case
  230. // of non-shared.
  231. this.kdByAngle = chart.tooltip && chart.tooltip.shared;
  232. if (this.kdByAngle) {
  233. this.searchPoint = this.searchPointByAngle;
  234. } else {
  235. this.options.findNearestPointBy = 'xy';
  236. }
  237. // Postprocess plot coordinates
  238. if (!this.preventPostTranslate) {
  239. points = this.points;
  240. i = points.length;
  241. while (i--) {
  242. // Translate plotX, plotY from angle and radius to true plot
  243. // coordinates
  244. this.toXY(points[i]);
  245. }
  246. }
  247. // Perform clip after render
  248. if (!this.hasClipCircleSetter) {
  249. this.hasClipCircleSetter = Boolean(
  250. H.addEvent(this, 'afterRender', function () {
  251. var circ;
  252. if (chart.polar) {
  253. circ = this.yAxis.center;
  254. this.group.clip(
  255. chart.renderer.clipCircle(
  256. circ[0],
  257. circ[1],
  258. circ[2] / 2
  259. )
  260. );
  261. this.setClip = H.noop;
  262. }
  263. })
  264. );
  265. }
  266. }
  267. }, { order: 2 }); // Run after translation of ||-coords
  268. /**
  269. * Extend getSegmentPath to allow connecting ends across 0 to provide a
  270. * closed circle in line-like series.
  271. */
  272. wrap(seriesProto, 'getGraphPath', function (proceed, points) {
  273. var series = this,
  274. i,
  275. firstValid,
  276. popLastPoint;
  277. // Connect the path
  278. if (this.chart.polar) {
  279. points = points || this.points;
  280. // Append first valid point in order to connect the ends
  281. for (i = 0; i < points.length; i++) {
  282. if (!points[i].isNull) {
  283. firstValid = i;
  284. break;
  285. }
  286. }
  287. /**
  288. * Polar charts only. Whether to connect the ends of a line series
  289. * plot across the extremes.
  290. *
  291. * @sample {highcharts} highcharts/plotoptions/line-connectends-false/
  292. * Do not connect
  293. *
  294. * @type {boolean}
  295. * @since 2.3.0
  296. * @product highcharts
  297. * @apioption plotOptions.series.connectEnds
  298. */
  299. if (this.options.connectEnds !== false &&
  300. firstValid !== undefined
  301. ) {
  302. this.connectEnds = true; // re-used in splines
  303. points.splice(points.length, 0, points[firstValid]);
  304. popLastPoint = true;
  305. }
  306. // For area charts, pseudo points are added to the graph, now we
  307. // need to translate these
  308. points.forEach(function (point) {
  309. if (point.polarPlotY === undefined) {
  310. series.toXY(point);
  311. }
  312. });
  313. }
  314. // Run uber method
  315. var ret = proceed.apply(this, [].slice.call(arguments, 1));
  316. // #6212 points.splice method is adding points to an array. In case of
  317. // areaspline getGraphPath method is used two times and in both times
  318. // points are added to an array. That is why points.pop is used, to get
  319. // unmodified points.
  320. if (popLastPoint) {
  321. points.pop();
  322. }
  323. return ret;
  324. });
  325. var polarAnimate = function (proceed, init) {
  326. var chart = this.chart,
  327. animation = this.options.animation,
  328. group = this.group,
  329. markerGroup = this.markerGroup,
  330. center = this.xAxis.center,
  331. plotLeft = chart.plotLeft,
  332. plotTop = chart.plotTop,
  333. attribs;
  334. // Specific animation for polar charts
  335. if (chart.polar) {
  336. // Enable animation on polar charts only in SVG. In VML, the scaling
  337. // is different, plus animation would be so slow it would't matter.
  338. if (chart.renderer.isSVG) {
  339. if (animation === true) {
  340. animation = {};
  341. }
  342. // Initialize the animation
  343. if (init) {
  344. // Scale down the group and place it in the center
  345. attribs = {
  346. translateX: center[0] + plotLeft,
  347. translateY: center[1] + plotTop,
  348. scaleX: 0.001, // #1499
  349. scaleY: 0.001
  350. };
  351. group.attr(attribs);
  352. if (markerGroup) {
  353. markerGroup.attr(attribs);
  354. }
  355. // Run the animation
  356. } else {
  357. attribs = {
  358. translateX: plotLeft,
  359. translateY: plotTop,
  360. scaleX: 1,
  361. scaleY: 1
  362. };
  363. group.animate(attribs, animation);
  364. if (markerGroup) {
  365. markerGroup.animate(attribs, animation);
  366. }
  367. // Delete this function to allow it only once
  368. this.animate = null;
  369. }
  370. }
  371. // For non-polar charts, revert to the basic animation
  372. } else {
  373. proceed.call(this, init);
  374. }
  375. };
  376. // Define the animate method for regular series
  377. wrap(seriesProto, 'animate', polarAnimate);
  378. if (seriesTypes.column) {
  379. colProto = seriesTypes.column.prototype;
  380. colProto.polarArc = function (low, high, start, end) {
  381. var center = this.xAxis.center,
  382. len = this.yAxis.len;
  383. return this.chart.renderer.symbols.arc(
  384. center[0],
  385. center[1],
  386. len - high,
  387. null,
  388. {
  389. start: start,
  390. end: end,
  391. innerR: len - pick(low, len)
  392. }
  393. );
  394. };
  395. /**
  396. * Define the animate method for columnseries
  397. */
  398. wrap(colProto, 'animate', polarAnimate);
  399. /**
  400. * Extend the column prototype's translate method
  401. */
  402. wrap(colProto, 'translate', function (proceed) {
  403. var xAxis = this.xAxis,
  404. startAngleRad = xAxis.startAngleRad,
  405. start,
  406. points,
  407. point,
  408. i;
  409. this.preventPostTranslate = true;
  410. // Run uber method
  411. proceed.call(this);
  412. // Postprocess plot coordinates
  413. if (xAxis.isRadial) {
  414. points = this.points;
  415. i = points.length;
  416. while (i--) {
  417. point = points[i];
  418. start = point.barX + startAngleRad;
  419. point.shapeType = 'path';
  420. point.shapeArgs = {
  421. d: this.polarArc(
  422. point.yBottom,
  423. point.plotY,
  424. start,
  425. start + point.pointWidth
  426. )
  427. };
  428. // Provide correct plotX, plotY for tooltip
  429. this.toXY(point);
  430. point.tooltipPos = [point.plotX, point.plotY];
  431. point.ttBelow = point.plotY > xAxis.center[1];
  432. }
  433. }
  434. });
  435. /**
  436. * Align column data labels outside the columns. #1199.
  437. */
  438. wrap(colProto, 'alignDataLabel', function (
  439. proceed,
  440. point,
  441. dataLabel,
  442. options,
  443. alignTo,
  444. isNew
  445. ) {
  446. if (this.chart.polar) {
  447. var angle = point.rectPlotX / Math.PI * 180,
  448. align,
  449. verticalAlign;
  450. // Align nicely outside the perimeter of the columns
  451. if (options.align === null) {
  452. if (angle > 20 && angle < 160) {
  453. align = 'left'; // right hemisphere
  454. } else if (angle > 200 && angle < 340) {
  455. align = 'right'; // left hemisphere
  456. } else {
  457. align = 'center'; // top or bottom
  458. }
  459. options.align = align;
  460. }
  461. if (options.verticalAlign === null) {
  462. if (angle < 45 || angle > 315) {
  463. verticalAlign = 'bottom'; // top part
  464. } else if (angle > 135 && angle < 225) {
  465. verticalAlign = 'top'; // bottom part
  466. } else {
  467. verticalAlign = 'middle'; // left or right
  468. }
  469. options.verticalAlign = verticalAlign;
  470. }
  471. seriesProto.alignDataLabel.call(
  472. this,
  473. point,
  474. dataLabel,
  475. options,
  476. alignTo,
  477. isNew
  478. );
  479. } else {
  480. proceed.call(this, point, dataLabel, options, alignTo, isNew);
  481. }
  482. });
  483. }
  484. /**
  485. * Extend getCoordinates to prepare for polar axis values
  486. */
  487. wrap(pointerProto, 'getCoordinates', function (proceed, e) {
  488. var chart = this.chart,
  489. ret = {
  490. xAxis: [],
  491. yAxis: []
  492. };
  493. if (chart.polar) {
  494. chart.axes.forEach(function (axis) {
  495. var isXAxis = axis.isXAxis,
  496. center = axis.center,
  497. x = e.chartX - center[0] - chart.plotLeft,
  498. y = e.chartY - center[1] - chart.plotTop;
  499. ret[isXAxis ? 'xAxis' : 'yAxis'].push({
  500. axis: axis,
  501. value: axis.translate(
  502. isXAxis ?
  503. Math.PI - Math.atan2(x, y) : // angle
  504. // distance from center
  505. Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)),
  506. true
  507. )
  508. });
  509. });
  510. } else {
  511. ret = proceed.call(this, e);
  512. }
  513. return ret;
  514. });
  515. H.SVGRenderer.prototype.clipCircle = function (x, y, r) {
  516. var wrapper,
  517. id = H.uniqueKey(),
  518. clipPath = this.createElement('clipPath').attr({
  519. id: id
  520. }).add(this.defs);
  521. wrapper = this.circle(x, y, r).add(clipPath);
  522. wrapper.id = id;
  523. wrapper.clipPath = clipPath;
  524. return wrapper;
  525. };
  526. H.addEvent(H.Chart, 'getAxes', function () {
  527. if (!this.pane) {
  528. this.pane = [];
  529. }
  530. H.splat(this.options.pane).forEach(function (paneOptions) {
  531. new H.Pane( // eslint-disable-line no-new
  532. paneOptions,
  533. this
  534. );
  535. }, this);
  536. });
  537. H.addEvent(H.Chart, 'afterDrawChartBox', function () {
  538. this.pane.forEach(function (pane) {
  539. pane.render();
  540. });
  541. });
  542. /**
  543. * Extend chart.get to also search in panes. Used internally in
  544. * responsiveness and chart.update.
  545. */
  546. wrap(H.Chart.prototype, 'get', function (proceed, id) {
  547. return H.find(this.pane, function (pane) {
  548. return pane.options.id === id;
  549. }) || proceed.call(this, id);
  550. });
  551. }