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

radialLinear: Hide pointLabels of hidden data #10018

Merged
merged 2 commits into from Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 12 additions & 10 deletions src/scales/scale.radialLinear.js
Expand Up @@ -86,7 +86,7 @@ function fitWithPointLabels(scale) {
const labelSizes = [];
const padding = [];

const valueCount = scale.getLabels().length;
const valueCount = scale._pointLabels.length;
for (let i = 0; i < valueCount; i++) {
const opts = scale.options.pointLabels.setContext(scale.getPointLabelContext(i));
padding[i] = opts.padding;
Expand Down Expand Up @@ -129,7 +129,7 @@ function fitWithPointLabels(scale) {

function buildPointLabelItems(scale, labelSizes, padding) {
const items = [];
const valueCount = scale.getLabels().length;
const valueCount = scale._pointLabels.length;
const opts = scale.options;
const tickBackdropHeight = getTickBackdropHeight(opts);
const outerDistance = scale.getDistanceFromCenterForValue(opts.ticks.reverse ? scale.min : scale.max);
Expand Down Expand Up @@ -321,10 +321,12 @@ export default class RadialLinearScale extends LinearScaleBase {
LinearScaleBase.prototype.generateTickLabels.call(this, ticks);

// Point labels
this._pointLabels = this.getLabels().map((value, index) => {
const label = callCallback(this.options.pointLabels.callback, [value, index], this);
return label || label === 0 ? label : '';
});
this._pointLabels = this.getLabels()
.filter((v, i) => this.chart.getDataVisibility(i))
.map((value, index) => {
const label = callCallback(this.options.pointLabels.callback, [value, index], this);
kurkle marked this conversation as resolved.
Show resolved Hide resolved
return label || label === 0 ? label : '';
});
}

fit() {
Expand Down Expand Up @@ -369,7 +371,7 @@ export default class RadialLinearScale extends LinearScaleBase {
}

getIndexAngle(index) {
const angleMultiplier = TAU / this.getLabels().length;
const angleMultiplier = TAU / this._pointLabels.length;
const startAngle = this.options.startAngle || 0;
return _normalizeAngle(index * angleMultiplier + toRadians(startAngle));
}
Expand Down Expand Up @@ -441,7 +443,7 @@ export default class RadialLinearScale extends LinearScaleBase {
const ctx = this.ctx;
ctx.save();
ctx.beginPath();
pathRadiusLine(this, this.getDistanceFromCenterForValue(this._endValue), circular, this.getLabels().length);
pathRadiusLine(this, this.getDistanceFromCenterForValue(this._endValue), circular, this._pointLabels.length);
ctx.closePath();
ctx.fillStyle = backgroundColor;
ctx.fill();
Expand All @@ -456,7 +458,7 @@ export default class RadialLinearScale extends LinearScaleBase {
const ctx = this.ctx;
const opts = this.options;
const {angleLines, grid} = opts;
const labelCount = this.getLabels().length;
const labelCount = this._pointLabels.length;

let i, offset, position;

Expand All @@ -477,7 +479,7 @@ export default class RadialLinearScale extends LinearScaleBase {
if (angleLines.display) {
ctx.save();

for (i = this.getLabels().length - 1; i >= 0; i--) {
for (i = labelCount - 1; i >= 0; i--) {
const optsAtIndex = angleLines.setContext(this.getPointLabelContext(i));
const {color, lineWidth} = optsAtIndex;

Expand Down
16 changes: 16 additions & 0 deletions test/specs/scale.radialLinear.tests.js
Expand Up @@ -369,6 +369,22 @@ describe('Test the radial linear scale', function() {
expect(chart.scales.r._pointLabels).toEqual([0, '', '', '', '', '']);
});

it('Should build point labels considering hidden data', function() {
const chart = window.acquireChart({
type: 'polarArea',
data: {
datasets: [{
data: [10, 5, 0, 25, 78, 20]
}],
labels: ['a', 'b', 'c', 'd', 'e', 'f']
}
});
chart.toggleDataVisibility(3);
chart.update();

expect(chart.scales.r._pointLabels).toEqual(['a', 'b', 'c', 'e', 'f']);
});

it('should correctly set the center point', function() {
var chart = window.acquireChart({
type: 'radar',
Expand Down