Skip to content

Commit b527a47

Browse files
nagixsimonbrunel
authored andcommitted
Fix cut off tick labels in radial scale (chartjs#5848)
Fix the issue that the topmost tick label and the bottom of the chart area are cut off with a radial scale.
1 parent b228909 commit b527a47

File tree

8 files changed

+51
-34
lines changed

8 files changed

+51
-34
lines changed

src/scales/scale.radialLinear.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ module.exports = function(Chart) {
7979
};
8080
}
8181

82+
function getTickFontSize(scale) {
83+
var opts = scale.options;
84+
var tickOpts = opts.ticks;
85+
86+
if (tickOpts.display && opts.display) {
87+
return helpers.valueOrDefault(tickOpts.fontSize, globalDefaults.defaultFontSize);
88+
}
89+
return 0;
90+
}
91+
8292
function measureLabelSize(ctx, fontSize, label) {
8393
if (helpers.isArray(label)) {
8494
return {
@@ -145,6 +155,7 @@ module.exports = function(Chart) {
145155
*/
146156

147157
var plFont = getPointLabelFontOptions(scale);
158+
var paddingTop = getTickFontSize(scale) / 2;
148159

149160
// Get maximum radius of the polygon. Either half the height (minus the text width) or half the width.
150161
// Use this to calculate the offset + change. - Make sure L/R protrusion is at least 0 to stop issues with centre points
@@ -194,16 +205,22 @@ module.exports = function(Chart) {
194205
}
195206
}
196207

208+
if (paddingTop && -paddingTop < furthestLimits.t) {
209+
furthestLimits.t = -paddingTop;
210+
furthestAngles.t = 0;
211+
}
212+
197213
scale.setReductions(largestPossibleRadius, furthestLimits, furthestAngles);
198214
}
199215

200216
/**
201217
* Helper function to fit a radial linear scale with no point labels
202218
*/
203219
function fit(scale) {
204-
var largestPossibleRadius = Math.min(scale.height / 2, scale.width / 2);
205-
scale.drawingArea = Math.round(largestPossibleRadius);
206-
scale.setCenterPoint(0, 0, 0, 0);
220+
var paddingTop = getTickFontSize(scale) / 2;
221+
var largestPossibleRadius = Math.min((scale.height - paddingTop) / 2, scale.width / 2);
222+
scale.drawingArea = Math.floor(largestPossibleRadius);
223+
scale.setCenterPoint(0, 0, paddingTop, 0);
207224
}
208225

209226
function getTextAlignForAngle(angle) {
@@ -341,8 +358,8 @@ module.exports = function(Chart) {
341358
// Set the unconstrained dimension before label rotation
342359
me.width = me.maxWidth;
343360
me.height = me.maxHeight;
344-
me.xCenter = Math.round(me.width / 2);
345-
me.yCenter = Math.round(me.height / 2);
361+
me.xCenter = Math.floor(me.width / 2);
362+
me.yCenter = Math.floor(me.height / 2);
346363

347364
var minSize = helpers.min([me.height, me.width]);
348365
var tickFontSize = helpers.valueOrDefault(tickOpts.fontSize, globalDefaults.defaultFontSize);
@@ -416,8 +433,8 @@ module.exports = function(Chart) {
416433
radiusReductionBottom = numberOrZero(radiusReductionBottom);
417434

418435
me.drawingArea = Math.min(
419-
Math.round(largestPossibleRadius - (radiusReductionLeft + radiusReductionRight) / 2),
420-
Math.round(largestPossibleRadius - (radiusReductionTop + radiusReductionBottom) / 2));
436+
Math.floor(largestPossibleRadius - (radiusReductionLeft + radiusReductionRight) / 2),
437+
Math.floor(largestPossibleRadius - (radiusReductionTop + radiusReductionBottom) / 2));
421438
me.setCenterPoint(radiusReductionLeft, radiusReductionRight, radiusReductionTop, radiusReductionBottom);
422439
},
423440
setCenterPoint: function(leftMovement, rightMovement, topMovement, bottomMovement) {
@@ -427,8 +444,8 @@ module.exports = function(Chart) {
427444
var maxTop = topMovement + me.drawingArea;
428445
var maxBottom = me.height - bottomMovement - me.drawingArea;
429446

430-
me.xCenter = Math.round(((maxLeft + maxRight) / 2) + me.left);
431-
me.yCenter = Math.round(((maxTop + maxBottom) / 2) + me.top);
447+
me.xCenter = Math.floor(((maxLeft + maxRight) / 2) + me.left);
448+
me.yCenter = Math.floor(((maxTop + maxBottom) / 2) + me.top);
432449
},
433450

434451
getIndexAngle: function(index) {
7.83 KB
Loading
Loading
Loading

test/specs/controller.doughnut.tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ describe('Chart.controllers.doughnut', function() {
195195
{c: Math.PI / 8, s: Math.PI, e: Math.PI + Math.PI / 8},
196196
{c: 3 * Math.PI / 8, s: Math.PI + Math.PI / 8, e: Math.PI + Math.PI / 2}
197197
].forEach(function(expected, i) {
198-
expect(meta.data[i]._model.x).toBeCloseToPixel(510);
199-
expect(meta.data[i]._model.y).toBeCloseToPixel(510);
200-
expect(meta.data[i]._model.outerRadius).toBeCloseToPixel(509);
201-
expect(meta.data[i]._model.innerRadius).toBeCloseToPixel(381);
198+
expect(meta.data[i]._model.x).toBeCloseToPixel(511);
199+
expect(meta.data[i]._model.y).toBeCloseToPixel(511);
200+
expect(meta.data[i]._model.outerRadius).toBeCloseToPixel(510);
201+
expect(meta.data[i]._model.innerRadius).toBeCloseToPixel(382);
202202
expect(meta.data[i]._model.circumference).toBeCloseTo(expected.c, 8);
203203
expect(meta.data[i]._model.startAngle).toBeCloseTo(expected.s, 8);
204204
expect(meta.data[i]._model.endAngle).toBeCloseTo(expected.e, 8);

test/specs/controller.polarArea.tests.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ describe('Chart.controllers.polarArea', function() {
9999
expect(meta.data.length).toBe(4);
100100

101101
[
102-
{o: 179, s: -0.5 * Math.PI, e: 0},
103-
{o: 243, s: 0, e: 0.5 * Math.PI},
102+
{o: 177, s: -0.5 * Math.PI, e: 0},
103+
{o: 240, s: 0, e: 0.5 * Math.PI},
104104
{o: 51, s: 0.5 * Math.PI, e: Math.PI},
105105
{o: 0, s: Math.PI, e: 1.5 * Math.PI}
106106
].forEach(function(expected, i) {
107107
expect(meta.data[i]._model.x).toBeCloseToPixel(256);
108-
expect(meta.data[i]._model.y).toBeCloseToPixel(256);
108+
expect(meta.data[i]._model.y).toBeCloseToPixel(259);
109109
expect(meta.data[i]._model.innerRadius).toBeCloseToPixel(0);
110110
expect(meta.data[i]._model.outerRadius).toBeCloseToPixel(expected.o);
111111
expect(meta.data[i]._model.startAngle).toBe(expected.s);
@@ -141,9 +141,9 @@ describe('Chart.controllers.polarArea', function() {
141141
chart.update();
142142

143143
expect(meta.data[0]._model.x).toBeCloseToPixel(256);
144-
expect(meta.data[0]._model.y).toBeCloseToPixel(256);
144+
expect(meta.data[0]._model.y).toBeCloseToPixel(259);
145145
expect(meta.data[0]._model.innerRadius).toBeCloseToPixel(0);
146-
expect(meta.data[0]._model.outerRadius).toBeCloseToPixel(179);
146+
expect(meta.data[0]._model.outerRadius).toBeCloseToPixel(177);
147147
expect(meta.data[0]._model).toEqual(jasmine.objectContaining({
148148
startAngle: -0.5 * Math.PI,
149149
endAngle: 0,
@@ -183,13 +183,13 @@ describe('Chart.controllers.polarArea', function() {
183183
expect(meta.data.length).toBe(4);
184184

185185
[
186-
{o: 179, s: 0, e: 0.5 * Math.PI},
187-
{o: 243, s: 0.5 * Math.PI, e: Math.PI},
186+
{o: 177, s: 0, e: 0.5 * Math.PI},
187+
{o: 240, s: 0.5 * Math.PI, e: Math.PI},
188188
{o: 51, s: Math.PI, e: 1.5 * Math.PI},
189189
{o: 0, s: 1.5 * Math.PI, e: 2.0 * Math.PI}
190190
].forEach(function(expected, i) {
191191
expect(meta.data[i]._model.x).toBeCloseToPixel(256);
192-
expect(meta.data[i]._model.y).toBeCloseToPixel(256);
192+
expect(meta.data[i]._model.y).toBeCloseToPixel(259);
193193
expect(meta.data[i]._model.innerRadius).toBeCloseToPixel(0);
194194
expect(meta.data[i]._model.outerRadius).toBeCloseToPixel(expected.o);
195195
expect(meta.data[i]._model.startAngle).toBe(expected.s);

test/specs/controller.radar.tests.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ describe('Chart.controllers.radar', function() {
154154
meta.controller.update();
155155

156156
[
157-
{x: 256, y: 117, cppx: 246, cppy: 117, cpnx: 272, cpny: 117},
158-
{x: 464, y: 256, cppx: 464, cppy: 248, cpnx: 464, cpny: 262},
159-
{x: 256, y: 256, cppx: 276.9, cppy: 256, cpnx: 250.4, cpny: 256},
160-
{x: 200, y: 256, cppx: 200, cppy: 259, cpnx: 200, cpny: 245},
157+
{x: 256, y: 116, cppx: 246, cppy: 116, cpnx: 273, cpny: 116},
158+
{x: 466, y: 256, cppx: 466, cppy: 248, cpnx: 466, cpny: 262},
159+
{x: 256, y: 256, cppx: 277, cppy: 256, cpnx: 250.4, cpny: 256},
160+
{x: 200, y: 256, cppx: 200, cppy: 260, cpnx: 200, cpny: 246},
161161
].forEach(function(expected, i) {
162162
expect(meta.data[i]._model.x).toBeCloseToPixel(expected.x);
163163
expect(meta.data[i]._model.y).toBeCloseToPixel(expected.y);
@@ -211,8 +211,8 @@ describe('Chart.controllers.radar', function() {
211211

212212
// Since tension is now 0, we don't care about the control points
213213
[
214-
{x: 256, y: 117},
215-
{x: 464, y: 256},
214+
{x: 256, y: 116},
215+
{x: 466, y: 256},
216216
{x: 256, y: 256},
217217
{x: 200, y: 256},
218218
].forEach(function(expected, i) {
@@ -270,11 +270,11 @@ describe('Chart.controllers.radar', function() {
270270
}));
271271

272272
expect(meta.data[0]._model.x).toBeCloseToPixel(256);
273-
expect(meta.data[0]._model.y).toBeCloseToPixel(117);
273+
expect(meta.data[0]._model.y).toBeCloseToPixel(116);
274274
expect(meta.data[0]._model.controlPointPreviousX).toBeCloseToPixel(241);
275-
expect(meta.data[0]._model.controlPointPreviousY).toBeCloseToPixel(117);
275+
expect(meta.data[0]._model.controlPointPreviousY).toBeCloseToPixel(116);
276276
expect(meta.data[0]._model.controlPointNextX).toBeCloseToPixel(281);
277-
expect(meta.data[0]._model.controlPointNextY).toBeCloseToPixel(117);
277+
expect(meta.data[0]._model.controlPointNextY).toBeCloseToPixel(116);
278278
expect(meta.data[0]._model).toEqual(jasmine.objectContaining({
279279
radius: 2.2,
280280
backgroundColor: 'rgb(0, 1, 3)',

test/specs/scale.radialLinear.tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ describe('Test the radial linear scale', function() {
349349
}
350350
});
351351

352-
expect(chart.scale.drawingArea).toBe(233);
352+
expect(chart.scale.drawingArea).toBe(232);
353353
expect(chart.scale.xCenter).toBe(256);
354-
expect(chart.scale.yCenter).toBe(280);
354+
expect(chart.scale.yCenter).toBe(279);
355355
});
356356

357357
it('should correctly get the label for a given data index', function() {
@@ -397,7 +397,7 @@ describe('Test the radial linear scale', function() {
397397
});
398398

399399
expect(chart.scale.getDistanceFromCenterForValue(chart.scale.min)).toBe(0);
400-
expect(chart.scale.getDistanceFromCenterForValue(chart.scale.max)).toBe(233);
400+
expect(chart.scale.getDistanceFromCenterForValue(chart.scale.max)).toBe(232);
401401
expect(chart.scale.getPointPositionForValue(1, 5)).toEqual({
402402
x: 270,
403403
y: 275,
@@ -406,7 +406,7 @@ describe('Test the radial linear scale', function() {
406406
chart.scale.options.ticks.reverse = true;
407407
chart.update();
408408

409-
expect(chart.scale.getDistanceFromCenterForValue(chart.scale.min)).toBe(233);
409+
expect(chart.scale.getDistanceFromCenterForValue(chart.scale.min)).toBe(232);
410410
expect(chart.scale.getDistanceFromCenterForValue(chart.scale.max)).toBe(0);
411411
});
412412

0 commit comments

Comments
 (0)