polar.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.polar = void 0;
  4. /* eslint-disable @typescript-eslint/no-unused-vars */
  5. var scale_1 = require("@antv/scale");
  6. var utils_1 = require("../utils");
  7. /**
  8. * Maps normalized value to normalized polar coordinate at the center of the bounding box.
  9. * It is used for Nightingale Rose Diagram.
  10. * @param params [x0, x1, y0, y1]
  11. * @param x x of the the bounding box of coordinate
  12. * @param y y of the the bounding box of coordinate
  13. * @param width width of the the bounding box of coordinate
  14. * @param height height of the the bounding box of coordinate
  15. * @returns transformer
  16. */
  17. var polar = function (params, x, y, width, height) {
  18. var _a = params, startAngle = _a[0], endAngle = _a[1], innerRadius = _a[2], outerRadius = _a[3];
  19. var radius = new scale_1.Linear({
  20. range: [innerRadius, outerRadius],
  21. });
  22. var angle = new scale_1.Linear({
  23. range: [startAngle, endAngle],
  24. });
  25. var aspect = height / width;
  26. var sx = aspect > 1 ? 1 : aspect;
  27. var sy = aspect > 1 ? 1 / aspect : 1;
  28. return {
  29. transform: function (vector) {
  30. var v1 = vector[0], v2 = vector[1];
  31. var theta = angle.map(v1);
  32. var r = radius.map(v2);
  33. // 根据长宽比调整,使得极坐标系内切外接矩形
  34. var x = r * Math.cos(theta) * sx;
  35. var y = r * Math.sin(theta) * sy;
  36. // 将坐标的原点移动到外接矩形的中心,并且将长度设置为一半
  37. var dx = x * 0.5 + 0.5;
  38. var dy = y * 0.5 + 0.5;
  39. return [dx, dy];
  40. },
  41. untransform: function (vector) {
  42. var dx = vector[0], dy = vector[1];
  43. var x = ((dx - 0.5) * 2) / sx;
  44. var y = ((dy - 0.5) * 2) / sy;
  45. var r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  46. var t = Math.atan2(y, x);
  47. var theta = (0, utils_1.adjustAngle)(t, startAngle, endAngle);
  48. var v1 = angle.invert(theta);
  49. var v2 = radius.invert(r);
  50. return [v1, v2];
  51. },
  52. };
  53. };
  54. exports.polar = polar;
  55. //# sourceMappingURL=polar.js.map