Polar.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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 A from '../Core/Animation/AnimationUtilities.js';
  12. var animObject = A.animObject;
  13. import Chart from '../Core/Chart/Chart.js';
  14. import H from '../Core/Globals.js';
  15. import Pane from './Pane.js';
  16. import Pointer from '../Core/Pointer.js';
  17. import SVGRenderer from '../Core/Renderer/SVG/SVGRenderer.js';
  18. import U from '../Core/Utilities.js';
  19. var addEvent = U.addEvent, defined = U.defined, find = U.find, isNumber = U.isNumber, pick = U.pick, splat = U.splat, uniqueKey = U.uniqueKey, wrap = U.wrap;
  20. import '../Series/LineSeries.js';
  21. // Extensions for polar charts. Additionally, much of the geometry required for
  22. // polar charts is gathered in RadialAxes.js.
  23. var Series = H.Series, seriesTypes = H.seriesTypes, seriesProto = Series.prototype, pointerProto = Pointer.prototype, colProto, arearangeProto;
  24. /* eslint-disable no-invalid-this, valid-jsdoc */
  25. /**
  26. * Search a k-d tree by the point angle, used for shared tooltips in polar
  27. * charts
  28. * @private
  29. */
  30. seriesProto.searchPointByAngle = function (e) {
  31. var series = this, chart = series.chart, xAxis = series.xAxis, center = xAxis.pane.center, plotX = e.chartX - center[0] - chart.plotLeft, plotY = e.chartY - center[1] - chart.plotTop;
  32. return this.searchKDTree({
  33. clientX: 180 + (Math.atan2(plotX, plotY) * (-180 / Math.PI))
  34. });
  35. };
  36. /**
  37. * #6212 Calculate connectors for spline series in polar chart.
  38. * @private
  39. * @param {boolean} calculateNeighbours
  40. * Check if connectors should be calculated for neighbour points as
  41. * well allows short recurence
  42. */
  43. seriesProto.getConnectors = function (segment, index, calculateNeighbours, connectEnds) {
  44. var i, prevPointInd, nextPointInd, previousPoint, nextPoint, previousX, previousY, nextX, nextY, plotX, plotY, ret,
  45. // 1 means control points midway between points, 2 means 1/3 from
  46. // the point, 3 is 1/4 etc;
  47. smoothing = 1.5, denom = smoothing + 1, leftContX, leftContY, rightContX, rightContY, dLControlPoint, // distance left control point
  48. dRControlPoint, leftContAngle, rightContAngle, jointAngle, addedNumber = connectEnds ? 1 : 0;
  49. // Calculate final index of points depending on the initial index value.
  50. // Because of calculating neighbours, index may be outisde segment
  51. // array.
  52. if (index >= 0 && index <= segment.length - 1) {
  53. i = index;
  54. }
  55. else if (index < 0) {
  56. i = segment.length - 1 + index;
  57. }
  58. else {
  59. i = 0;
  60. }
  61. prevPointInd = (i - 1 < 0) ? segment.length - (1 + addedNumber) : i - 1;
  62. nextPointInd = (i + 1 > segment.length - 1) ? addedNumber : i + 1;
  63. previousPoint = segment[prevPointInd];
  64. nextPoint = segment[nextPointInd];
  65. previousX = previousPoint.plotX;
  66. previousY = previousPoint.plotY;
  67. nextX = nextPoint.plotX;
  68. nextY = nextPoint.plotY;
  69. plotX = segment[i].plotX; // actual point
  70. plotY = segment[i].plotY;
  71. leftContX = (smoothing * plotX + previousX) / denom;
  72. leftContY = (smoothing * plotY + previousY) / denom;
  73. rightContX = (smoothing * plotX + nextX) / denom;
  74. rightContY = (smoothing * plotY + nextY) / denom;
  75. dLControlPoint = Math.sqrt(Math.pow(leftContX - plotX, 2) + Math.pow(leftContY - plotY, 2));
  76. dRControlPoint = Math.sqrt(Math.pow(rightContX - plotX, 2) + Math.pow(rightContY - plotY, 2));
  77. leftContAngle = Math.atan2(leftContY - plotY, leftContX - plotX);
  78. rightContAngle = Math.atan2(rightContY - plotY, rightContX - plotX);
  79. jointAngle = (Math.PI / 2) + ((leftContAngle + rightContAngle) / 2);
  80. // Ensure the right direction, jointAngle should be in the same quadrant
  81. // as leftContAngle
  82. if (Math.abs(leftContAngle - jointAngle) > Math.PI / 2) {
  83. jointAngle -= Math.PI;
  84. }
  85. // Find the corrected control points for a spline straight through the
  86. // point
  87. leftContX = plotX + Math.cos(jointAngle) * dLControlPoint;
  88. leftContY = plotY + Math.sin(jointAngle) * dLControlPoint;
  89. rightContX = plotX + Math.cos(Math.PI + jointAngle) * dRControlPoint;
  90. rightContY = plotY + Math.sin(Math.PI + jointAngle) * dRControlPoint;
  91. // push current point's connectors into returned object
  92. ret = {
  93. rightContX: rightContX,
  94. rightContY: rightContY,
  95. leftContX: leftContX,
  96. leftContY: leftContY,
  97. plotX: plotX,
  98. plotY: plotY
  99. };
  100. // calculate connectors for previous and next point and push them inside
  101. // returned object
  102. if (calculateNeighbours) {
  103. ret.prevPointCont = this.getConnectors(segment, prevPointInd, false, connectEnds);
  104. }
  105. return ret;
  106. };
  107. /**
  108. * Translate a point's plotX and plotY from the internal angle and radius
  109. * measures to true plotX, plotY coordinates
  110. * @private
  111. */
  112. seriesProto.toXY = function (point) {
  113. var xy, chart = this.chart, xAxis = this.xAxis, yAxis = this.yAxis, plotX = point.plotX, plotY = point.plotY, series = point.series, inverted = chart.inverted, pointY = point.y, radius = inverted ? plotX : yAxis.len - plotY, clientX;
  114. // Corrected y position of inverted series other than column
  115. if (inverted && series && !series.isRadialBar) {
  116. point.plotY = plotY =
  117. typeof pointY === 'number' ? (yAxis.translate(pointY) || 0) : 0;
  118. }
  119. // Save rectangular plotX, plotY for later computation
  120. point.rectPlotX = plotX;
  121. point.rectPlotY = plotY;
  122. if (yAxis.center) {
  123. radius += yAxis.center[3] / 2;
  124. }
  125. // Find the polar plotX and plotY
  126. xy = inverted ? yAxis.postTranslate(plotY, radius) :
  127. xAxis.postTranslate(plotX, radius);
  128. point.plotX = point.polarPlotX = xy.x - chart.plotLeft;
  129. point.plotY = point.polarPlotY = xy.y - chart.plotTop;
  130. // If shared tooltip, record the angle in degrees in order to align X
  131. // points. Otherwise, use a standard k-d tree to get the nearest point
  132. // in two dimensions.
  133. if (this.kdByAngle) {
  134. clientX = ((plotX / Math.PI * 180) +
  135. xAxis.pane.options.startAngle) % 360;
  136. if (clientX < 0) { // #2665
  137. clientX += 360;
  138. }
  139. point.clientX = clientX;
  140. }
  141. else {
  142. point.clientX = point.plotX;
  143. }
  144. };
  145. if (seriesTypes.spline) {
  146. /**
  147. * Overridden method for calculating a spline from one point to the next
  148. * @private
  149. */
  150. wrap(seriesTypes.spline.prototype, 'getPointSpline', function (proceed, segment, point, i) {
  151. var ret, connectors;
  152. if (this.chart.polar) {
  153. // moveTo or lineTo
  154. if (!i) {
  155. ret = ['M', point.plotX, point.plotY];
  156. }
  157. else { // curve from last point to this
  158. connectors = this.getConnectors(segment, i, true, this.connectEnds);
  159. ret = [
  160. 'C',
  161. connectors.prevPointCont.rightContX,
  162. connectors.prevPointCont.rightContY,
  163. connectors.leftContX,
  164. connectors.leftContY,
  165. connectors.plotX,
  166. connectors.plotY
  167. ];
  168. }
  169. }
  170. else {
  171. ret = proceed.call(this, segment, point, i);
  172. }
  173. return ret;
  174. });
  175. // #6430 Areasplinerange series use unwrapped getPointSpline method, so
  176. // we need to set this method again.
  177. if (seriesTypes.areasplinerange) {
  178. seriesTypes.areasplinerange.prototype.getPointSpline =
  179. seriesTypes.spline.prototype.getPointSpline;
  180. }
  181. }
  182. /**
  183. * Extend translate. The plotX and plotY values are computed as if the polar
  184. * chart were a cartesian plane, where plotX denotes the angle in radians
  185. * and (yAxis.len - plotY) is the pixel distance from center.
  186. * @private
  187. */
  188. addEvent(Series, 'afterTranslate', function () {
  189. var series = this;
  190. var chart = series.chart;
  191. if (chart.polar && series.xAxis) {
  192. // Prepare k-d-tree handling. It searches by angle (clientX) in
  193. // case of shared tooltip, and by two dimensional distance in case
  194. // of non-shared.
  195. series.kdByAngle = chart.tooltip && chart.tooltip.shared;
  196. if (series.kdByAngle) {
  197. series.searchPoint = series.searchPointByAngle;
  198. }
  199. else {
  200. series.options.findNearestPointBy = 'xy';
  201. }
  202. // Postprocess plot coordinates
  203. if (!series.preventPostTranslate) {
  204. var points = series.points;
  205. var i = points.length;
  206. while (i--) {
  207. // Translate plotX, plotY from angle and radius to true plot
  208. // coordinates
  209. series.toXY(points[i]);
  210. // Treat points below Y axis min as null (#10082)
  211. if (!chart.hasParallelCoordinates &&
  212. !series.yAxis.reversed &&
  213. points[i].y < series.yAxis.min) {
  214. points[i].isNull = true;
  215. }
  216. }
  217. }
  218. // Perform clip after render
  219. if (!this.hasClipCircleSetter) {
  220. this.hasClipCircleSetter = !!series.eventsToUnbind.push(addEvent(series, 'afterRender', function () {
  221. var circ;
  222. if (chart.polar) {
  223. // For clipping purposes there is a need for
  224. // coordinates from the absolute center
  225. circ = this.yAxis.pane.center;
  226. if (!this.clipCircle) {
  227. this.clipCircle = chart.renderer.clipCircle(circ[0], circ[1], circ[2] / 2, circ[3] / 2);
  228. }
  229. else {
  230. this.clipCircle.animate({
  231. x: circ[0],
  232. y: circ[1],
  233. r: circ[2] / 2,
  234. innerR: circ[3] / 2
  235. });
  236. }
  237. this.group.clip(this.clipCircle);
  238. this.setClip = H.noop;
  239. }
  240. }));
  241. }
  242. }
  243. }, { order: 2 }); // Run after translation of ||-coords
  244. /**
  245. * Extend getSegmentPath to allow connecting ends across 0 to provide a
  246. * closed circle in line-like series.
  247. * @private
  248. */
  249. wrap(seriesProto, 'getGraphPath', function (proceed, points) {
  250. var series = this, i, firstValid, popLastPoint;
  251. // Connect the path
  252. if (this.chart.polar) {
  253. points = points || this.points;
  254. // Append first valid point in order to connect the ends
  255. for (i = 0; i < points.length; i++) {
  256. if (!points[i].isNull) {
  257. firstValid = i;
  258. break;
  259. }
  260. }
  261. /**
  262. * Polar charts only. Whether to connect the ends of a line series
  263. * plot across the extremes.
  264. *
  265. * @sample {highcharts} highcharts/plotoptions/line-connectends-false/
  266. * Do not connect
  267. *
  268. * @type {boolean}
  269. * @since 2.3.0
  270. * @product highcharts
  271. * @apioption plotOptions.series.connectEnds
  272. */
  273. if (this.options.connectEnds !== false &&
  274. typeof firstValid !== 'undefined') {
  275. this.connectEnds = true; // re-used in splines
  276. points.splice(points.length, 0, points[firstValid]);
  277. popLastPoint = true;
  278. }
  279. // For area charts, pseudo points are added to the graph, now we
  280. // need to translate these
  281. points.forEach(function (point) {
  282. if (typeof point.polarPlotY === 'undefined') {
  283. series.toXY(point);
  284. }
  285. });
  286. }
  287. // Run uber method
  288. var ret = proceed.apply(this, [].slice.call(arguments, 1));
  289. // #6212 points.splice method is adding points to an array. In case of
  290. // areaspline getGraphPath method is used two times and in both times
  291. // points are added to an array. That is why points.pop is used, to get
  292. // unmodified points.
  293. if (popLastPoint) {
  294. points.pop();
  295. }
  296. return ret;
  297. });
  298. var polarAnimate = function (proceed, init) {
  299. var series = this, chart = this.chart, animation = this.options.animation, group = this.group, markerGroup = this.markerGroup, center = this.xAxis.center, plotLeft = chart.plotLeft, plotTop = chart.plotTop, attribs, paneInnerR, graphic, shapeArgs, r, innerR;
  300. // Specific animation for polar charts
  301. if (chart.polar) {
  302. if (series.isRadialBar) {
  303. if (!init) {
  304. // Run the pie animation for radial bars
  305. series.startAngleRad = pick(series.translatedThreshold, series.xAxis.startAngleRad);
  306. H.seriesTypes.pie.prototype.animate.call(series, init);
  307. }
  308. }
  309. else {
  310. // Enable animation on polar charts only in SVG. In VML, the scaling
  311. // is different, plus animation would be so slow it would't matter.
  312. if (chart.renderer.isSVG) {
  313. animation = animObject(animation);
  314. // A different animation needed for column like series
  315. if (series.is('column')) {
  316. if (!init) {
  317. paneInnerR = center[3] / 2;
  318. series.points.forEach(function (point) {
  319. graphic = point.graphic;
  320. shapeArgs = point.shapeArgs;
  321. r = shapeArgs && shapeArgs.r;
  322. innerR = shapeArgs && shapeArgs.innerR;
  323. if (graphic && shapeArgs) {
  324. // start values
  325. graphic.attr({
  326. r: paneInnerR,
  327. innerR: paneInnerR
  328. });
  329. // animate
  330. graphic.animate({
  331. r: r,
  332. innerR: innerR
  333. }, series.options.animation);
  334. }
  335. });
  336. }
  337. }
  338. else {
  339. // Initialize the animation
  340. if (init) {
  341. // Scale down the group and place it in the center
  342. attribs = {
  343. translateX: center[0] + plotLeft,
  344. translateY: center[1] + plotTop,
  345. scaleX: 0.001,
  346. scaleY: 0.001
  347. };
  348. group.attr(attribs);
  349. if (markerGroup) {
  350. markerGroup.attr(attribs);
  351. }
  352. // Run the animation
  353. }
  354. else {
  355. attribs = {
  356. translateX: plotLeft,
  357. translateY: plotTop,
  358. scaleX: 1,
  359. scaleY: 1
  360. };
  361. group.animate(attribs, animation);
  362. if (markerGroup) {
  363. markerGroup.animate(attribs, animation);
  364. }
  365. }
  366. }
  367. }
  368. }
  369. // For non-polar charts, revert to the basic animation
  370. }
  371. else {
  372. proceed.call(this, init);
  373. }
  374. };
  375. // Define the animate method for regular series
  376. wrap(seriesProto, 'animate', polarAnimate);
  377. if (seriesTypes.column) {
  378. arearangeProto = seriesTypes.arearange.prototype;
  379. colProto = seriesTypes.column.prototype;
  380. colProto.polarArc = function (low, high, start, end) {
  381. var center = this.xAxis.center, len = this.yAxis.len, paneInnerR = center[3] / 2, r = len - high + paneInnerR, innerR = len - pick(low, len) + paneInnerR;
  382. // Prevent columns from shooting through the pane's center
  383. if (this.yAxis.reversed) {
  384. if (r < 0) {
  385. r = paneInnerR;
  386. }
  387. if (innerR < 0) {
  388. innerR = paneInnerR;
  389. }
  390. }
  391. // Return a new shapeArgs
  392. return {
  393. x: center[0],
  394. y: center[1],
  395. r: r,
  396. innerR: innerR,
  397. start: start,
  398. end: end
  399. };
  400. };
  401. /**
  402. * Define the animate method for columnseries
  403. * @private
  404. */
  405. wrap(colProto, 'animate', polarAnimate);
  406. /**
  407. * Extend the column prototype's translate method
  408. * @private
  409. */
  410. wrap(colProto, 'translate', function (proceed) {
  411. var series = this, options = series.options, threshold = options.threshold, stacking = options.stacking, chart = series.chart, xAxis = series.xAxis, yAxis = series.yAxis, reversed = yAxis.reversed, center = yAxis.center, startAngleRad = xAxis.startAngleRad, endAngleRad = xAxis.endAngleRad, visibleRange = endAngleRad - startAngleRad, thresholdAngleRad, points, point, i, yMin, yMax, start, end, tooltipPos, pointX, pointY, stackValues, stack, barX, innerR, r;
  412. series.preventPostTranslate = true;
  413. // Run uber method
  414. proceed.call(series);
  415. // Postprocess plot coordinates
  416. if (xAxis.isRadial) {
  417. points = series.points;
  418. i = points.length;
  419. yMin = yAxis.translate(yAxis.min);
  420. yMax = yAxis.translate(yAxis.max);
  421. threshold = options.threshold || 0;
  422. if (chart.inverted) {
  423. // Finding a correct threshold
  424. if (isNumber(threshold)) {
  425. thresholdAngleRad = yAxis.translate(threshold);
  426. // Checks if threshold is outside the visible range
  427. if (defined(thresholdAngleRad)) {
  428. if (thresholdAngleRad < 0) {
  429. thresholdAngleRad = 0;
  430. }
  431. else if (thresholdAngleRad > visibleRange) {
  432. thresholdAngleRad = visibleRange;
  433. }
  434. // Adding start angle offset
  435. series.translatedThreshold =
  436. thresholdAngleRad + startAngleRad;
  437. }
  438. }
  439. }
  440. while (i--) {
  441. point = points[i];
  442. barX = point.barX;
  443. pointX = point.x;
  444. pointY = point.y;
  445. point.shapeType = 'arc';
  446. if (chart.inverted) {
  447. point.plotY = yAxis.translate(pointY);
  448. if (stacking && yAxis.stacking) {
  449. stack = yAxis.stacking.stacks[(pointY < 0 ? '-' : '') +
  450. series.stackKey];
  451. if (series.visible && stack && stack[pointX]) {
  452. if (!point.isNull) {
  453. stackValues = stack[pointX].points[series.getStackIndicator(void 0, pointX, series.index).key];
  454. // Translating to radial values
  455. start = yAxis.translate(stackValues[0]);
  456. end = yAxis.translate(stackValues[1]);
  457. // If starting point is beyond the
  458. // range, set it to 0
  459. if (defined(start)) {
  460. start = U.clamp(start, 0, visibleRange);
  461. }
  462. }
  463. }
  464. }
  465. else {
  466. // Initial start and end angles for radial bar
  467. start = thresholdAngleRad;
  468. end = point.plotY;
  469. }
  470. if (start > end) {
  471. // Swapping start and end
  472. end = [start, start = end][0];
  473. }
  474. // Prevent from rendering point outside the
  475. // acceptable circular range
  476. if (!reversed) {
  477. if (start < yMin) {
  478. start = yMin;
  479. }
  480. else if (end > yMax) {
  481. end = yMax;
  482. }
  483. else if (end < yMin || start > yMax) {
  484. start = end = 0;
  485. }
  486. }
  487. else {
  488. if (end > yMin) {
  489. end = yMin;
  490. }
  491. else if (start < yMax) {
  492. start = yMax;
  493. }
  494. else if (start > yMin || end < yMax) {
  495. start = end = visibleRange;
  496. }
  497. }
  498. if (yAxis.min > yAxis.max) {
  499. start = end = reversed ? visibleRange : 0;
  500. }
  501. start += startAngleRad;
  502. end += startAngleRad;
  503. if (center) {
  504. point.barX = barX += center[3] / 2;
  505. }
  506. // In case when radius, inner radius or both are
  507. // negative, a point is rendered but partially or as
  508. // a center point
  509. innerR = Math.max(barX, 0);
  510. r = Math.max(barX + point.pointWidth, 0);
  511. point.shapeArgs = {
  512. x: center && center[0],
  513. y: center && center[1],
  514. r: r,
  515. innerR: innerR,
  516. start: start,
  517. end: end
  518. };
  519. // Fade out the points if not inside the polar "plot area"
  520. point.opacity = start === end ? 0 : void 0;
  521. // A correct value for stacked or not fully visible
  522. // point
  523. point.plotY = (defined(series.translatedThreshold) &&
  524. (start < series.translatedThreshold ? start : end)) -
  525. startAngleRad;
  526. }
  527. else {
  528. start = barX + startAngleRad;
  529. // Changed the way polar columns are drawn in order to make
  530. // it more consistent with the drawing of inverted columns
  531. // (they are using the same function now). Also, it was
  532. // essential to make the animation work correctly (the
  533. // scaling of the group) is replaced by animating each
  534. // element separately.
  535. point.shapeArgs = series.polarArc(point.yBottom, point.plotY, start, start + point.pointWidth);
  536. }
  537. // Provided a correct coordinates for the tooltip
  538. series.toXY(point);
  539. if (chart.inverted) {
  540. tooltipPos = yAxis.postTranslate(point.rectPlotY, barX + point.pointWidth / 2);
  541. point.tooltipPos = [
  542. tooltipPos.x - chart.plotLeft,
  543. tooltipPos.y - chart.plotTop
  544. ];
  545. }
  546. else {
  547. point.tooltipPos = [point.plotX, point.plotY];
  548. }
  549. if (center) {
  550. point.ttBelow = point.plotY > center[1];
  551. }
  552. }
  553. }
  554. });
  555. /**
  556. * Find correct align and vertical align based on an angle in polar chart
  557. * @private
  558. */
  559. colProto.findAlignments = function (angle, options) {
  560. var align, verticalAlign;
  561. if (options.align === null) {
  562. if (angle > 20 && angle < 160) {
  563. align = 'left'; // right hemisphere
  564. }
  565. else if (angle > 200 && angle < 340) {
  566. align = 'right'; // left hemisphere
  567. }
  568. else {
  569. align = 'center'; // top or bottom
  570. }
  571. options.align = align;
  572. }
  573. if (options.verticalAlign === null) {
  574. if (angle < 45 || angle > 315) {
  575. verticalAlign = 'bottom'; // top part
  576. }
  577. else if (angle > 135 && angle < 225) {
  578. verticalAlign = 'top'; // bottom part
  579. }
  580. else {
  581. verticalAlign = 'middle'; // left or right
  582. }
  583. options.verticalAlign = verticalAlign;
  584. }
  585. return options;
  586. };
  587. if (arearangeProto) {
  588. arearangeProto.findAlignments = colProto.findAlignments;
  589. }
  590. /**
  591. * Align column data labels outside the columns. #1199.
  592. * @private
  593. */
  594. wrap(colProto, 'alignDataLabel', function (proceed, point, dataLabel, options, alignTo, isNew) {
  595. var chart = this.chart, inside = pick(options.inside, !!this.options.stacking), angle, shapeArgs, labelPos;
  596. if (chart.polar) {
  597. angle = point.rectPlotX / Math.PI * 180;
  598. if (!chart.inverted) {
  599. // Align nicely outside the perimeter of the columns
  600. if (this.findAlignments) {
  601. options = this.findAlignments(angle, options);
  602. }
  603. }
  604. else { // Required corrections for data labels of inverted bars
  605. // The plotX and plotY are correctly set therefore they
  606. // don't need to be swapped (inverted argument is false)
  607. this.forceDL = chart.isInsidePlot(point.plotX, Math.round(point.plotY), false);
  608. // Checks if labels should be positioned inside
  609. if (inside && point.shapeArgs) {
  610. shapeArgs = point.shapeArgs;
  611. // Calculates pixel positions for a data label to be
  612. // inside
  613. labelPos =
  614. this.yAxis.postTranslate(
  615. // angle
  616. (shapeArgs.start + shapeArgs.end) / 2 -
  617. this
  618. .xAxis.startAngleRad,
  619. // radius
  620. point.barX +
  621. point.pointWidth / 2);
  622. alignTo = {
  623. x: labelPos.x - chart.plotLeft,
  624. y: labelPos.y - chart.plotTop
  625. };
  626. }
  627. else if (point.tooltipPos) {
  628. alignTo = {
  629. x: point.tooltipPos[0],
  630. y: point.tooltipPos[1]
  631. };
  632. }
  633. options.align = pick(options.align, 'center');
  634. options.verticalAlign =
  635. pick(options.verticalAlign, 'middle');
  636. }
  637. seriesProto.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  638. // Hide label of a point (only inverted) that is outside the
  639. // visible y range
  640. if (this.isRadialBar && point.shapeArgs &&
  641. point.shapeArgs.start === point.shapeArgs.end) {
  642. dataLabel.hide(true);
  643. }
  644. }
  645. else {
  646. proceed.call(this, point, dataLabel, options, alignTo, isNew);
  647. }
  648. });
  649. }
  650. /**
  651. * Extend getCoordinates to prepare for polar axis values
  652. * @private
  653. */
  654. wrap(pointerProto, 'getCoordinates', function (proceed, e) {
  655. var chart = this.chart, ret = {
  656. xAxis: [],
  657. yAxis: []
  658. };
  659. if (chart.polar) {
  660. chart.axes.forEach(function (axis) {
  661. var isXAxis = axis.isXAxis, center = axis.center, x, y;
  662. // Skip colorAxis
  663. if (axis.coll === 'colorAxis') {
  664. return;
  665. }
  666. x = e.chartX - center[0] - chart.plotLeft;
  667. y = e.chartY - center[1] - chart.plotTop;
  668. ret[isXAxis ? 'xAxis' : 'yAxis'].push({
  669. axis: axis,
  670. value: axis.translate(isXAxis ?
  671. Math.PI - Math.atan2(x, y) : // angle
  672. // distance from center
  673. Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)), true)
  674. });
  675. });
  676. }
  677. else {
  678. ret = proceed.call(this, e);
  679. }
  680. return ret;
  681. });
  682. SVGRenderer.prototype.clipCircle = function (x, y, r, innerR) {
  683. var wrapper, id = uniqueKey(), clipPath = this.createElement('clipPath').attr({
  684. id: id
  685. }).add(this.defs);
  686. wrapper = innerR ?
  687. this.arc(x, y, r, innerR, 0, 2 * Math.PI).add(clipPath) :
  688. this.circle(x, y, r).add(clipPath);
  689. wrapper.id = id;
  690. wrapper.clipPath = clipPath;
  691. return wrapper;
  692. };
  693. addEvent(Chart, 'getAxes', function () {
  694. if (!this.pane) {
  695. this.pane = [];
  696. }
  697. splat(this.options.pane).forEach(function (paneOptions) {
  698. new Pane(// eslint-disable-line no-new
  699. paneOptions, this);
  700. }, this);
  701. });
  702. addEvent(Chart, 'afterDrawChartBox', function () {
  703. this.pane.forEach(function (pane) {
  704. pane.render();
  705. });
  706. });
  707. addEvent(H.Series, 'afterInit', function () {
  708. var chart = this.chart;
  709. // Add flags that identifies radial inverted series
  710. if (chart.inverted && chart.polar) {
  711. this.isRadialSeries = true;
  712. if (this.is('column')) {
  713. this.isRadialBar = true;
  714. }
  715. }
  716. });
  717. /**
  718. * Extend chart.get to also search in panes. Used internally in
  719. * responsiveness and chart.update.
  720. * @private
  721. */
  722. wrap(Chart.prototype, 'get', function (proceed, id) {
  723. return find(this.pane, function (pane) {
  724. return pane.options.id === id;
  725. }) || proceed.call(this, id);
  726. });