UNPKG

@macrostrat/column-components

Version:

React rendering primitives for stratigraphic columns

87 lines (86 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const React = require("react"); const hyper = require("./hyper.cjs"); const d3Selection = require("d3-selection"); const d3Axis = require("d3-axis"); const column = require("./context/column.cjs"); const __d3axisKeys = [ "ticks", "tickArguments", "tickValues", "tickFormat", "tickSize", "tickSizeInner", "tickSizeOuter", "tickPadding" ]; function ColumnAxis(props) { const { scale } = column.useColumn(); return hyper(AgeAxis, { scale, ...props }); } function AgeAxis(props) { const { showLabel, className, showDomain = true, tickSpacing = 60, minTickSpacing = 20, scale } = props; const range = scale.range(); const pixelHeight = Math.abs(range[0] - range[range.length - 1]); let tickValues = void 0; let ticks = Math.round(pixelHeight / tickSpacing); if (pixelHeight < 3 * tickSpacing) { let t0 = []; while (t0.length <= 2) { ticks += 1; t0 = scale.ticks(ticks); } tickValues = t0; if (pixelHeight < 2 * tickSpacing) { tickValues = [t0[0], t0[t0.length - 1]]; } } if (pixelHeight < minTickSpacing) { ticks = 1; tickValues = scale.ticks(1); tickValues = [tickValues[0]]; } const defaultProps = { ticks, // Suppress domain endpoints tickSizeOuter: 0, tickValues }; const ref = React.useRef(null); const axisRef = React.useRef(d3Axis.axisLeft()); const deps = __d3axisKeys.map((k) => props[k]); React.useEffect(() => { const el = ref.current; if (!el) return; axisRef.current.scale(scale); for (let k of __d3axisKeys) { const val = props[k] ?? defaultProps[k]; if (val == null) continue; axisRef.current[k](val); } const ax = d3Selection.select(el).call(axisRef.current); if (!showDomain) { ax.select(".domain").remove(); } ax.selectAll(".tick text").each(function(d) { if (!(showLabel?.(d) ?? true)) { d3Selection.select(this).attr("visibility", "hidden"); } }); return () => { d3Selection.select(el).selectAll("*").remove(); }; }, [scale, ref.current, showDomain, showLabel, ...deps]); return hyper("g.y.axis.column-axis", { className, ref }); } exports.AgeAxis = AgeAxis; exports.ColumnAxis = ColumnAxis; //# sourceMappingURL=axis.cjs.map