@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
99 lines (90 loc) • 3.33 kB
JavaScript
import $f5b828bbb980a05d$export$2e2bcd8739ae039 from "./column-components.de2f942b.js";
import {useColumn as $i30LL$useColumn} from "./column-components.0ccab336.js";
import {useRef as $i30LL$useRef, useEffect as $i30LL$useEffect} from "react";
import {select as $i30LL$select} from "d3-selection";
import {axisLeft as $i30LL$axisLeft} from "d3-axis";
const $b4e0040440f6983c$var$__d3axisKeys = [
"ticks",
"tickArguments",
"tickValues",
"tickFormat",
"tickSize",
"tickSizeInner",
"tickSizeOuter",
"tickPadding"
];
function $b4e0040440f6983c$export$59643c0aa17d0c6f(props) {
const { scale: scale } = (0, $i30LL$useColumn)();
return (0, $f5b828bbb980a05d$export$2e2bcd8739ae039)($b4e0040440f6983c$export$25ce20521d0ab97d, {
scale: scale,
...props
});
}
function $b4e0040440f6983c$export$25ce20521d0ab97d(props) {
const { showLabel: showLabel, className: className, showDomain: showDomain = true, tickSpacing: tickSpacing = 60, minTickSpacing: minTickSpacing = 20, scale: scale } = props;
const range = scale.range();
const pixelHeight = Math.abs(range[0] - range[range.length - 1]);
let tickValues = undefined;
let ticks = Math.round(pixelHeight / tickSpacing);
if (pixelHeight < 3 * tickSpacing) {
// Push ticks towards extrema (we need more than 2 to be resolved)
let t0 = [];
while(t0.length <= 2){
ticks += 1;
t0 = scale.ticks(ticks);
}
tickValues = t0;
if (pixelHeight < 2 * tickSpacing) // Only show first and last ticks
tickValues = [
t0[0],
t0[t0.length - 1]
];
}
if (pixelHeight < minTickSpacing) {
ticks = 1;
tickValues = scale.ticks(1);
// Get the last tick value only
tickValues = [
tickValues[0]
];
}
const defaultProps = {
ticks: ticks,
// Suppress domain endpoints
tickSizeOuter: 0,
tickValues: tickValues
};
const ref = (0, $i30LL$useRef)(null);
const axisRef = (0, $i30LL$useRef)((0, $i30LL$axisLeft)());
const deps = $b4e0040440f6983c$var$__d3axisKeys.map((k)=>props[k]);
(0, $i30LL$useEffect)(()=>{
const el = ref.current;
if (!el) return;
axisRef.current.scale(scale);
for (let k of $b4e0040440f6983c$var$__d3axisKeys){
const val = props[k] ?? defaultProps[k];
if (val == null) continue;
axisRef.current[k](val);
}
const ax = (0, $i30LL$select)(el).call(axisRef.current);
if (!showDomain) ax.select(".domain").remove();
ax.selectAll(".tick text").each(function(d) {
if (!(showLabel?.(d) ?? true)) (0, $i30LL$select)(this).attr("visibility", "hidden");
});
return ()=>{
(0, $i30LL$select)(el).selectAll("*").remove();
};
}, [
scale,
ref.current,
showDomain,
showLabel,
...deps
]);
return (0, $f5b828bbb980a05d$export$2e2bcd8739ae039)("g.y.axis.column-axis", {
className: className,
ref: ref
});
}
export {$b4e0040440f6983c$export$59643c0aa17d0c6f as ColumnAxis, $b4e0040440f6983c$export$25ce20521d0ab97d as AgeAxis};
//# sourceMappingURL=column-components.ad8b2d72.js.map