Skip to content

Commit

Permalink
fix(axis): Correct on tick count display
Browse files Browse the repository at this point in the history
Make the interval of tick value to be rounded for x Axis category type.

Ref #1077
  • Loading branch information
netil authored Sep 25, 2019
1 parent e5ddb73 commit d4c8eb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/internals/axis-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe("AXIS", function() {
]
},
axis: {
x: {
tick: {
count: undefined
}
},
y: {
tick: {
values: null,
Expand All @@ -42,6 +47,25 @@ describe("AXIS", function() {
chart = util.generate(args);
});

describe("axis.x.tick.count", () => {
after(() => {
args.axis.x.type = "indexed";
args.axis.x.tick.count = undefined;
});

it("set options axis.x.tick.count=3", () => {
args.axis.x.type = "category";
args.axis.x.tick.count = 3;
});

it("should have only 3 tick on x axis", () => {
const ticks = chart.$.main.select(`.${CLASS.axisX}`).selectAll("g.tick");

expect(ticks.size()).to.be.equal(3);
expect(ticks.data()).to.be.deep.equal([0,3,5]);
});
});

describe("axis.y.tick.count", () => {
it("set options axis.y.tick.count=1", () => {
args.axis.y.tick.count = 1;
Expand Down
8 changes: 7 additions & 1 deletion src/axis/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@ export default class Axis {
} else if (targetCount === 2) {
tickValues = [values[0], values[values.length - 1]];
} else if (targetCount > 2) {
const isCategorized = this.owner.isCategorized();

count = targetCount - 2;
start = values[0];
end = values[values.length - 1];
Expand All @@ -615,7 +617,11 @@ export default class Axis {

for (i = 0; i < count; i++) {
tickValue = +start + interval * (i + 1);
tickValues.push(forTimeSeries ? new Date(tickValue) : tickValue);
tickValues.push(
forTimeSeries ? new Date(tickValue) : (
isCategorized ? Math.round(tickValue) : tickValue
)
);
}

tickValues.push(end);
Expand Down

0 comments on commit d4c8eb1

Please sign in to comment.