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

Fix arc border with circumference over 2*PI #6215

Merged
merged 6 commits into from Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
90 changes: 63 additions & 27 deletions src/elements/element.arc.js
Expand Up @@ -15,6 +15,38 @@ defaults._set('global', {
}
});

function pathArc(ctx, x, y, args) {
var innerRadius = args.innerRadius;
var outerRadius = args.outerRadius;
var startAngle = args.startAngle;
var endAngle = args.endAngle;

ctx.beginPath();
ctx.arc(x, y, outerRadius, startAngle, endAngle);
ctx.arc(x, y, innerRadius, endAngle, startAngle, true);
ctx.closePath();
}

function clipArc(ctx, vm, args) {
var startAngle = args.startAngle;
var endAngle = args.endAngle;
var pixelMargin = args.pixelMargin;
var angleMargin = pixelMargin / vm.outerRadius;

// Draw an inner border by cliping the arc and drawing a double-width border
// Enlarge the clipping arc by 0.33 pixels to eliminate glitches between borders
ctx.beginPath();
ctx.arc(vm.x, vm.y, vm.outerRadius, startAngle - angleMargin, endAngle + angleMargin);
if (vm.innerRadius > pixelMargin) {
angleMargin = pixelMargin / vm.innerRadius;
ctx.arc(vm.x, vm.y, vm.innerRadius - pixelMargin, endAngle + angleMargin, startAngle - angleMargin, true);
} else {
ctx.arc(vm.x, vm.y, pixelMargin, endAngle + Math.PI / 2, startAngle - Math.PI / 2);
}
ctx.closePath();
ctx.clip();
}

module.exports = Element.extend({
inLabelRange: function(mouseX) {
var vm = this._view;
Expand All @@ -30,7 +62,7 @@ module.exports = Element.extend({

if (vm) {
var pointRelativePosition = helpers.getAngleFromPoint(vm, {x: chartX, y: chartY});
var angle = pointRelativePosition.angle;
var angle = pointRelativePosition.angle;
var distance = pointRelativePosition.distance;

// Sanitise angle range
Expand Down Expand Up @@ -84,41 +116,45 @@ module.exports = Element.extend({
draw: function() {
var ctx = this._chart.ctx;
var vm = this._view;
var sA = vm.startAngle;
var eA = vm.endAngle;
var startAngle = vm.startAngle;
var endAngle = vm.endAngle;
var pixelMargin = (vm.borderAlign === 'inner') ? 0.33 : 0;
var angleMargin;
var outerRadius = Math.max(vm.outerRadius - pixelMargin, 0);
var innerRadius = vm.innerRadius;
var x = vm.x;
var y = vm.y;
var tA;

ctx.save();

ctx.beginPath();
ctx.arc(vm.x, vm.y, Math.max(vm.outerRadius - pixelMargin, 0), sA, eA);
ctx.arc(vm.x, vm.y, vm.innerRadius, eA, sA, true);
ctx.closePath();

ctx.fillStyle = vm.backgroundColor;

if (vm.circumference > Math.PI * 2) {
Copy link
Contributor

@nagix nagix Apr 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in case 0 <= circumference <= 2*PI and 2*PI < circumference <= 4*PI, but appearance is not consistent when circumference > 4*PI. How about looping this part (endAngle - startAngle) / (Math.PI * 2) times?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loop implemented, did some more refactoring to keep CC (and me) happy 😄
Demo updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a loop would doing % 2PI be more efficient?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etimberg that was my initial approach, but with transparency it is not consistent with circumference > 4*PI.
Not sure if anyone is going to use circumference that high anyway, but now its drawn consistently.

tA = startAngle;
startAngle += (endAngle - startAngle) % (Math.PI * 2);
pathArc(ctx, x, y, {
innerRadius: innerRadius, outerRadius: outerRadius,
startAngle: tA, endAngle: startAngle
});
ctx.fill();
}

pathArc(ctx, x, y, {
innerRadius: innerRadius, outerRadius: outerRadius,
startAngle: startAngle, endAngle: endAngle
});
ctx.fill();

if (vm.borderWidth) {
if (vm.borderAlign === 'inner') {
// Draw an inner border by cliping the arc and drawing a double-width border
// Enlarge the clipping arc by 0.33 pixels to eliminate glitches between borders
ctx.beginPath();
angleMargin = pixelMargin / vm.outerRadius;
ctx.arc(vm.x, vm.y, vm.outerRadius, sA - angleMargin, eA + angleMargin);
if (vm.innerRadius > pixelMargin) {
angleMargin = pixelMargin / vm.innerRadius;
ctx.arc(vm.x, vm.y, vm.innerRadius - pixelMargin, eA + angleMargin, sA - angleMargin, true);
} else {
ctx.arc(vm.x, vm.y, pixelMargin, eA + Math.PI / 2, sA - Math.PI / 2);
}
ctx.closePath();
ctx.clip();

ctx.beginPath();
ctx.arc(vm.x, vm.y, vm.outerRadius, sA, eA);
ctx.arc(vm.x, vm.y, vm.innerRadius, eA, sA, true);
ctx.closePath();
clipArc(ctx, vm, {
startAngle: startAngle, endAngle: endAngle,
pixelMargin: pixelMargin
});
pathArc(ctx, x, y, {
innerRadius: innerRadius, outerRadius: vm.outerRadius,
startAngle: startAngle, endAngle: endAngle
});

ctx.lineWidth = vm.borderWidth * 2;
ctx.lineJoin = 'round';
Expand Down
@@ -0,0 +1,24 @@
{
"config": {
"type": "doughnut",
"data": {
"labels": ["A"],
"datasets": [{
"data": [100],
"backgroundColor": [
"rgba(153, 102, 255, 0.8)"
],
"borderWidth": 20,
"borderColor": [
"rgb(153, 102, 255)"
]
}]
},
"options": {
"circumference": 7,
"responsive": false,
"legend": false,
"title": false
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
210 changes: 0 additions & 210 deletions test/specs/element.arc.tests.js
Expand Up @@ -99,214 +99,4 @@ describe('Arc element tests', function() {
expect(center.x).toBeCloseTo(0.5, 6);
expect(center.y).toBeCloseTo(0.5, 6);
});

it ('should draw correctly with no border', function() {
var mockContext = window.createMockContext();
var arc = new Chart.elements.Arc({
_datasetIndex: 2,
_index: 1,
_chart: {
ctx: mockContext,
}
});

// Mock out the view as if the controller put it there
arc._view = {
startAngle: 0,
endAngle: Math.PI / 2,
x: 10,
y: 5,
innerRadius: 1,
outerRadius: 3,

backgroundColor: 'rgb(0, 0, 255)',
borderColor: 'rgb(255, 0, 0)',
};

arc.draw();

expect(mockContext.getCalls()).toEqual([{
name: 'save',
args: []
}, {
name: 'beginPath',
args: []
}, {
name: 'arc',
args: [10, 5, 3, 0, Math.PI / 2]
}, {
name: 'arc',
args: [10, 5, 1, Math.PI / 2, 0, true]
}, {
name: 'closePath',
args: []
}, {
name: 'setFillStyle',
args: ['rgb(0, 0, 255)']
}, {
name: 'fill',
args: []
}, {
name: 'restore',
args: []
}]);
});

it ('should draw correctly with a border', function() {
var mockContext = window.createMockContext();
var arc = new Chart.elements.Arc({
_datasetIndex: 2,
_index: 1,
_chart: {
ctx: mockContext,
}
});

// Mock out the view as if the controller put it there
arc._view = {
startAngle: 0,
endAngle: Math.PI / 2,
x: 10,
y: 5,
innerRadius: 1,
outerRadius: 3,

backgroundColor: 'rgb(0, 0, 255)',
borderColor: 'rgb(255, 0, 0)',
borderWidth: 5
};

arc.draw();

expect(mockContext.getCalls()).toEqual([{
name: 'save',
args: []
}, {
name: 'beginPath',
args: []
}, {
name: 'arc',
args: [10, 5, 3, 0, Math.PI / 2]
}, {
name: 'arc',
args: [10, 5, 1, Math.PI / 2, 0, true]
}, {
name: 'closePath',
args: []
}, {
name: 'setFillStyle',
args: ['rgb(0, 0, 255)']
}, {
name: 'fill',
args: []
}, {
name: 'setLineWidth',
args: [5]
}, {
name: 'setLineJoin',
args: ['bevel']
}, {
name: 'setStrokeStyle',
args: ['rgb(255, 0, 0)']
}, {
name: 'stroke',
args: []
}, {
name: 'restore',
args: []
}]);
});

it ('should draw correctly with an inner border', function() {
var mockContext = window.createMockContext();
var arc = new Chart.elements.Arc({
_datasetIndex: 2,
_index: 1,
_chart: {
ctx: mockContext,
}
});

// Mock out the view as if the controller put it there
arc._view = {
startAngle: 0,
endAngle: Math.PI / 2,
x: 10,
y: 5,
innerRadius: 1,
outerRadius: 3,

backgroundColor: 'rgb(0, 0, 255)',
borderColor: 'rgb(255, 0, 0)',
borderWidth: 5,
borderAlign: 'inner'
};

arc.draw();

expect(mockContext.getCalls()).toEqual([{
name: 'save',
args: []
}, {
name: 'beginPath',
args: []
}, {
name: 'arc',
args: [10, 5, 2.67, 0, Math.PI / 2]
}, {
name: 'arc',
args: [10, 5, 1, Math.PI / 2, 0, true]
}, {
name: 'closePath',
args: []
}, {
name: 'setFillStyle',
args: ['rgb(0, 0, 255)']
}, {
name: 'fill',
args: []
}, {
name: 'beginPath',
args: []
}, {
name: 'arc',
args: [10, 5, 3, -0.11, Math.PI / 2 + 0.11]
}, {
name: 'arc',
args: [10, 5, 1 - 0.33, Math.PI / 2 + 0.33, -0.33, true]
}, {
name: 'closePath',
args: []
}, {
name: 'clip',
args: []
}, {
name: 'beginPath',
args: []
}, {
name: 'arc',
args: [10, 5, 3, 0, Math.PI / 2]
}, {
name: 'arc',
args: [10, 5, 1, Math.PI / 2, 0, true]
}, {
name: 'closePath',
args: []
}, {
name: 'setLineWidth',
args: [10]
}, {
name: 'setLineJoin',
args: ['round']
}, {
name: 'setStrokeStyle',
args: ['rgb(255, 0, 0)']
}, {
name: 'stroke',
args: []
}, {
name: 'restore',
args: []
}]);
});
});