Skip to content

Commit

Permalink
Implement radar span gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 9, 2018
1 parent 4098f92 commit 2454c41
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/controllers/controller.radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = function(Chart) {
// Model
_model: {
// Appearance
spanGaps: helpers.valueOrDefault(dataset.spanGaps, me.chart.options.spanGaps),
tension: custom.tension ? custom.tension : helpers.valueOrDefault(dataset.lineTension, lineElementOptions.tension),
backgroundColor: custom.backgroundColor ? custom.backgroundColor : (dataset.backgroundColor || lineElementOptions.backgroundColor),
borderWidth: custom.borderWidth ? custom.borderWidth : (dataset.borderWidth || lineElementOptions.borderWidth),
Expand Down Expand Up @@ -104,6 +105,7 @@ module.exports = function(Chart) {
_model: {
x: reset ? scale.xCenter : pointPosition.x, // value not used in dataset scale, but we want a consistent API between scales
y: reset ? scale.yCenter : pointPosition.y,
skip: custom.skip || isNaN(pointPosition.x) || isNaN(pointPosition.y),

// Appearance
tension: custom.tension ? custom.tension : helpers.valueOrDefault(dataset.lineTension, me.chart.options.elements.line.tension),
Expand All @@ -128,6 +130,13 @@ module.exports = function(Chart) {
var points = meta.data || [];
var i, ilen, model, controlPoints;

// Only consider points that are drawn in case the spanGaps option is used
if (meta.dataset._model.spanGaps) {
points = points.filter(function(pt) {
return !pt._model.skip;
});
}

function capControlPoint(pt, min, max) {
return Math.max(Math.min(pt, max), min);
}
Expand Down
10 changes: 8 additions & 2 deletions src/elements/element.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ module.exports = Element.extend({
var lastDrawnIndex = -1;
var index, current, previous, currentVM;

// If we are looping, adding the first point again
// If we are looping, adding the first non-skipped point again
if (me._loop && points.length) {
points.push(points[0]);
for (index = 0; index < points.length; index++) {
current = points[index];
if (!current._view.skip) {
points.push(current);
break;
}
}
}

ctx.save();
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/plugin.filler.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,22 @@ function doFill(ctx, points, mapper, view, color, loop) {
var len0 = 0;
var len1 = 0;
var i, ilen, index, p0, p1, d0, d1;
var loopOffset;

ctx.beginPath();

for (i = 0, ilen = (count + !!loop); i < ilen; ++i) {
for (i = 0, ilen = count; i < ilen; i++) {
index = i % count;
p0 = points[index]._view;
p1 = mapper(p0, index, view);
d0 = isDrawable(p0);
d1 = isDrawable(p1);

if (loop && loopOffset === undefined && d0) {
loopOffset = i + 1;
ilen = count + loopOffset;
}

if (d0 && d1) {
len0 = curve0.push(p0);
len1 = curve1.push(p1);
Expand Down
4 changes: 2 additions & 2 deletions src/scales/scale.radialLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ module.exports = function(Chart) {
getDistanceFromCenterForValue: function(value) {
var me = this;

if (value === null) {
return 0; // null always in center
if (value === null || isNaN(value)) {
return NaN; // null always in center
}

// Take into account half font size + the yPadding of the top value
Expand Down
3 changes: 3 additions & 0 deletions test/specs/element.line.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,9 @@ describe('Chart.elements.Line', function() {
}, {
name: 'lineTo',
args: [19, -5]
}, {
name: 'lineTo',
args: [5, 0]
}, {
name: 'stroke',
args: [],
Expand Down

0 comments on commit 2454c41

Please sign in to comment.