@thi.ng/viz
Version: 
Declarative, functional & multi-format data visualization toolkit based around @thi.ng/hiccup
30 lines (29 loc) • 745 B
JavaScript
import { lens, mix } from "@thi.ng/math/mix";
import { safeDiv } from "@thi.ng/math/safe-div";
import { mergeDeepObj } from "@thi.ng/object-utils/merge-deep";
import { axisDefaults } from "./common.js";
const lensScale = ([d1, d2], [r1, r2], focus = (d1 + d2) / 2, strength) => {
  const dr = d2 - d1;
  const f = safeDiv(focus - d1, dr);
  return (x) => mix(r1, r2, lens(f, strength, safeDiv(x - d1, dr)));
};
const lensAxis = (src) => {
  const spec = mergeDeepObj(
    axisDefaults({
      focus: (src.domain[0] + src.domain[1]) / 2,
      strength: 1
    }),
    src
  );
  !spec.scale && (spec.scale = lensScale(
    spec.domain,
    spec.range,
    spec.focus,
    spec.strength
  ));
  return spec;
};
export {
  lensAxis,
  lensScale
};