Skip to content

Commit d5b6698

Browse files
kurkleetimberg
authored andcommitted
Performance improvements (#6729)
1 parent 91466ae commit d5b6698

File tree

3 files changed

+22
-71
lines changed

3 files changed

+22
-71
lines changed

src/elements/element.line.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Line extends Element {
3636
var vm = me._view;
3737
var ctx = me._ctx;
3838
var spanGaps = vm.spanGaps;
39-
var points = me._children.slice(); // clone array
39+
var points = me._children;
4040
var globalDefaults = defaults.global;
4141
var globalOptionLineElements = globalDefaults.elements.line;
4242
var lastDrawnIndex = -1;
@@ -48,6 +48,7 @@ class Line extends Element {
4848
}
4949

5050
if (me._loop) {
51+
points = points.slice(); // clone array
5152
for (index = 0; index < points.length; ++index) {
5253
previous = helpers.previousItem(points, index);
5354
// If the line has an open path, shift the point array

src/elements/element.point.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const defaults = require('../core/core.defaults');
44
const Element = require('../core/core.element');
55
const helpers = require('../helpers/index');
66

7-
const valueOrDefault = helpers.valueOrDefault;
8-
97
const defaultColor = defaults.global.defaultColor;
108

119
defaults._set('global', {
@@ -31,37 +29,37 @@ class Point extends Element {
3129
}
3230

3331
inRange(mouseX, mouseY) {
34-
var vm = this._view;
32+
const vm = this._view;
3533
return vm ? ((Math.pow(mouseX - vm.x, 2) + Math.pow(mouseY - vm.y, 2)) < Math.pow(vm.hitRadius + vm.radius, 2)) : false;
3634
}
3735

3836
inXRange(mouseX) {
39-
var vm = this._view;
37+
const vm = this._view;
4038
return vm ? (Math.abs(mouseX - vm.x) < vm.radius + vm.hitRadius) : false;
4139
}
4240

4341
inYRange(mouseY) {
44-
var vm = this._view;
42+
const vm = this._view;
4543
return vm ? (Math.abs(mouseY - vm.y) < vm.radius + vm.hitRadius) : false;
4644
}
4745

4846
getCenterPoint() {
49-
var vm = this._view;
47+
const vm = this._view;
5048
return {
5149
x: vm.x,
5250
y: vm.y
5351
};
5452
}
5553

5654
size() {
57-
var vm = this._view;
58-
var radius = vm.radius || 0;
59-
var borderWidth = vm.borderWidth || 0;
55+
const vm = this._view;
56+
const radius = vm.radius || 0;
57+
const borderWidth = vm.borderWidth || 0;
6058
return (radius + borderWidth) * 2;
6159
}
6260

6361
tooltipPosition() {
64-
var vm = this._view;
62+
const vm = this._view;
6563
return {
6664
x: vm.x,
6765
y: vm.y,
@@ -70,25 +68,23 @@ class Point extends Element {
7068
}
7169

7270
draw(chartArea) {
73-
var vm = this._view;
74-
var ctx = this._ctx;
75-
var pointStyle = vm.pointStyle;
76-
var rotation = vm.rotation;
77-
var radius = vm.radius;
78-
var x = vm.x;
79-
var y = vm.y;
80-
var globalDefaults = defaults.global;
81-
var defaultColor = globalDefaults.defaultColor; // eslint-disable-line no-shadow
82-
83-
if (vm.skip) {
71+
const vm = this._view;
72+
const ctx = this._ctx;
73+
const pointStyle = vm.pointStyle;
74+
const rotation = vm.rotation;
75+
const radius = vm.radius;
76+
const x = vm.x;
77+
const y = vm.y;
78+
79+
if (vm.skip || radius <= 0) {
8480
return;
8581
}
8682

8783
// Clipping for Points.
8884
if (chartArea === undefined || helpers.canvas._isPointInArea(vm, chartArea)) {
89-
ctx.strokeStyle = vm.borderColor || defaultColor;
90-
ctx.lineWidth = valueOrDefault(vm.borderWidth, globalDefaults.elements.point.borderWidth);
91-
ctx.fillStyle = vm.backgroundColor || defaultColor;
85+
ctx.strokeStyle = vm.borderColor;
86+
ctx.lineWidth = vm.borderWidth;
87+
ctx.fillStyle = vm.backgroundColor;
9288
helpers.canvas.drawPoint(ctx, pointStyle, radius, x, y, rotation);
9389
}
9490
}

test/specs/element.point.tests.js

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -72,52 +72,6 @@ describe('Chart.elements.Point', function() {
7272
expect(point.getCenterPoint()).toEqual({x: 10, y: 10});
7373
});
7474

75-
it ('should draw correctly with default settings if necessary', function() {
76-
var mockContext = window.createMockContext();
77-
var point = new Chart.elements.Point({
78-
_datasetIndex: 2,
79-
_index: 1,
80-
_ctx: mockContext
81-
});
82-
83-
// Attach a view object as if we were the controller
84-
point._view = {
85-
radius: 2,
86-
hitRadius: 3,
87-
x: 10,
88-
y: 15,
89-
ctx: mockContext
90-
};
91-
92-
point.draw();
93-
94-
expect(mockContext.getCalls()).toEqual([{
95-
name: 'setStrokeStyle',
96-
args: ['rgba(0,0,0,0.1)']
97-
}, {
98-
name: 'setLineWidth',
99-
args: [1]
100-
}, {
101-
name: 'setFillStyle',
102-
args: ['rgba(0,0,0,0.1)']
103-
}, {
104-
name: 'beginPath',
105-
args: []
106-
}, {
107-
name: 'arc',
108-
args: [10, 15, 2, 0, 2 * Math.PI]
109-
}, {
110-
name: 'closePath',
111-
args: [],
112-
}, {
113-
name: 'fill',
114-
args: [],
115-
}, {
116-
name: 'stroke',
117-
args: []
118-
}]);
119-
});
120-
12175
it ('should not draw if skipped', function() {
12276
var mockContext = window.createMockContext();
12377
var point = new Chart.elements.Point({

0 commit comments

Comments
 (0)