Skip to content
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

Fix range calculation when all datasets hidden and axis minimum set #4425

Merged
merged 1 commit into from Jun 25, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/scales/scale.linear.js
Expand Up @@ -124,8 +124,8 @@ module.exports = function(Chart) {
});
}

me.min = isFinite(me.min) ? me.min : DEFAULT_MIN;
me.max = isFinite(me.max) ? me.max : DEFAULT_MAX;
me.min = isFinite(me.min) && !isNaN(me.min) ? me.min : DEFAULT_MIN;
me.max = isFinite(me.max) && !isNaN(me.max) ? me.max : DEFAULT_MAX;

// Common base implementation to handle ticks.min, ticks.max, ticks.beginAtZero
this.handleTickRangeOptions();
Expand Down
17 changes: 17 additions & 0 deletions src/scales/scale.linearbase.js
Expand Up @@ -27,6 +27,9 @@ module.exports = function(Chart) {
}
}

var setMin = tickOpts.min !== undefined || tickOpts.suggestedMin !== undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also handle min/suggestedMin === null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I'm not sure. The only way that could happen is if a user explicitly sets it (the default is undefined)

var setMax = tickOpts.max !== undefined || tickOpts.suggestedMax !== undefined;

if (tickOpts.min !== undefined) {
me.min = tickOpts.min;
} else if (tickOpts.suggestedMin !== undefined) {
Expand All @@ -47,6 +50,20 @@ module.exports = function(Chart) {
}
}

if (setMin !== setMax) {
// We set the min or the max but not both.
// So ensure that our range is good
// Inverted or 0 length range can happen when
// ticks.min is set, and no datasets are visible
if (me.min >= me.max) {
if (setMin) {
me.max = me.min + 1;
} else {
me.min = me.max - 1;
}
}
}

if (me.min === me.max) {
me.max++;

Expand Down
29 changes: 29 additions & 0 deletions test/specs/scale.linear.tests.js
Expand Up @@ -877,4 +877,33 @@ describe('Linear Scale', function() {
expect(chart.scales['x-axis-0'].min).toEqual(0);
expect(chart.scales['x-axis-0'].max).toEqual(1);
});

it('max and min value should be valid when min is set and all datasets are hidden', function() {
var barData = {
labels: ['S1', 'S2', 'S3'],
datasets: [{
label: 'dataset 1',
backgroundColor: '#382765',
data: [2500, 2000, 1500],
hidden: true,
}]
};

var chart = window.acquireChart({
type: 'horizontalBar',
data: barData,
options: {
scales: {
xAxes: [{
ticks: {
min: 20
}
}]
}
}
});

expect(chart.scales['x-axis-0'].min).toEqual(20);
expect(chart.scales['x-axis-0'].max).toEqual(21);
});
});