point.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { baseGeometryChannels, basePostInference, basePreInference, createBandOffset, tooltip2d, } from './utils';
  2. /**
  3. * Convert value for each channel to point shapes.
  4. * Calc the bbox of each point based on x, y and r.
  5. * This is for allowing their radius can be affected by coordinate(e.g. fisheye).
  6. */
  7. export const Point = (options) => {
  8. return (index, scale, value, coordinate) => {
  9. const { x: X, y: Y, x1: X1, y1: Y1, size: S, dx: DX, dy: DY } = value;
  10. const [width, height] = coordinate.getSize();
  11. const offset = createBandOffset(scale, value, options);
  12. const xy = (i) => {
  13. const dx = +((DX === null || DX === void 0 ? void 0 : DX[i]) || 0);
  14. const dy = +((DY === null || DY === void 0 ? void 0 : DY[i]) || 0);
  15. const x = X1 ? (+X[i] + +X1[i]) / 2 : +X[i];
  16. const y = Y1 ? (+Y[i] + +Y1[i]) / 2 : +Y[i];
  17. const cx = x + dx;
  18. const cy = y + dy;
  19. return [cx, cy];
  20. };
  21. const P = S
  22. ? Array.from(index, (i) => {
  23. const [cx, cy] = xy(i);
  24. const r = +S[i];
  25. const a = r / width;
  26. const b = r / height;
  27. const p1 = [cx - a, cy - b];
  28. const p2 = [cx + a, cy + b];
  29. return [
  30. coordinate.map(offset(p1, i)),
  31. coordinate.map(offset(p2, i)),
  32. ];
  33. })
  34. : Array.from(index, (i) => [coordinate.map(offset(xy(i), i))]);
  35. return [index, P];
  36. };
  37. };
  38. const shapes = [
  39. 'hollow',
  40. 'hollowDiamond',
  41. 'hollowHexagon',
  42. 'hollowSquare',
  43. 'hollowTriangleDown',
  44. 'hollowTriangle',
  45. 'hollowBowtie',
  46. 'point',
  47. 'plus',
  48. 'diamond',
  49. 'square',
  50. 'triangle',
  51. 'hexagon',
  52. 'cross',
  53. 'bowtie',
  54. 'hyphen',
  55. 'line',
  56. 'tick',
  57. 'triangleDown',
  58. ];
  59. Point.props = {
  60. defaultShape: 'hollow',
  61. defaultLabelShape: 'label',
  62. composite: false,
  63. channels: [
  64. ...baseGeometryChannels({ shapes }),
  65. { name: 'x', required: true },
  66. { name: 'y', required: true },
  67. { name: 'series', scale: 'band' },
  68. { name: 'size', scale: 'sqrt' },
  69. { name: 'dx', scale: 'identity' },
  70. { name: 'dy', scale: 'identity' },
  71. ],
  72. preInference: [
  73. ...basePreInference(),
  74. { type: 'maybeZeroY' },
  75. { type: 'maybeZeroX' },
  76. ],
  77. postInference: [
  78. ...basePostInference(),
  79. { type: 'maybeSize' },
  80. ...tooltip2d(),
  81. ],
  82. };
  83. //# sourceMappingURL=point.js.map