Skip to content

Commit

Permalink
Add ticks.integerSteps option to linear scale. When true, and
Browse files Browse the repository at this point in the history
`stepSize`, is not specified, the generated step size will be increased
to the nearest integer value.
  • Loading branch information
etimberg committed Oct 15, 2017
1 parent 8ac0257 commit ddf9a29
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/axes/cartesian/linear.md
Expand Up @@ -9,6 +9,7 @@ The following options are provided by the linear scale. They are all located in
| Name | Type | Default | Description
| -----| ---- | --------| -----------
| `beginAtZero` | `Boolean` | | if true, scale will include 0 if it is not already included.
| `integerSteps` | `Boolean` | | if true and `stepSize` is not specified, the step size will be increased to the next integer value.
| `min` | `Number` | | User defined minimum number for the scale, overrides minimum value from data. [more...](#axis-range-settings)
| `max` | `Number` | | User defined maximum number for the scale, overrides maximum value from data. [more...](#axis-range-settings)
| `maxTicksLimit` | `Number` | `11` | Maximum number of ticks and gridlines to show.
Expand Down
1 change: 1 addition & 0 deletions docs/axes/radial/linear.md
Expand Up @@ -24,6 +24,7 @@ The following options are provided by the linear scale. They are all located in
| `backdropPaddingX` | `Number` | `2` | Horizontal padding of label backdrop.
| `backdropPaddingY` | `Number` | `2` | Vertical padding of label backdrop.
| `beginAtZero` | `Boolean` | `false` | if true, scale will include 0 if it is not already included.
| `integerSteps` | `Boolean` | | if true and `stepSize` is not specified, the step size will be increased to the next integer value.
| `min` | `Number` | | User defined minimum number for the scale, overrides minimum value from data. [more...](#axis-range-settings)
| `max` | `Number` | | User defined maximum number for the scale, overrides maximum value from data. [more...](#axis-range-settings)
| `maxTicksLimit` | `Number` | `11` | Maximum number of ticks and gridlines to show.
Expand Down
11 changes: 11 additions & 0 deletions src/core/core.ticks.js
Expand Up @@ -39,6 +39,12 @@ module.exports = {
* @type Number
* @optional
*/
/**
* If true and tick spacing is auto generated, the spacing will be an integer.
* @name INumericTickGenerationOptions#integerSteps
* @type Boolean
* @optional
*/

/**
* Generate a set of linear ticks
Expand All @@ -59,6 +65,11 @@ module.exports = {
} else {
var niceRange = helpers.niceNum(dataRange.max - dataRange.min, false);
spacing = helpers.niceNum(niceRange / (generationOptions.maxTicks - 1), true);

if (generationOptions.integerSteps && spacing !== Math.round(spacing)) {
// If forcing integer steps, and our step is not an integer, scale up
spacing = Math.ceil(spacing);
}
}
var niceMin = Math.floor(dataRange.min / spacing) * spacing;
var niceMax = Math.ceil(dataRange.max / spacing) * spacing;
Expand Down
1 change: 1 addition & 0 deletions src/scales/scale.linearbase.js
Expand Up @@ -100,6 +100,7 @@ module.exports = function(Chart) {
maxTicks: maxTicks,
min: tickOpts.min,
max: tickOpts.max,
integerSteps: tickOpts.integerSteps,
stepSize: helpers.valueOrDefault(tickOpts.fixedStepSize, tickOpts.stepSize)
};
var ticks = me.ticks = Ticks.generators.linear(numericGeneratorOptions, me);
Expand Down
28 changes: 28 additions & 0 deletions test/specs/scale.linear.tests.js
Expand Up @@ -548,6 +548,34 @@ describe('Linear Scale', function() {
expect(chart.scales.yScale0.ticks).toEqual(['11', '9', '7', '5', '3', '1']);
});

it('Should create integer steps if integerSteps is true', function() {
var chart = window.acquireChart({
type: 'bar',
data: {
datasets: [{
yAxisID: 'yScale0',
data: [0, 1, 2, 1, 0, 1]
}],
labels: ['a', 'b', 'c', 'd', 'e', 'f']
},
options: {
scales: {
yAxes: [{
id: 'yScale0',
type: 'linear',
ticks: {
integerSteps: true
}
}]
}
}
});

expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
expect(chart.scales.yScale0.min).toBe(0);
expect(chart.scales.yScale0.max).toBe(2);
expect(chart.scales.yScale0.ticks).toEqual(['2', '1', '0']);
});

it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
var chart = window.acquireChart({
Expand Down

0 comments on commit ddf9a29

Please sign in to comment.