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

Add alignToPixles option for scales #8649

Merged
merged 1 commit into from
Mar 16, 2021
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
1 change: 1 addition & 0 deletions docs/docs/axes/_common.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Namespace: `options.scales[scaleId]`
| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| `type` | `string` | | Type of scale being employed. Custom scales can be created and registered with a string key. This allows changing the type of an axis for a chart.
| `alignToPixels` | `boolean` | `false` | Align pixel values to device pixels.
| `backgroundColor` | [`Color`](../general/colors.md) | | Background color of the scale area.
| `display` | `boolean`\|`string` | `true` | Controls the axis global visibility (visible when `true`, hidden when `false`). When `display: 'auto'`, the axis is visible only if at least one associated dataset is visible.
| `grid` | `object` | | Grid line configuration. [more...](./styling.mdx#grid-line-configuration)
Expand Down
4 changes: 3 additions & 1 deletion src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ export default class Scale extends Element {
me._endPixel = endPixel;
me._reversePixels = reversePixels;
me._length = endPixel - startPixel;
me._alignToPixels = me.options.alignToPixels;
}

afterUpdate() {
Expand Down Expand Up @@ -1122,7 +1123,8 @@ export default class Scale extends Element {
decimal = 1 - decimal;
}

return _int16Range(me._startPixel + decimal * me._length);
const pixel = me._startPixel + decimal * me._length;
return _int16Range(me._alignToPixels ? _alignPixel(me.chart, pixel, 0) : pixel);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/controller.bar/aligned-pixels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a'],
datasets: [{
data: [-1]
}, {
data: [1]
}]
},
options: {
indexAxis: 'y',
events: [],
backgroundColor: 'navy',
devicePixelRatio: 1.25,
scales: {
x: {display: false, alignToPixels: true},
y: {display: false, stacked: true}
}
}
},
options: {
canvas: {
width: 100,
height: 500
}
}
};
Binary file added test/fixtures/controller.bar/aligned-pixels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions types/index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,10 @@ export interface CoreScaleOptions {
* @default true
*/
display: boolean | 'auto';
/**
* Align pixel values to device pixels
*/
alignToPixels: boolean;
/**
* Reverse the scale.
* @default false
Expand Down