select.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { maxIndex, minIndex } from 'd3-array';
  2. import { columnOf } from './utils/helper';
  3. import { createGroups } from './utils/order';
  4. function first(I, V) {
  5. return [I[0]];
  6. }
  7. function last(I, V) {
  8. const i = I.length - 1;
  9. return [I[i]];
  10. }
  11. function max(I, V) {
  12. const i = maxIndex(I, (i) => V[i]);
  13. return [I[i]];
  14. }
  15. function min(I, V) {
  16. const i = minIndex(I, (i) => V[i]);
  17. return [I[i]];
  18. }
  19. function normalizeSelector(selector) {
  20. if (typeof selector === 'function')
  21. return selector;
  22. const registry = { first, last, max, min };
  23. return registry[selector] || first;
  24. }
  25. /**
  26. * The select transform groups marks with specified channels, and
  27. * filter index by specified selector for each series, say to
  28. * pull a single or multiple values out of each series.
  29. */
  30. export const Select = (options = {}) => {
  31. const { groupBy = 'series', channel, selector } = options;
  32. return (I, mark) => {
  33. const { encode } = mark;
  34. const groups = createGroups(groupBy, I, mark);
  35. const [V] = columnOf(encode, channel);
  36. const selectFunction = normalizeSelector(selector);
  37. return [groups.flatMap((GI) => selectFunction(GI, V)), mark];
  38. };
  39. };
  40. Select.props = {};
  41. //# sourceMappingURL=select.js.map