polar.js 2.0 KB

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