Skip to content

Commit d84db2c

Browse files
authored
Radial scale point label backdrop color (#8633)
* Radial scale point label backdrop color * Update default tests * backdropPadding is a single setting * Up the tolerance a bit * Update tick backdrop padding options
1 parent 96f6b42 commit d84db2c

File tree

7 files changed

+94
-24
lines changed

7 files changed

+94
-24
lines changed

docs/docs/axes/radial/linear.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ Namespace: `options.scales[scaleId].ticks`
3434
| Name | Type | Scriptable | Default | Description
3535
| ---- | ---- | ------- | ------- | -----------
3636
| `backdropColor` | [`Color`](../../general/colors.md) | Yes | `'rgba(255, 255, 255, 0.75)'` | Color of label backdrops.
37-
| `backdropPaddingX` | `number` | | `2` | Horizontal padding of label backdrop.
38-
| `backdropPaddingY` | `number` | | `2` | Vertical padding of label backdrop.
37+
| `backdropPadding` | `number`\|`{top: number, bottom: number}` | | `2` | Padding of label backdrop.
3938
| `format` | `object` | | | The [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) options used by the default label formatter
4039
| `maxTicksLimit` | `number` | | `11` | Maximum number of ticks and gridlines to show.
4140
| `precision` | `number` | | | if defined and `stepSize` is not specified, the step size will be rounded to this many decimal places.
@@ -126,6 +125,8 @@ Namespace: `options.scales[scaleId].pointLabels`
126125

127126
| Name | Type | Scriptable | Default | Description
128127
| ---- | ---- | ------- | ------- | -----------
128+
| `backdropColor` | [`Color`](../../general/colors.md) | `true` | `undefined` | Background color of the point label.
129+
| `backdropPadding` | `number`\|`{top: number, bottom: number}` | | `2` | Padding of label backdrop.
129130
| `display` | `boolean` | | `true` | if true, point labels are shown.
130131
| `callback` | `function` | | | Callback function to transform data labels to point labels. The default implementation simply returns the current string.
131132
| `color` | [`Color`](../../general/colors.md) | Yes | `Chart.defaults.color` | Color of label.

docs/docs/getting-started/v3-migration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ Animation system was completely rewritten in Chart.js v3. Each property can now
235235
* `options.ticks.fixedStepSize` is no longer used. Use `options.ticks.stepSize`.
236236
* `options.ticks.major` and `options.ticks.minor` were replaced with scriptable options for tick fonts.
237237
* `Chart.Ticks.formatters.linear` was renamed to `Chart.Ticks.formatters.numeric`.
238+
* `options.ticks.backdropPaddingX` and `options.ticks.backdropPaddingY` were replaced with `options.ticks.backdropPadding` in the radial linear scale.
238239

239240
#### Tooltip
240241

src/scales/scale.radialLinear.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import {HALF_PI, isNumber, TAU, toDegrees, toRadians, _normalizeAngle} from '../
44
import LinearScaleBase from './scale.linearbase';
55
import Ticks from '../core/core.ticks';
66
import {valueOrDefault, isArray, isFinite, callback as callCallback, isNullOrUndef} from '../helpers/helpers.core';
7-
import {toFont} from '../helpers/helpers.options';
7+
import {toFont, toPadding} from '../helpers/helpers.options';
88

99
function getTickBackdropHeight(opts) {
1010
const tickOpts = opts.ticks;
1111

1212
if (tickOpts.display && opts.display) {
13-
return valueOrDefault(tickOpts.font && tickOpts.font.size, defaults.font.size) + tickOpts.backdropPaddingY * 2;
13+
const padding = toPadding(tickOpts.backdropPadding);
14+
return valueOrDefault(tickOpts.font && tickOpts.font.size, defaults.font.size) + padding.height;
1415
}
1516
return 0;
1617
}
@@ -202,7 +203,15 @@ function drawPointLabels(scale, labelCount) {
202203
for (let i = labelCount - 1; i >= 0; i--) {
203204
const optsAtIndex = pointLabels.setContext(scale.getContext(i));
204205
const plFont = toFont(optsAtIndex.font);
205-
const {x, y, textAlign} = scale._pointLabelItems[i];
206+
const {x, y, textAlign, left, top, right, bottom} = scale._pointLabelItems[i];
207+
const {backdropColor} = optsAtIndex;
208+
209+
if (!isNullOrUndef(backdropColor)) {
210+
const padding = toPadding(optsAtIndex.backdropPadding);
211+
ctx.fillStyle = backdropColor;
212+
ctx.fillRect(left - padding.left, top - padding.top, right - left + padding.width, bottom - top + padding.height);
213+
}
214+
206215
renderText(
207216
ctx,
208217
scale._pointLabels[i],
@@ -532,12 +541,12 @@ export default class RadialLinearScale extends LinearScaleBase {
532541
width = ctx.measureText(tick.label).width;
533542
ctx.fillStyle = optsAtIndex.backdropColor;
534543

535-
const {backdropPaddingX, backdropPaddingY} = optsAtIndex;
544+
const padding = toPadding(optsAtIndex.backdropPadding);
536545
ctx.fillRect(
537-
-width / 2 - backdropPaddingX,
538-
-offset - tickFont.size / 2 - backdropPaddingY,
539-
width + backdropPaddingX * 2,
540-
tickFont.size + backdropPaddingY * 2
546+
-width / 2 - padding.left,
547+
-offset - tickFont.size / 2 - padding.top,
548+
width + padding.width,
549+
tickFont.size + padding.height
541550
);
542551
}
543552

@@ -588,16 +597,18 @@ RadialLinearScale.defaults = {
588597
// String - The colour of the label backdrop
589598
backdropColor: 'rgba(255,255,255,0.75)',
590599

591-
// Number - The backdrop padding above & below the label in pixels
592-
backdropPaddingY: 2,
593-
594-
// Number - The backdrop padding to the side of the label in pixels
595-
backdropPaddingX: 2,
600+
// Number/Object - The backdrop padding of the label in pixels
601+
backdropPadding: 2,
596602

597603
callback: Ticks.formatters.numeric
598604
},
599605

600606
pointLabels: {
607+
backdropColor: undefined,
608+
609+
// Number - The backdrop padding above & below the label in pixels
610+
backdropPadding: 2,
611+
601612
// Boolean - if true, show point labels
602613
display: true,
603614

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module.exports = {
2+
tolerance: 0.01,
3+
config: {
4+
type: 'radar',
5+
data: {
6+
labels: [
7+
['VENTE ET', 'COMMERCIALISATION'],
8+
['GESTION', 'FINANCIÈRE'],
9+
'NUMÉRIQUE',
10+
['ADMINISTRATION', 'ET OPÉRATION'],
11+
['RESSOURCES', 'HUMAINES'],
12+
'INNOVATION'
13+
],
14+
datasets: [
15+
{
16+
backgroundColor: '#E43E51',
17+
label: 'Compétences entrepreunariales',
18+
data: [3, 2, 2, 1, 3, 1]
19+
}
20+
]
21+
},
22+
options: {
23+
plugins: {
24+
legend: false,
25+
tooltip: false,
26+
filler: false
27+
},
28+
scales: {
29+
r: {
30+
min: 0,
31+
max: 3,
32+
pointLabels: {
33+
backdropColor: 'blue',
34+
backdropPadding: {left: 5, right: 5, top: 2, bottom: 2},
35+
},
36+
ticks: {
37+
display: false,
38+
stepSize: 1,
39+
maxTicksLimit: 1
40+
}
41+
}
42+
},
43+
responsive: true,
44+
maintainAspectRatio: false
45+
}
46+
},
47+
options: {
48+
spriteText: true
49+
}
50+
};
27.7 KB
Loading

test/specs/scale.radialLinear.tests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ describe('Test the radial linear scale', function() {
3737
color: Chart.defaults.color,
3838
showLabelBackdrop: true,
3939
backdropColor: 'rgba(255,255,255,0.75)',
40-
backdropPaddingY: 2,
41-
backdropPaddingX: 2,
40+
backdropPadding: 2,
4241
callback: defaultConfig.ticks.callback
4342
},
4443

4544
pointLabels: {
45+
backdropColor: undefined,
46+
backdropPadding: 2,
4647
color: Chart.defaults.color,
4748
display: true,
4849
font: {

types/index.esm.d.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,17 @@ export type RadialLinearScaleOptions = CoreScaleOptions & {
30053005
max: number;
30063006

30073007
pointLabels: {
3008+
/**
3009+
* Background color of the point label.
3010+
* @default undefined
3011+
*/
3012+
backdropColor: Scriptable<Color, ScriptableScaleContext>;
3013+
/**
3014+
* Padding of label backdrop.
3015+
* @default 2
3016+
*/
3017+
backdropPadding: Scriptable<number | ChartArea, ScriptableScaleContext>;
3018+
30083019
/**
30093020
* if true, point labels are shown.
30103021
* @default true
@@ -3043,15 +3054,10 @@ export type RadialLinearScaleOptions = CoreScaleOptions & {
30433054
*/
30443055
backdropColor: Scriptable<Color, ScriptableScaleContext>;
30453056
/**
3046-
* Horizontal padding of label backdrop.
3047-
* @default 2
3048-
*/
3049-
backdropPaddingX: number;
3050-
/**
3051-
* Vertical padding of label backdrop.
3057+
* Padding of label backdrop.
30523058
* @default 2
30533059
*/
3054-
backdropPaddingY: number;
3060+
backdropPadding: number | ChartArea;
30553061

30563062
/**
30573063
* The Intl.NumberFormat options used by the default label formatter

0 commit comments

Comments
 (0)