Repository navigation
feat: add symlog scale - #12386
feat: add symlog scale#12386wmunoz1018 wants to merge 2 commits into
Conversation
Adds a symmetric logarithmic (symlog) cartesian scale: values are placed at sign(x) * ln(1 + |x| / linearThreshold), so zero, negative and positive values all land on one axis, with a linearThreshold option (default 1) for the width of the near-linear band around zero. Includes the symlog tick formatter, types with a type test, a docs page with its sidebar entry, specs and image fixtures. Refs chartjs#11919
TARUNTEjjjj
left a comment
There was a problem hiding this comment.
AI-assisted review with independent browser validation on the current head. Details and scope are in the inline comment.
| return Math.log1p((a - b) / (c + b)); | ||
| } | ||
| if (a <= 0 && b <= 0) { | ||
| return -Math.log1p((b - a) / (c - b)); |
There was a problem hiding this comment.
The same-sign delta formula can still lose precision when the values are far apart: for a = 0, b = -1e16, c = 1, (b - a) / (c - b) rounds to -1, so this returns Infinity instead of the finite transformed distance. A scatter chart with data [-1e16, -1, 0, 1, 1e16] and y: {type: 'symlog', min: -1e16, max: 1e16} places both 0 and -1 at pixel -32768; zero should be at the midpoint (201.8 in my 600×400 chart). The existing large-value checks only require finite pixels, which passes because the core clamps the infinite result. Please retain the close-value calculation but use a stable alternative when this ratio approaches -1, and assert zero's midpoint/monotonicity in the extreme-range tests. I reproduced this in Chrome Headless 157: the existing 135 symlog tests pass and an added midpoint regression fails. AI-assisted review and local validation with OpenAI Codex.
Title: feat: add symlog scale
Refs #11919
What this adds
A
symlog(symmetric logarithmic) cartesian scale,type: 'symlog', for data that mixes zeros, negative and positive values across several orders of magnitude (counts with many zeros, or differences from a baseline). A valuexis placed atsign(x) * ln(1 + |x| / linearThreshold): the axis is close to linear near zero and close to logarithmic further out, with no cutoff, andxand-xare mirrored around zero.linearThreshold(default1) sets how wide the near-linear band is.It behaves like the other cartesian scales:
min,max,suggestedMin,suggestedMax,reverse,graceandboundswork the same way (percent grace is applied in the scale's log space, so it does not add orders of magnitude of empty axis below zero), it works as an x or y axis,ticks.callbackand tooltips receive the real values, and it is exported (SymLogScale) and registered like the built-in scales.Ticks are powers of ten on both sides of zero plus zero, with 2x/5x values when every decade on the axis has room, evenly spaced values inside the linear band and on narrow ranges, and short labels (
-5K,100M) that switch to plain numbers only when three significant digits cannot tell neighbouring ticks apart. Zero and the axis ends take priority over the powers of ten next to them; a power of ten next to an end is kept when its label fits.Files
src/scales/scale.symlog.js(new), exported fromsrc/scales/index.jssrc/core/core.ticks.js: asymlogformatter; the existing formatters are unchangedsrc/types/index.d.ts,src/types.ts,test/types/scales/symlog.tsdocs/axes/cartesian/symlog.mdwith its sidebar entry, plus one line each indocs/axes/cartesian/index.mdanddocs/getting-started/integration.mdtest/specs/scale.symlog.tests.jsand image fixtures intest/fixtures/scale.symlog/No existing spec or fixture is changed;
scale.linear.js,scale.linearbase.jsandscale.logarithmic.jsare untouched.Checks
On Linux, Node 22, pnpm 8.13.0:
pnpm lint: lint-js 0 errors (the 49 existing warnings), lint-md 0 errors (the 3 existing warnings), lint-types passespnpm buildpassesxvfb-run --auto-servernum pnpm test-ci-karma: Chrome 1838 of 1838; Firefox 135 of 135 forscale.symlogand 1837 of 1838 overall, the one failure beingcore.scale/label-offset-vertical-axes.json, which fails the same way on untouchedmasterwith Firefox 153 ESRpnpm test-ci-integrationpassespnpm docsbuilds, with the new page ataxes/cartesian/symlog.htmlKnown limits
ticks.includeBounds: truewith an explicit bound very close to zero (for examplemin: -0.01): zero wins and the bound tick is dropped, unlike linear.linearThreshold(1e-300) produces long decimal labels; a value above about 1e-12 is fine.Notes for reviewers
log10sketch becamelog1pwith a configurable threshold so the band around zero is adjustable and the transform is exact near zero.