RadialAxis.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 Axis from './Axis.js';
  12. import Tick from './Tick.js';
  13. import HiddenAxis from './HiddenAxis.js';
  14. import U from '../Utilities.js';
  15. var addEvent = U.addEvent, correctFloat = U.correctFloat, defined = U.defined, extend = U.extend, fireEvent = U.fireEvent, isNumber = U.isNumber, merge = U.merge, pick = U.pick, pInt = U.pInt, relativeLength = U.relativeLength, wrap = U.wrap;
  16. /**
  17. * @private
  18. * @class
  19. */
  20. var RadialAxis = /** @class */ (function () {
  21. function RadialAxis() {
  22. }
  23. /* *
  24. *
  25. * Static Functions
  26. *
  27. * */
  28. RadialAxis.init = function (axis) {
  29. var axisProto = Axis.prototype;
  30. // Merge and set options.
  31. axis.setOptions = function (userOptions) {
  32. var options = this.options = merge(axis.constructor.defaultOptions, this.defaultPolarOptions, userOptions);
  33. // Make sure the plotBands array is instanciated for each Axis
  34. // (#2649)
  35. if (!options.plotBands) {
  36. options.plotBands = [];
  37. }
  38. fireEvent(this, 'afterSetOptions');
  39. };
  40. // Wrap the getOffset method to return zero offset for title or labels
  41. // in a radial axis.
  42. axis.getOffset = function () {
  43. // Call the Axis prototype method (the method we're in now is on the
  44. // instance)
  45. axisProto.getOffset.call(this);
  46. // Title or label offsets are not counted
  47. this.chart.axisOffset[this.side] = 0;
  48. };
  49. /**
  50. * Get the path for the axis line. This method is also referenced in the
  51. * getPlotLinePath method.
  52. *
  53. * @private
  54. *
  55. * @param {number} _lineWidth
  56. * Line width is not used.
  57. *
  58. * @param {number} [radius]
  59. * Radius of radial path.
  60. *
  61. * @param {number} [innerRadius]
  62. * Inner radius of radial path.
  63. *
  64. * @return {RadialAxisPath}
  65. */
  66. axis.getLinePath = function (_lineWidth, radius, innerRadius) {
  67. var center = this.pane.center, end, chart = this.chart, r = pick(radius, center[2] / 2 - this.offset), path;
  68. if (typeof innerRadius === 'undefined') {
  69. innerRadius = this.horiz ? 0 : this.center && -this.center[3] / 2;
  70. }
  71. // In case when innerSize of pane is set, it must be included
  72. if (innerRadius) {
  73. r += innerRadius;
  74. }
  75. if (this.isCircular || typeof radius !== 'undefined') {
  76. path = this.chart.renderer.symbols.arc(this.left + center[0], this.top + center[1], r, r, {
  77. start: this.startAngleRad,
  78. end: this.endAngleRad,
  79. open: true,
  80. innerR: 0
  81. });
  82. // Bounds used to position the plotLine label next to the line
  83. // (#7117)
  84. path.xBounds = [this.left + center[0]];
  85. path.yBounds = [this.top + center[1] - r];
  86. }
  87. else {
  88. end = this.postTranslate(this.angleRad, r);
  89. path = [
  90. ['M', this.center[0] + chart.plotLeft, this.center[1] + chart.plotTop],
  91. ['L', end.x, end.y]
  92. ];
  93. }
  94. return path;
  95. };
  96. /**
  97. * Override setAxisTranslation by setting the translation to the
  98. * difference in rotation. This allows the translate method to return
  99. * angle for any given value.
  100. *
  101. * @private
  102. */
  103. axis.setAxisTranslation = function () {
  104. // Call uber method
  105. axisProto.setAxisTranslation.call(this);
  106. // Set transA and minPixelPadding
  107. if (this.center) { // it's not defined the first time
  108. if (this.isCircular) {
  109. this.transA = (this.endAngleRad - this.startAngleRad) /
  110. ((this.max - this.min) || 1);
  111. }
  112. else {
  113. // The transA here is the length of the axis, so in case
  114. // of inner radius, the length must be decreased by it
  115. this.transA = ((this.center[2] - this.center[3]) / 2) /
  116. ((this.max - this.min) || 1);
  117. }
  118. if (this.isXAxis) {
  119. this.minPixelPadding = this.transA * this.minPointOffset;
  120. }
  121. else {
  122. // This is a workaround for regression #2593, but categories
  123. // still don't position correctly.
  124. this.minPixelPadding = 0;
  125. }
  126. }
  127. };
  128. /**
  129. * In case of auto connect, add one closestPointRange to the max value
  130. * right before tickPositions are computed, so that ticks will extend
  131. * passed the real max.
  132. * @private
  133. */
  134. axis.beforeSetTickPositions = function () {
  135. // If autoConnect is true, polygonal grid lines are connected, and
  136. // one closestPointRange is added to the X axis to prevent the last
  137. // point from overlapping the first.
  138. this.autoConnect = (this.isCircular &&
  139. typeof pick(this.userMax, this.options.max) === 'undefined' &&
  140. correctFloat(this.endAngleRad - this.startAngleRad) ===
  141. correctFloat(2 * Math.PI));
  142. // This will lead to add an extra tick to xAxis in order to display
  143. // a correct range on inverted polar
  144. if (!this.isCircular && this.chart.inverted) {
  145. this.max++;
  146. }
  147. if (this.autoConnect) {
  148. this.max += ((this.categories && 1) ||
  149. this.pointRange ||
  150. this.closestPointRange ||
  151. 0); // #1197, #2260
  152. }
  153. };
  154. /**
  155. * Override the setAxisSize method to use the arc's circumference as
  156. * length. This allows tickPixelInterval to apply to pixel lengths along
  157. * the perimeter.
  158. * @private
  159. */
  160. axis.setAxisSize = function () {
  161. var center, start;
  162. axisProto.setAxisSize.call(this);
  163. if (this.isRadial) {
  164. // Set the center array
  165. this.pane.updateCenter(this);
  166. // In case when the innerSize is set in a polar chart, the axis'
  167. // center cannot be a reference to pane's center
  168. center = this.center = extend([], this.pane.center);
  169. // The sector is used in Axis.translate to compute the
  170. // translation of reversed axis points (#2570)
  171. if (this.isCircular) {
  172. this.sector = this.endAngleRad - this.startAngleRad;
  173. }
  174. else {
  175. // When the pane's startAngle or the axis' angle is set then
  176. // new x and y values for vertical axis' center must be
  177. // calulated
  178. start = this.postTranslate(this.angleRad, center[3] / 2);
  179. center[0] = start.x - this.chart.plotLeft;
  180. center[1] = start.y - this.chart.plotTop;
  181. }
  182. // Axis len is used to lay out the ticks
  183. this.len = this.width = this.height =
  184. (center[2] - center[3]) * pick(this.sector, 1) / 2;
  185. }
  186. };
  187. /**
  188. * Returns the x, y coordinate of a point given by a value and a pixel
  189. * distance from center.
  190. *
  191. * @private
  192. *
  193. * @param {number} value
  194. * Point value.
  195. *
  196. * @param {number} [length]
  197. * Distance from center.
  198. *
  199. * @return {Highcharts.PositionObject}
  200. */
  201. axis.getPosition = function (value, length) {
  202. var translatedVal = this.translate(value);
  203. return this.postTranslate(this.isCircular ? translatedVal : this.angleRad, // #2848
  204. // In case when translatedVal is negative, the 0 value must be
  205. // used instead, in order to deal with lines and labels that
  206. // fall out of the visible range near the center of a pane
  207. pick(this.isCircular ?
  208. length :
  209. (translatedVal < 0 ? 0 : translatedVal), this.center[2] / 2) - this.offset);
  210. };
  211. /**
  212. * Translate from intermediate plotX (angle), plotY (axis.len - radius)
  213. * to final chart coordinates.
  214. *
  215. * @private
  216. *
  217. * @param {number} angle
  218. * Translation angle.
  219. *
  220. * @param {number} radius
  221. * Translation radius.
  222. *
  223. * @return {Highcharts.PositionObject}
  224. */
  225. axis.postTranslate = function (angle, radius) {
  226. var chart = this.chart, center = this.center;
  227. angle = this.startAngleRad + angle;
  228. return {
  229. x: chart.plotLeft + center[0] + Math.cos(angle) * radius,
  230. y: chart.plotTop + center[1] + Math.sin(angle) * radius
  231. };
  232. };
  233. /**
  234. * Find the path for plot bands along the radial axis.
  235. *
  236. * @private
  237. *
  238. * @param {number} from
  239. * From value.
  240. *
  241. * @param {number} to
  242. * To value.
  243. *
  244. * @param {Highcharts.AxisPlotBandsOptions} options
  245. * Band options.
  246. *
  247. * @return {RadialAxisPath}
  248. */
  249. axis.getPlotBandPath = function (from, to, options) {
  250. var radiusToPixels = function (radius) {
  251. if (typeof radius === 'string') {
  252. var r = parseInt(radius, 10);
  253. if (percentRegex.test(radius)) {
  254. r = (r * fullRadius) / 100;
  255. }
  256. return r;
  257. }
  258. return radius;
  259. };
  260. var center = this.center, startAngleRad = this.startAngleRad, fullRadius = center[2] / 2, offset = Math.min(this.offset, 0), percentRegex = /%$/, start, end, angle, xOnPerimeter, open, isCircular = this.isCircular, // X axis in a polar chart
  261. path, outerRadius = pick(radiusToPixels(options.outerRadius), fullRadius), innerRadius = radiusToPixels(options.innerRadius), thickness = pick(radiusToPixels(options.thickness), 10);
  262. // Polygonal plot bands
  263. if (this.options.gridLineInterpolation === 'polygon') {
  264. path = this.getPlotLinePath({ value: from }).concat(this.getPlotLinePath({ value: to, reverse: true }));
  265. // Circular grid bands
  266. }
  267. else {
  268. // Keep within bounds
  269. from = Math.max(from, this.min);
  270. to = Math.min(to, this.max);
  271. var transFrom = this.translate(from);
  272. var transTo = this.translate(to);
  273. // Plot bands on Y axis (radial axis) - inner and outer
  274. // radius depend on to and from
  275. if (!isCircular) {
  276. outerRadius = transFrom || 0;
  277. innerRadius = transTo || 0;
  278. }
  279. // Handle full circle
  280. if (options.shape === 'circle' || !isCircular) {
  281. start = -Math.PI / 2;
  282. end = Math.PI * 1.5;
  283. open = true;
  284. }
  285. else {
  286. start = startAngleRad + (transFrom || 0);
  287. end = startAngleRad + (transTo || 0);
  288. }
  289. outerRadius -= offset; // #5283
  290. thickness -= offset; // #5283
  291. path = this.chart.renderer.symbols.arc(this.left + center[0], this.top + center[1], outerRadius, outerRadius, {
  292. // Math is for reversed yAxis (#3606)
  293. start: Math.min(start, end),
  294. end: Math.max(start, end),
  295. innerR: pick(innerRadius, outerRadius - thickness),
  296. open: open
  297. });
  298. // Provide positioning boxes for the label (#6406)
  299. if (isCircular) {
  300. angle = (end + start) / 2;
  301. xOnPerimeter = (this.left +
  302. center[0] +
  303. (center[2] / 2) * Math.cos(angle));
  304. path.xBounds = angle > -Math.PI / 2 && angle < Math.PI / 2 ?
  305. // Right hemisphere
  306. [xOnPerimeter, this.chart.plotWidth] :
  307. // Left hemisphere
  308. [0, xOnPerimeter];
  309. path.yBounds = [
  310. this.top + center[1] + (center[2] / 2) * Math.sin(angle)
  311. ];
  312. // Shift up or down to get the label clear of the perimeter
  313. path.yBounds[0] += ((angle > -Math.PI && angle < 0) ||
  314. (angle > Math.PI)) ? -10 : 10;
  315. }
  316. }
  317. return path;
  318. };
  319. // Find the correct end values of crosshair in polar.
  320. axis.getCrosshairPosition = function (options, x1, y1) {
  321. var axis = this, value = options.value, center = axis.pane.center, shapeArgs, end, x2, y2;
  322. if (axis.isCircular) {
  323. if (!defined(value)) {
  324. // When the snap is set to false
  325. x2 = options.chartX || 0;
  326. y2 = options.chartY || 0;
  327. value = axis.translate(Math.atan2(y2 - y1, x2 - x1) - axis.startAngleRad, true);
  328. }
  329. else if (options.point) {
  330. // When the snap is set to true
  331. shapeArgs = options.point.shapeArgs || {};
  332. if (shapeArgs.start) {
  333. // Find a true value of the point based on the
  334. // angle
  335. value = axis.chart.inverted ?
  336. axis.translate(options.point.rectPlotY, true) :
  337. options.point.x;
  338. }
  339. }
  340. end = axis.getPosition(value);
  341. x2 = end.x;
  342. y2 = end.y;
  343. }
  344. else {
  345. if (!defined(value)) {
  346. x2 = options.chartX;
  347. y2 = options.chartY;
  348. }
  349. if (defined(x2) && defined(y2)) {
  350. // Calculate radius of non-circular axis' crosshair
  351. y1 = center[1] + axis.chart.plotTop;
  352. value = axis.translate(Math.min(Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)), center[2] / 2) - center[3] / 2, true);
  353. }
  354. }
  355. return [value, x2 || 0, y2 || 0];
  356. };
  357. // Find the path for plot lines perpendicular to the radial axis.
  358. axis.getPlotLinePath = function (options) {
  359. var axis = this, center = axis.pane.center, chart = axis.chart, inverted = chart.inverted, value = options.value, reverse = options.reverse, end = axis.getPosition(value), background = axis.pane.options.background ?
  360. (axis.pane.options.background[0] ||
  361. axis.pane.options.background) :
  362. {}, innerRadius = background.innerRadius || '0%', outerRadius = background.outerRadius || '100%', x1 = center[0] + chart.plotLeft, y1 = center[1] + chart.plotTop, x2 = end.x, y2 = end.y, height = axis.height, isCrosshair = options.isCrosshair, paneInnerR = center[3] / 2, innerRatio, distance, a, b, otherAxis, xy, tickPositions, crossPos, path;
  363. // Crosshair logic
  364. if (isCrosshair) {
  365. // Find crosshair's position and perform destructuring
  366. // assignment
  367. crossPos = this.getCrosshairPosition(options, x1, y1);
  368. value = crossPos[0];
  369. x2 = crossPos[1];
  370. y2 = crossPos[2];
  371. }
  372. // Spokes
  373. if (axis.isCircular) {
  374. distance =
  375. Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  376. a = (typeof innerRadius === 'string') ?
  377. relativeLength(innerRadius, 1) : (innerRadius / distance);
  378. b = (typeof outerRadius === 'string') ?
  379. relativeLength(outerRadius, 1) : (outerRadius / distance);
  380. // To ensure that gridlines won't be displayed in area
  381. // defined by innerSize in case of custom radiuses of pane's
  382. // background
  383. if (center && paneInnerR) {
  384. innerRatio = paneInnerR / distance;
  385. if (a < innerRatio) {
  386. a = innerRatio;
  387. }
  388. if (b < innerRatio) {
  389. b = innerRatio;
  390. }
  391. }
  392. path = [
  393. ['M', x1 + a * (x2 - x1), y1 - a * (y1 - y2)],
  394. ['L', x2 - (1 - b) * (x2 - x1), y2 + (1 - b) * (y1 - y2)]
  395. ];
  396. // Concentric circles
  397. }
  398. else {
  399. // Pick the right values depending if it is grid line or
  400. // crosshair
  401. value = axis.translate(value);
  402. // This is required in case when xAxis is non-circular to
  403. // prevent grid lines (or crosshairs, if enabled) from
  404. // rendering above the center after they supposed to be
  405. // displayed below the center point
  406. if (value) {
  407. if (value < 0 || value > height) {
  408. value = 0;
  409. }
  410. }
  411. if (axis.options.gridLineInterpolation === 'circle') {
  412. // A value of 0 is in the center, so it won't be
  413. // visible, but draw it anyway for update and animation
  414. // (#2366)
  415. path = axis.getLinePath(0, value, paneInnerR);
  416. // Concentric polygons
  417. }
  418. else {
  419. path = [];
  420. // Find the other axis (a circular one) in the same pane
  421. chart[inverted ? 'yAxis' : 'xAxis'].forEach(function (a) {
  422. if (a.pane === axis.pane) {
  423. otherAxis = a;
  424. }
  425. });
  426. if (otherAxis) {
  427. tickPositions = otherAxis.tickPositions;
  428. if (otherAxis.autoConnect) {
  429. tickPositions =
  430. tickPositions.concat([tickPositions[0]]);
  431. }
  432. // Reverse the positions for concatenation of polygonal
  433. // plot bands
  434. if (reverse) {
  435. tickPositions = tickPositions.slice().reverse();
  436. }
  437. if (value) {
  438. value += paneInnerR;
  439. }
  440. for (var i = 0; i < tickPositions.length; i++) {
  441. xy = otherAxis.getPosition(tickPositions[i], value);
  442. path.push(i ? ['L', xy.x, xy.y] : ['M', xy.x, xy.y]);
  443. }
  444. }
  445. }
  446. }
  447. return path;
  448. };
  449. // Find the position for the axis title, by default inside the gauge.
  450. axis.getTitlePosition = function () {
  451. var center = this.center, chart = this.chart, titleOptions = this.options.title;
  452. return {
  453. x: chart.plotLeft + center[0] + (titleOptions.x || 0),
  454. y: (chart.plotTop +
  455. center[1] -
  456. ({
  457. high: 0.5,
  458. middle: 0.25,
  459. low: 0
  460. }[titleOptions.align] *
  461. center[2]) +
  462. (titleOptions.y || 0))
  463. };
  464. };
  465. /**
  466. * Attach and return collecting function for labels in radial axis for
  467. * anti-collision.
  468. *
  469. * @private
  470. *
  471. * @return {Highcharts.ChartLabelCollectorFunction}
  472. */
  473. axis.createLabelCollector = function () {
  474. var axis = this;
  475. return function () {
  476. if (axis.isRadial &&
  477. axis.tickPositions &&
  478. // undocumented option for now, but working
  479. axis.options.labels.allowOverlap !== true) {
  480. return axis.tickPositions
  481. .map(function (pos) {
  482. return axis.ticks[pos] && axis.ticks[pos].label;
  483. })
  484. .filter(function (label) {
  485. return Boolean(label);
  486. });
  487. }
  488. };
  489. };
  490. };
  491. /**
  492. * Augments methods for the value axis.
  493. *
  494. * @private
  495. *
  496. * @param {Highcharts.Axis} AxisClass
  497. * Axis class to extend.
  498. *
  499. * @param {Highcharts.Tick} TickClass
  500. * Tick class to use.
  501. */
  502. RadialAxis.compose = function (AxisClass, TickClass) {
  503. /* eslint-disable no-invalid-this */
  504. // Actions before axis init.
  505. addEvent(AxisClass, 'init', function (e) {
  506. var axis = this;
  507. var chart = axis.chart;
  508. var inverted = chart.inverted, angular = chart.angular, polar = chart.polar, isX = axis.isXAxis, coll = axis.coll, isHidden = angular && isX, isCircular, chartOptions = chart.options, paneIndex = e.userOptions.pane || 0, pane = this.pane =
  509. chart.pane && chart.pane[paneIndex];
  510. // Prevent changes for colorAxis
  511. if (coll === 'colorAxis') {
  512. this.isRadial = false;
  513. return;
  514. }
  515. // Before prototype.init
  516. if (angular) {
  517. if (isHidden) {
  518. HiddenAxis.init(axis);
  519. }
  520. else {
  521. RadialAxis.init(axis);
  522. }
  523. isCircular = !isX;
  524. if (isCircular) {
  525. axis.defaultPolarOptions = RadialAxis.defaultRadialGaugeOptions;
  526. }
  527. }
  528. else if (polar) {
  529. RadialAxis.init(axis);
  530. // Check which axis is circular
  531. isCircular = axis.horiz;
  532. axis.defaultPolarOptions = isCircular ?
  533. RadialAxis.defaultCircularOptions :
  534. merge(coll === 'xAxis' ?
  535. AxisClass.defaultOptions :
  536. AxisClass.defaultYAxisOptions, RadialAxis.defaultRadialOptions);
  537. // Apply the stack labels for yAxis in case of inverted chart
  538. if (inverted && coll === 'yAxis') {
  539. axis.defaultPolarOptions.stackLabels = AxisClass.defaultYAxisOptions.stackLabels;
  540. }
  541. }
  542. // Disable certain features on angular and polar axes
  543. if (angular || polar) {
  544. axis.isRadial = true;
  545. chartOptions.chart.zoomType = null;
  546. if (!axis.labelCollector) {
  547. axis.labelCollector = axis.createLabelCollector();
  548. }
  549. if (axis.labelCollector) {
  550. // Prevent overlapping axis labels (#9761)
  551. chart.labelCollectors.push(axis.labelCollector);
  552. }
  553. }
  554. else {
  555. this.isRadial = false;
  556. }
  557. // A pointer back to this axis to borrow geometry
  558. if (pane && isCircular) {
  559. pane.axis = axis;
  560. }
  561. axis.isCircular = isCircular;
  562. });
  563. addEvent(AxisClass, 'afterInit', function () {
  564. var axis = this;
  565. var chart = axis.chart, options = axis.options, isHidden = chart.angular && axis.isXAxis, pane = axis.pane, paneOptions = pane && pane.options;
  566. if (!isHidden && pane && (chart.angular || chart.polar)) {
  567. // Start and end angle options are given in degrees relative to
  568. // top, while internal computations are in radians relative to
  569. // right (like SVG).
  570. // Y axis in polar charts
  571. axis.angleRad = (options.angle || 0) * Math.PI / 180;
  572. // Gauges
  573. axis.startAngleRad =
  574. (paneOptions.startAngle - 90) * Math.PI / 180;
  575. axis.endAngleRad = (pick(paneOptions.endAngle, paneOptions.startAngle + 360) - 90) * Math.PI / 180; // Gauges
  576. axis.offset = options.offset || 0;
  577. }
  578. });
  579. // Wrap auto label align to avoid setting axis-wide rotation on radial
  580. // axes. (#4920)
  581. addEvent(AxisClass, 'autoLabelAlign', function (e) {
  582. if (this.isRadial) {
  583. e.align = void 0;
  584. e.preventDefault();
  585. }
  586. });
  587. // Remove label collector function on axis remove/update
  588. addEvent(AxisClass, 'destroy', function () {
  589. var axis = this;
  590. if (axis.chart &&
  591. axis.chart.labelCollectors) {
  592. var index = (axis.labelCollector ?
  593. axis.chart.labelCollectors.indexOf(axis.labelCollector) :
  594. -1);
  595. if (index >= 0) {
  596. axis.chart.labelCollectors.splice(index, 1);
  597. }
  598. }
  599. });
  600. addEvent(AxisClass, 'initialAxisTranslation', function () {
  601. var axis = this;
  602. if (axis.isRadial) {
  603. axis.beforeSetTickPositions();
  604. }
  605. });
  606. // Add special cases within the Tick class' methods for radial axes.
  607. addEvent(TickClass, 'afterGetPosition', function (e) {
  608. var tick = this;
  609. if (tick.axis.getPosition) {
  610. extend(e.pos, tick.axis.getPosition(this.pos));
  611. }
  612. });
  613. // Find the center position of the label based on the distance option.
  614. addEvent(TickClass, 'afterGetLabelPosition', function (e) {
  615. var tick = this;
  616. var axis = tick.axis;
  617. var label = tick.label;
  618. if (!label) {
  619. return;
  620. }
  621. var labelBBox = label.getBBox(), labelOptions = axis.options.labels, optionsY = labelOptions.y, ret, centerSlot = 20, // 20 degrees to each side at the top and bottom
  622. align = labelOptions.align, angle = ((axis.translate(this.pos) + axis.startAngleRad +
  623. Math.PI / 2) / Math.PI * 180) % 360, correctAngle = Math.round(angle), labelDir = 'end', // Direction of the label 'start' or 'end'
  624. reducedAngle1 = correctAngle < 0 ?
  625. correctAngle + 360 : correctAngle, reducedAngle2 = reducedAngle1, translateY = 0, translateX = 0, labelYPosCorrection = labelOptions.y === null ? -labelBBox.height * 0.3 : 0;
  626. if (axis.isRadial) { // Both X and Y axes in a polar chart
  627. ret = axis.getPosition(this.pos, (axis.center[2] / 2) +
  628. relativeLength(pick(labelOptions.distance, -25), axis.center[2] / 2, -axis.center[2] / 2));
  629. // Automatically rotated
  630. if (labelOptions.rotation === 'auto') {
  631. label.attr({
  632. rotation: angle
  633. });
  634. // Vertically centered
  635. }
  636. else if (optionsY === null) {
  637. optionsY = (axis.chart.renderer
  638. .fontMetrics(label.styles && label.styles.fontSize).b -
  639. labelBBox.height / 2);
  640. }
  641. // Automatic alignment
  642. if (align === null) {
  643. if (axis.isCircular) { // Y axis
  644. if (labelBBox.width >
  645. axis.len * axis.tickInterval / (axis.max - axis.min)) { // #3506
  646. centerSlot = 0;
  647. }
  648. if (angle > centerSlot && angle < 180 - centerSlot) {
  649. align = 'left'; // right hemisphere
  650. }
  651. else if (angle > 180 + centerSlot &&
  652. angle < 360 - centerSlot) {
  653. align = 'right'; // left hemisphere
  654. }
  655. else {
  656. align = 'center'; // top or bottom
  657. }
  658. }
  659. else {
  660. align = 'center';
  661. }
  662. label.attr({
  663. align: align
  664. });
  665. }
  666. // Auto alignment for solid-gauges with two labels (#10635)
  667. if (align === 'auto' &&
  668. axis.tickPositions.length === 2 &&
  669. axis.isCircular) {
  670. // Angles reduced to 0 - 90 or 180 - 270
  671. if (reducedAngle1 > 90 && reducedAngle1 < 180) {
  672. reducedAngle1 = 180 - reducedAngle1;
  673. }
  674. else if (reducedAngle1 > 270 && reducedAngle1 <= 360) {
  675. reducedAngle1 = 540 - reducedAngle1;
  676. }
  677. // Angles reduced to 0 - 180
  678. if (reducedAngle2 > 180 && reducedAngle2 <= 360) {
  679. reducedAngle2 = 360 - reducedAngle2;
  680. }
  681. if ((axis.pane.options.startAngle === correctAngle) ||
  682. (axis.pane.options.startAngle === correctAngle + 360) ||
  683. (axis.pane.options.startAngle === correctAngle - 360)) {
  684. labelDir = 'start';
  685. }
  686. if ((correctAngle >= -90 && correctAngle <= 90) ||
  687. (correctAngle >= -360 && correctAngle <= -270) ||
  688. (correctAngle >= 270 && correctAngle <= 360)) {
  689. align = (labelDir === 'start') ? 'right' : 'left';
  690. }
  691. else {
  692. align = (labelDir === 'start') ? 'left' : 'right';
  693. }
  694. // For angles beetwen (90 + n * 180) +- 20
  695. if (reducedAngle2 > 70 && reducedAngle2 < 110) {
  696. align = 'center';
  697. }
  698. // auto Y translation
  699. if (reducedAngle1 < 15 ||
  700. (reducedAngle1 >= 180 && reducedAngle1 < 195)) {
  701. translateY = labelBBox.height * 0.3;
  702. }
  703. else if (reducedAngle1 >= 15 && reducedAngle1 <= 35) {
  704. translateY = labelDir === 'start' ?
  705. 0 : labelBBox.height * 0.75;
  706. }
  707. else if (reducedAngle1 >= 195 && reducedAngle1 <= 215) {
  708. translateY = labelDir === 'start' ?
  709. labelBBox.height * 0.75 : 0;
  710. }
  711. else if (reducedAngle1 > 35 && reducedAngle1 <= 90) {
  712. translateY = labelDir === 'start' ?
  713. -labelBBox.height * 0.25 : labelBBox.height;
  714. }
  715. else if (reducedAngle1 > 215 && reducedAngle1 <= 270) {
  716. translateY = labelDir === 'start' ?
  717. labelBBox.height : -labelBBox.height * 0.25;
  718. }
  719. // auto X translation
  720. if (reducedAngle2 < 15) {
  721. translateX = labelDir === 'start' ?
  722. -labelBBox.height * 0.15 : labelBBox.height * 0.15;
  723. }
  724. else if (reducedAngle2 > 165 && reducedAngle2 <= 180) {
  725. translateX = labelDir === 'start' ?
  726. labelBBox.height * 0.15 : -labelBBox.height * 0.15;
  727. }
  728. label.attr({ align: align });
  729. label.translate(translateX, translateY + labelYPosCorrection);
  730. }
  731. e.pos.x = ret.x + labelOptions.x;
  732. e.pos.y = ret.y + optionsY;
  733. }
  734. });
  735. // Wrap the getMarkPath function to return the path of the radial marker
  736. wrap(TickClass.prototype, 'getMarkPath', function (proceed, x, y, tickLength, tickWidth, horiz, renderer) {
  737. var tick = this;
  738. var axis = tick.axis;
  739. var endPoint, ret;
  740. if (axis.isRadial) {
  741. endPoint = axis.getPosition(this.pos, axis.center[2] / 2 + tickLength);
  742. ret = [
  743. 'M',
  744. x,
  745. y,
  746. 'L',
  747. endPoint.x,
  748. endPoint.y
  749. ];
  750. }
  751. else {
  752. ret = proceed.call(this, x, y, tickLength, tickWidth, horiz, renderer);
  753. }
  754. return ret;
  755. });
  756. };
  757. /* *
  758. *
  759. * Static Properties
  760. *
  761. * */
  762. /**
  763. * Circular axis around the perimeter of a polar chart.
  764. * @private
  765. */
  766. RadialAxis.defaultCircularOptions = {
  767. gridLineWidth: 1,
  768. labels: {
  769. align: null,
  770. distance: 15,
  771. x: 0,
  772. y: null,
  773. style: {
  774. textOverflow: 'none' // wrap lines by default (#7248)
  775. }
  776. },
  777. maxPadding: 0,
  778. minPadding: 0,
  779. showLastLabel: false,
  780. tickLength: 0
  781. };
  782. /**
  783. * The default options extend defaultYAxisOptions.
  784. * @private
  785. */
  786. RadialAxis.defaultRadialGaugeOptions = {
  787. labels: {
  788. align: 'center',
  789. x: 0,
  790. y: null // auto
  791. },
  792. minorGridLineWidth: 0,
  793. minorTickInterval: 'auto',
  794. minorTickLength: 10,
  795. minorTickPosition: 'inside',
  796. minorTickWidth: 1,
  797. tickLength: 10,
  798. tickPosition: 'inside',
  799. tickWidth: 2,
  800. title: {
  801. rotation: 0
  802. },
  803. zIndex: 2 // behind dials, points in the series group
  804. };
  805. /**
  806. * Radial axis, like a spoke in a polar chart.
  807. * @private
  808. */
  809. RadialAxis.defaultRadialOptions = {
  810. /**
  811. * In a polar chart, this is the angle of the Y axis in degrees, where
  812. * 0 is up and 90 is right. The angle determines the position of the
  813. * axis line and the labels, though the coordinate system is unaffected.
  814. * Since v8.0.0 this option is also applicable for X axis (inverted
  815. * polar).
  816. *
  817. * @sample {highcharts} highcharts/xaxis/angle/
  818. * Custom X axis' angle on inverted polar chart
  819. * @sample {highcharts} highcharts/yaxis/angle/
  820. * Dual axis polar chart
  821. *
  822. * @type {number}
  823. * @default 0
  824. * @since 4.2.7
  825. * @product highcharts
  826. * @apioption xAxis.angle
  827. */
  828. /**
  829. * Polar charts only. Whether the grid lines should draw as a polygon
  830. * with straight lines between categories, or as circles. Can be either
  831. * `circle` or `polygon`. Since v8.0.0 this option is also applicable
  832. * for X axis (inverted polar).
  833. *
  834. * @sample {highcharts} highcharts/demo/polar-spider/
  835. * Polygon grid lines
  836. * @sample {highcharts} highcharts/xaxis/gridlineinterpolation/
  837. * Circle and polygon on inverted polar
  838. * @sample {highcharts} highcharts/yaxis/gridlineinterpolation/
  839. * Circle and polygon
  840. *
  841. * @type {string}
  842. * @product highcharts
  843. * @validvalue ["circle", "polygon"]
  844. * @apioption xAxis.gridLineInterpolation
  845. */
  846. gridLineInterpolation: 'circle',
  847. gridLineWidth: 1,
  848. labels: {
  849. align: 'right',
  850. x: -3,
  851. y: -2
  852. },
  853. showLastLabel: false,
  854. title: {
  855. x: 4,
  856. text: null,
  857. rotation: 90
  858. }
  859. };
  860. return RadialAxis;
  861. }());
  862. RadialAxis.compose(Axis, Tick); // @todo move outside
  863. export default RadialAxis;