helix.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 helix coordinate at the center of the bounding box.
  6. * @param params [x0, x1, y0, y1]
  7. * @param x x of the the bounding box of coordinate
  8. * @param y y of the the bounding box of coordinate
  9. * @param width width of the the bounding box of coordinate
  10. * @param height height of the the bounding box of coordinate
  11. * @returns transformer
  12. */
  13. export var helix = function (params, x, y, width, height) {
  14. var _a = params, startAngle = _a[0], endAngle = _a[1], innerRadius = _a[2], outerRadius = _a[3];
  15. // 计算螺旋系数:r = a + b * theta
  16. // d = 2 * PI * b
  17. // 这里不管 startAngle 从多少开始,都从 0 开始计算
  18. // 这样才能保证坐标系在 bounding box 里面
  19. var count = (endAngle - 0) / (2 * Math.PI) + 1;
  20. var d = (outerRadius - innerRadius) / count;
  21. var b = d / (Math.PI * 2);
  22. // 当 theta 为 0 的时候的极径
  23. var step = new Linear({
  24. range: [innerRadius, innerRadius + d * 0.99], // 防止和下一个螺线重合
  25. });
  26. var angle = new Linear({
  27. range: [startAngle, endAngle],
  28. });
  29. var aspect = height / width;
  30. var sx = aspect > 1 ? 1 : aspect;
  31. var sy = aspect > 1 ? 1 / aspect : 1;
  32. return {
  33. transform: function (vector) {
  34. var v1 = vector[0], v2 = vector[1];
  35. var theta = angle.map(v1);
  36. var a = step.map(v2);
  37. // 根据长宽比调整,使得极坐标系内切外接矩形
  38. var x = Math.cos(theta) * (b * theta + a) * sx;
  39. var y = Math.sin(theta) * (b * theta + a) * sy;
  40. // 将坐标的原点移动到外接矩形的中心,并且将长度设置为一半
  41. var dx = x * 0.5 + 0.5;
  42. var dy = y * 0.5 + 0.5;
  43. return [dx, dy];
  44. },
  45. untransform: function (vector) {
  46. var dx = vector[0], dy = vector[1];
  47. var x = ((dx - 0.5) * 2) / sx;
  48. var y = ((dy - 0.5) * 2) / sy;
  49. var r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  50. var t = Math.atan2(y, x) + Math.floor(r / d) * Math.PI * 2;
  51. var theta = adjustAngle(t, startAngle, endAngle);
  52. var a = r - b * theta;
  53. var v1 = angle.invert(theta);
  54. var v2 = step.invert(a);
  55. return [v1, v2];
  56. },
  57. };
  58. };
  59. //# sourceMappingURL=helix.js.map