From: Jukka Kurkela Date: Tue, 16 Mar 2021 12:41:51 +0000 (+0200) Subject: Add alignToPixles option for scales (#8649) X-Git-Tag: v3.0.0-beta.14~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0a579b68139e528d3fc1ae2a9051002f0a98d4de;p=thirdparty%2FChart.js.git Add alignToPixles option for scales (#8649) --- diff --git a/docs/docs/axes/_common.md b/docs/docs/axes/_common.md index 057b4988f..19311b9fc 100644 --- a/docs/docs/axes/_common.md +++ b/docs/docs/axes/_common.md @@ -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) diff --git a/src/core/core.scale.js b/src/core/core.scale.js index bc82c9125..f62ec10e9 100644 --- a/src/core/core.scale.js +++ b/src/core/core.scale.js @@ -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() { @@ -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); } /** diff --git a/test/fixtures/controller.bar/aligned-pixels.js b/test/fixtures/controller.bar/aligned-pixels.js new file mode 100644 index 000000000..e0ddd37bc --- /dev/null +++ b/test/fixtures/controller.bar/aligned-pixels.js @@ -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 + } + } +}; diff --git a/test/fixtures/controller.bar/aligned-pixels.png b/test/fixtures/controller.bar/aligned-pixels.png new file mode 100644 index 000000000..d261d2f22 Binary files /dev/null and b/test/fixtures/controller.bar/aligned-pixels.png differ diff --git a/types/index.esm.d.ts b/types/index.esm.d.ts index 936c9f938..8cdaf9921 100644 --- a/types/index.esm.d.ts +++ b/types/index.esm.d.ts @@ -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