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
8 changes: 8 additions & 0 deletions src/scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function autoScaleRangeX(scale, dimensions) {
const {width, marginLeft = 0, marginRight = 0} = dimensions;
scale.scale.range([marginLeft + inset, width - marginRight - inset]);
}
autoScaleRound(scale);
}

function autoScaleRangeY(scale, dimensions) {
Expand All @@ -45,6 +46,13 @@ function autoScaleRangeY(scale, dimensions) {
if (scale.type === "ordinal") range.reverse();
scale.scale.range(range);
}
autoScaleRound(scale);
}

function autoScaleRound(scale) {
if (scale.round === undefined && scale.type === "ordinal" && scale.scale.step() >= 5) {
scale.scale.round(true);
}
}

function Scale(key, channels = [], options = {}) {
Expand Down
20 changes: 12 additions & 8 deletions src/scales/ordinal.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,12 @@ export function ScaleOrdinal(key, channels, {
export function ScalePoint(key, channels, {
align = 0.5,
padding = 0.5,
round = true,
...options
}) {
return ScaleO(
return maybeRound(
scalePoint()
.align(align)
.padding(padding)
.round(round),
.padding(padding),
channels,
options
);
Expand All @@ -227,20 +225,26 @@ export function ScaleBand(key, channels, {
padding = 0.1,
paddingInner = padding,
paddingOuter = key === "fx" || key === "fy" ? 0 : padding,
round = true,
...options
}) {
return ScaleO(
return maybeRound(
scaleBand()
.align(align)
.paddingInner(paddingInner)
.paddingOuter(paddingOuter)
.round(round),
.paddingOuter(paddingOuter),
channels,
options
);
}

function maybeRound(scale, channels, options = {}) {
const {round} = options;
if (round !== undefined) scale.round(round);
scale = ScaleO(scale, channels, options);
scale.round = round;
return scale;
}

function inferDomain(channels) {
const domain = new Set();
for (const {value} of channels) {
Expand Down