Skip to content

Commit

Permalink
fix(axis): Correct not to generate unnecessary ticks (#560)
Browse files Browse the repository at this point in the history
When given data is zero and tick count is greater than 1,
limit tick generation count to 1.

Fix #348
Close #560
  • Loading branch information
netil authored Aug 24, 2018
1 parent c050f0c commit 9346e66
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
25 changes: 25 additions & 0 deletions spec/internals/axis-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,4 +1153,29 @@ describe("AXIS", function() {
});
});
});

describe("when data is zero, unnecessary tick shouldn't be showing", () => {
before(() => {
args = {
data: {
columns: [
["data1", 0]
]
},
axis: {
y: {
tick: {
count: 4
}
}
}
}
});

it("only one tick should be generated even counts are greater than 1", () => {
const ticks = chart.$.main.selectAll(`.${CLASS.axisY} .tick`);

expect(ticks.size()).to.be.equal(1);
});
});
});
40 changes: 19 additions & 21 deletions src/internals/ChartInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,27 +642,25 @@ export default class ChartInternal {
xDomainForZoom = $$.x.orgDomain();
}

$$.y.domain($$.getYDomain(targetsToShow, "y", xDomainForZoom));
$$.y2.domain($$.getYDomain(targetsToShow, "y2", xDomainForZoom));

if (!config.axis_y_tick_values && config.axis_y_tick_count) {
$$.yAxis.tickValues(
$$.axis.generateTickValues(
$$.y.domain(),
config.axis_y_tick_count,
$$.isTimeSeriesY()
)
);
}

if (!config.axis_y2_tick_values && config.axis_y2_tick_count) {
$$.y2Axis.tickValues(
$$.axis.generateTickValues(
$$.y2.domain(),
config.axis_y2_tick_count
)
);
}
["y", "y2"].forEach(key => {
const axis = $$[key];
const tickValues = config[`axis_${key}_tick_values`];
const tickCount = config[`axis_${key}_tick_count`];

axis.domain($$.getYDomain(targetsToShow, key, xDomainForZoom));

if (!tickValues && tickCount) {
const domain = axis.domain();

$$[`${key}Axis`].tickValues(
$$.axis.generateTickValues(
domain,
domain.every(v => v === 0) ? 1 : tickCount,
$$.isTimeSeriesY()
)
);
}
});

// axes
$$.axis.redraw(transitions, hideAxis);
Expand Down

0 comments on commit 9346e66

Please sign in to comment.