area.js 925 B

12345678910111213141516171819202122232425262728
  1. import { maxIndex } from 'd3-array';
  2. import { sub, angle } from '../../../utils/vector';
  3. /**
  4. * Only for Area label.
  5. */
  6. export function area(position, points, value, coordinate) {
  7. const l = points.length / 2;
  8. const Y1 = points.slice(0, l);
  9. const Y0 = points.slice(l);
  10. // Get the maximal space for label.
  11. let idx = maxIndex(Y1, (p, i) => Math.abs(p[1] - Y0[i][1]));
  12. // Do not show label at first and last.
  13. idx = Math.max(Math.min(idx, l - 2), 1);
  14. const mid = (i) => [Y1[i][0], (Y1[i][1] + Y0[i][1]) / 2];
  15. const point = mid(idx);
  16. const prev = mid(idx - 1);
  17. const next = mid(idx + 1);
  18. // todo: G rotate only support deg.
  19. const rotate = (angle(sub(next, prev)) / Math.PI) * 180;
  20. return {
  21. x: point[0],
  22. y: point[1],
  23. transform: `rotate(${rotate})`,
  24. textAlign: 'center',
  25. textBaseline: 'middle',
  26. };
  27. }
  28. //# sourceMappingURL=area.js.map