Skip to content

fix #278; robust log domain #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/defined.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export function positive(x) {
return x > 0 ? x : NaN;
}

export function negative(x) {
return x < 0 ? x : NaN;
}

export function firstof(...values) {
for (const v of values) {
if (v !== undefined) {
Expand Down
25 changes: 19 additions & 6 deletions src/scales/quantitative.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
} from "d3";
import {scaleDiverging, scaleLinear, scaleLog, scalePow, scaleSymlog} from "d3";
import {registry, radius, color} from "./index.js";
import {positive} from "../defined.js";
import {positive, negative} from "../defined.js";

const constant = x => () => x;
const flip = i => t => i(1 - t);
Expand Down Expand Up @@ -179,8 +179,8 @@ export function ScalePow(key, channels, {exponent = 1, ...options}) {
return ScaleQ(key, scalePow().exponent(exponent), channels, options);
}

export function ScaleLog(key, channels, {base = 10, ...options}) {
return ScaleQ(key, scaleLog().base(base), channels, options);
export function ScaleLog(key, channels, {base = 10, domain = inferLogDomain(channels), ...options}) {
return ScaleQ(key, scaleLog().base(base), channels, {domain, ...options});
}

export function ScaleSymlog(key, channels, {constant = 1, ...options}) {
Expand Down Expand Up @@ -215,10 +215,10 @@ export function ScaleDiverging(key, channels, {
return {type: "quantitative", invert, domain, scale};
}

function inferDomain(channels) {
function inferDomain(channels, f) {
return [
min(channels, ({value}) => value === undefined ? value : min(value)),
max(channels, ({value}) => value === undefined ? value : max(value))
min(channels, ({value}) => value === undefined ? value : min(value, f)),
max(channels, ({value}) => value === undefined ? value : max(value, f))
];
}

Expand All @@ -232,3 +232,16 @@ function inferRadialRange(channels, domain) {
const h25 = quantile(channels, 0.5, ({value}) => value === undefined ? NaN : quantile(value, 0.25, positive));
return domain.map(d => 3 * Math.sqrt(d / h25));
}

function inferLogDomain(channels) {
for (const {value} of channels) {
if (value !== undefined) {
for (let v of value) {
v = +v;
if (v > 0) return inferDomain(channels, positive);
if (v < 0) return inferDomain(channels, negative);
}
}
}
return [1, 10];
}
68 changes: 68 additions & 0 deletions test/output/logDegenerate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/plots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export {default as letterFrequencyBar} from "./letter-frequency-bar.js";
export {default as letterFrequencyColumn} from "./letter-frequency-column.js";
export {default as letterFrequencyDot} from "./letter-frequency-dot.js";
export {default as letterFrequencyLollipop} from "./letter-frequency-lollipop.js";
export {default as logDegenerate} from "./log-degenerate.js";
export {default as metroInequality} from "./metro-inequality.js";
export {default as metroInequalityChange} from "./metro-inequality-change.js";
export {default as metroUnemployment} from "./metro-unemployment.js";
Expand Down
12 changes: 12 additions & 0 deletions test/plots/log-degenerate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Plot from "@observablehq/plot";

export default async function() {
return Plot.plot({
x: {
type: "log"
},
marks: [
Plot.dotX([0, 0.1, 1, 2, 10])
]
});
}