Skip to content
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
20 changes: 14 additions & 6 deletions src/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export class AxisX {
} = {}) {
this.name = name;
this.axis = keyword(axis, "axis", ["top", "bottom"]);
this.ticks = ticks;
this.ticks = maybeTicks(ticks);
this.tickSize = number(tickSize);
this.tickPadding = number(tickPadding);
this.tickFormat = tickFormat;
this.tickFormat = maybeTickFormat(tickFormat);
this.fontVariant = impliedString(fontVariant, "normal");
this.grid = boolean(grid);
this.label = string(label);
Expand Down Expand Up @@ -117,10 +117,10 @@ export class AxisY {
} = {}) {
this.name = name;
this.axis = keyword(axis, "axis", ["left", "right"]);
this.ticks = ticks;
this.ticks = maybeTicks(ticks);
this.tickSize = number(tickSize);
this.tickPadding = number(tickPadding);
this.tickFormat = tickFormat;
this.tickFormat = maybeTickFormat(tickFormat);
this.fontVariant = impliedString(fontVariant, "normal");
this.grid = boolean(grid);
this.label = string(label);
Expand Down Expand Up @@ -234,10 +234,18 @@ function gridFacetY(index, fx, tx) {
.attr("d", (index ? take(domain, index) : domain).map(v => `M${fx(v) + tx},0h${dx}`).join(""));
}

function maybeTicks(ticks) {
return ticks === null ? [] : ticks;
}

function maybeTickFormat(tickFormat) {
return tickFormat === null ? () => null : tickFormat;
}

// D3 doesn’t provide a tick format for ordinal scales; we want shorthand when
// an ordinal domain is numbers or dates, and we want null to mean the empty
// string, not the default identity format.
export function maybeTickFormat(tickFormat, domain) {
export function maybeAutoTickFormat(tickFormat, domain) {
return tickFormat === undefined ? (isTemporal(domain) ? formatIsoDate : string)
: typeof tickFormat === "function" ? tickFormat
: (typeof tickFormat === "string" ? (isTemporal(domain) ? utcFormat : format)
Expand All @@ -246,7 +254,7 @@ export function maybeTickFormat(tickFormat, domain) {

function createAxis(axis, scale, {ticks, tickSize, tickPadding, tickFormat}) {
if (!scale.tickFormat) {
tickFormat = maybeTickFormat(tickFormat, scale.domain());
tickFormat = maybeAutoTickFormat(tickFormat, scale.domain());
}
return axis(scale)
.ticks(Array.isArray(ticks) ? null : ticks, typeof tickFormat === "function" ? null : tickFormat)
Expand Down
4 changes: 2 additions & 2 deletions src/legends/swatches.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {create, path} from "d3";
import {inferFontVariant} from "../axes.js";
import {maybeTickFormat} from "../axis.js";
import {maybeAutoTickFormat} from "../axis.js";
import {isNoneish, maybeColorChannel, maybeNumberChannel} from "../options.js";
import {applyInlineStyles, impliedString, maybeClassName} from "../style.js";

Expand Down Expand Up @@ -84,7 +84,7 @@ function legendItems(scale, {
width
} = {}, swatch, swatchStyle) {
className = maybeClassName(className);
tickFormat = maybeTickFormat(tickFormat, scale.domain);
tickFormat = maybeAutoTickFormat(tickFormat, scale.domain);

const swatches = create("div")
.attr("class", className)
Expand Down