coordinate.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { Coordinate } from '@antv/coord';
  13. import { useLibrary } from './library';
  14. export function createCoordinate(layout, partialOptions, library) {
  15. const [useCoordinate] = useLibrary('coordinate', library);
  16. const { innerHeight, innerWidth, insetLeft, insetTop, insetRight, insetBottom, marginLeft, marginTop, } = layout;
  17. const { coordinates: partialTransform = [] } = partialOptions;
  18. const transform = inferCoordinate(partialTransform);
  19. const coordinate = new Coordinate(Object.assign(Object.assign({}, layout), { x: insetLeft + marginLeft, y: insetTop + marginTop, width: innerWidth - insetLeft - insetRight, height: innerHeight - insetBottom - insetTop, transformations: transform.map(useCoordinate).flat() }));
  20. return coordinate;
  21. }
  22. export function coordinate2Transform(node, library) {
  23. // @ts-ignore
  24. const { coordinate = {} } = node, rest = __rest(node, ["coordinate"]);
  25. const { type, transform = [] } = coordinate, options = __rest(coordinate, ["type", "transform"]);
  26. if (!type)
  27. return Object.assign(Object.assign({}, rest), { coordinates: transform });
  28. const [, createCoordinate] = useLibrary('coordinate', library);
  29. const { transform: isTransform = false } = createCoordinate(type).props || {};
  30. if (isTransform) {
  31. throw new Error(`Unknown coordinate: ${type}.`);
  32. }
  33. return Object.assign(Object.assign({}, rest), { coordinates: [Object.assign({ type }, options), ...transform] });
  34. }
  35. export function coordOf(coordinates, type) {
  36. return coordinates.filter((d) => d.type === type);
  37. }
  38. /**
  39. * todo Duplication is not considered
  40. */
  41. export function isPolar(coordinates) {
  42. return coordOf(coordinates, 'polar').length > 0;
  43. }
  44. export function isHelix(coordinates) {
  45. return coordOf(coordinates, 'helix').length > 0;
  46. }
  47. /**
  48. * todo The number of transposes matters
  49. */
  50. export function isTranspose(coordinates) {
  51. return coordOf(coordinates, 'transpose').length % 2 === 1;
  52. }
  53. export function isParallel(coordinates) {
  54. return coordOf(coordinates, 'parallel').length > 0;
  55. }
  56. export function isTheta(coordinates) {
  57. return coordOf(coordinates, 'theta').length > 0;
  58. }
  59. export function isReflect(coordinates) {
  60. return coordOf(coordinates, 'reflect').length > 0;
  61. }
  62. export function isRadial(coordinates) {
  63. return coordOf(coordinates, 'radial').length > 0;
  64. }
  65. export function isRadar(coordinates) {
  66. return coordOf(coordinates, 'radar').length > 0;
  67. }
  68. /**
  69. * todo The axis corresponding to the Y reversal is not reversed
  70. */
  71. export function isReflectY(coordinates) {
  72. return coordOf(coordinates, 'reflectY').length > 0;
  73. }
  74. function inferCoordinate(coordinates) {
  75. if (coordinates.find((d) => d.type === 'cartesian'))
  76. return coordinates;
  77. return [...coordinates, { type: 'cartesian' }];
  78. }
  79. //# sourceMappingURL=coordinate.js.map