2016-02-12 04:30:53 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
var helpers = Chart.helpers;
|
|
|
|
|
|
|
|
|
|
|
|
Chart.defaults.radar = {
|
|
|
|
scale: {
|
|
|
|
type: "radialLinear"
|
|
|
|
},
|
|
|
|
elements: {
|
|
|
|
line: {
|
|
|
|
tension: 0 // no bezier in radar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Chart.controllers.radar = Chart.DatasetController.extend({
|
|
|
|
linkScales: function() {
|
|
|
|
// No need. Single scale only
|
|
|
|
},
|
|
|
|
|
|
|
|
addElements: function() {
|
2016-04-21 17:11:52 +02:00
|
|
|
var meta = this.getMeta();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-04-21 17:11:52 +02:00
|
|
|
meta.dataset = meta.dataset || new Chart.elements.Line({
|
2016-02-14 23:06:00 +01:00
|
|
|
_chart: this.chart.chart,
|
|
|
|
_datasetIndex: this.index,
|
2016-04-21 17:11:52 +02:00
|
|
|
_points: meta.data,
|
2016-02-14 23:06:00 +01:00
|
|
|
_loop: true
|
|
|
|
});
|
|
|
|
|
|
|
|
helpers.each(this.getDataset().data, function(value, index) {
|
2016-04-21 17:11:52 +02:00
|
|
|
meta.data[index] = meta.data[index] || new Chart.elements.Point({
|
2016-02-14 23:06:00 +01:00
|
|
|
_chart: this.chart.chart,
|
|
|
|
_datasetIndex: this.index,
|
|
|
|
_index: index,
|
|
|
|
_model: {
|
|
|
|
x: 0, //xScale.getPixelForValue(null, index, true),
|
|
|
|
y: 0 //this.chartArea.bottom,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
addElementAndReset: function(index) {
|
|
|
|
var point = new Chart.elements.Point({
|
|
|
|
_chart: this.chart.chart,
|
|
|
|
_datasetIndex: this.index,
|
|
|
|
_index: index
|
|
|
|
});
|
|
|
|
|
2016-04-21 17:11:52 +02:00
|
|
|
// Add to the points array and reset it
|
|
|
|
this.getMeta().data.splice(index, 0, point);
|
2016-02-14 23:06:00 +01:00
|
|
|
this.updateElement(point, index, true);
|
|
|
|
|
|
|
|
// Make sure bezier control points are updated
|
|
|
|
this.updateBezierControlPoints();
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function update(reset) {
|
2016-04-21 17:11:52 +02:00
|
|
|
var meta = this.getMeta();
|
|
|
|
var line = meta.dataset;
|
|
|
|
var points = meta.data;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
var scale = this.chart.scale;
|
|
|
|
var scaleBase;
|
|
|
|
|
|
|
|
if (scale.min < 0 && scale.max < 0) {
|
|
|
|
scaleBase = scale.getPointPositionForValue(0, scale.max);
|
|
|
|
} else if (scale.min > 0 && scale.max > 0) {
|
|
|
|
scaleBase = scale.getPointPositionForValue(0, scale.min);
|
|
|
|
} else {
|
|
|
|
scaleBase = scale.getPointPositionForValue(0, 0);
|
|
|
|
}
|
|
|
|
|
2016-04-21 17:11:52 +02:00
|
|
|
helpers.extend(meta.dataset, {
|
2016-02-14 23:06:00 +01:00
|
|
|
// Utility
|
|
|
|
_datasetIndex: this.index,
|
|
|
|
// Data
|
2016-04-21 17:11:52 +02:00
|
|
|
_children: points,
|
2016-02-14 23:06:00 +01:00
|
|
|
// Model
|
|
|
|
_model: {
|
|
|
|
// Appearance
|
|
|
|
tension: line.custom && line.custom.tension ? line.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension),
|
|
|
|
backgroundColor: line.custom && line.custom.backgroundColor ? line.custom.backgroundColor : (this.getDataset().backgroundColor || this.chart.options.elements.line.backgroundColor),
|
|
|
|
borderWidth: line.custom && line.custom.borderWidth ? line.custom.borderWidth : (this.getDataset().borderWidth || this.chart.options.elements.line.borderWidth),
|
|
|
|
borderColor: line.custom && line.custom.borderColor ? line.custom.borderColor : (this.getDataset().borderColor || this.chart.options.elements.line.borderColor),
|
|
|
|
fill: line.custom && line.custom.fill ? line.custom.fill : (this.getDataset().fill !== undefined ? this.getDataset().fill : this.chart.options.elements.line.fill),
|
|
|
|
borderCapStyle: line.custom && line.custom.borderCapStyle ? line.custom.borderCapStyle : (this.getDataset().borderCapStyle || this.chart.options.elements.line.borderCapStyle),
|
|
|
|
borderDash: line.custom && line.custom.borderDash ? line.custom.borderDash : (this.getDataset().borderDash || this.chart.options.elements.line.borderDash),
|
|
|
|
borderDashOffset: line.custom && line.custom.borderDashOffset ? line.custom.borderDashOffset : (this.getDataset().borderDashOffset || this.chart.options.elements.line.borderDashOffset),
|
|
|
|
borderJoinStyle: line.custom && line.custom.borderJoinStyle ? line.custom.borderJoinStyle : (this.getDataset().borderJoinStyle || this.chart.options.elements.line.borderJoinStyle),
|
|
|
|
|
|
|
|
// Scale
|
|
|
|
scaleTop: scale.top,
|
|
|
|
scaleBottom: scale.bottom,
|
|
|
|
scaleZero: scaleBase
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-21 17:11:52 +02:00
|
|
|
meta.dataset.pivot();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Update Points
|
|
|
|
helpers.each(points, function(point, index) {
|
|
|
|
this.updateElement(point, index, reset);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
|
|
|
|
// Update bezier control points
|
|
|
|
this.updateBezierControlPoints();
|
|
|
|
},
|
|
|
|
updateElement: function(point, index, reset) {
|
|
|
|
var pointPosition = this.chart.scale.getPointPositionForValue(index, this.getDataset().data[index]);
|
|
|
|
|
|
|
|
helpers.extend(point, {
|
|
|
|
// Utility
|
|
|
|
_datasetIndex: this.index,
|
|
|
|
_index: index,
|
|
|
|
_scale: this.chart.scale,
|
|
|
|
|
|
|
|
// Desired view properties
|
|
|
|
_model: {
|
|
|
|
x: reset ? this.chart.scale.xCenter : pointPosition.x, // value not used in dataset scale, but we want a consistent API between scales
|
|
|
|
y: reset ? this.chart.scale.yCenter : pointPosition.y,
|
|
|
|
|
|
|
|
// Appearance
|
|
|
|
tension: point.custom && point.custom.tension ? point.custom.tension : helpers.getValueOrDefault(this.getDataset().tension, this.chart.options.elements.line.tension),
|
|
|
|
radius: point.custom && point.custom.radius ? point.custom.radius : helpers.getValueAtIndexOrDefault(this.getDataset().pointRadius, index, this.chart.options.elements.point.radius),
|
|
|
|
backgroundColor: point.custom && point.custom.backgroundColor ? point.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().pointBackgroundColor, index, this.chart.options.elements.point.backgroundColor),
|
|
|
|
borderColor: point.custom && point.custom.borderColor ? point.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().pointBorderColor, index, this.chart.options.elements.point.borderColor),
|
|
|
|
borderWidth: point.custom && point.custom.borderWidth ? point.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().pointBorderWidth, index, this.chart.options.elements.point.borderWidth),
|
|
|
|
pointStyle: point.custom && point.custom.pointStyle ? point.custom.pointStyle : helpers.getValueAtIndexOrDefault(this.getDataset().pointStyle, index, this.chart.options.elements.point.pointStyle),
|
|
|
|
|
|
|
|
// Tooltip
|
|
|
|
hitRadius: point.custom && point.custom.hitRadius ? point.custom.hitRadius : helpers.getValueAtIndexOrDefault(this.getDataset().hitRadius, index, this.chart.options.elements.point.hitRadius)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
point._model.skip = point.custom && point.custom.skip ? point.custom.skip : (isNaN(point._model.x) || isNaN(point._model.y));
|
|
|
|
},
|
|
|
|
updateBezierControlPoints: function() {
|
2016-04-21 17:11:52 +02:00
|
|
|
var meta = this.getMeta();
|
|
|
|
helpers.each(meta.data, function(point, index) {
|
2016-02-14 23:06:00 +01:00
|
|
|
var controlPoints = helpers.splineCurve(
|
2016-04-21 17:11:52 +02:00
|
|
|
helpers.previousItem(meta.data, index, true)._model,
|
2016-02-14 23:06:00 +01:00
|
|
|
point._model,
|
2016-04-21 17:11:52 +02:00
|
|
|
helpers.nextItem(meta.data, index, true)._model,
|
2016-02-14 23:06:00 +01:00
|
|
|
point._model.tension
|
|
|
|
);
|
|
|
|
|
|
|
|
// Prevent the bezier going outside of the bounds of the graph
|
|
|
|
point._model.controlPointPreviousX = Math.max(Math.min(controlPoints.previous.x, this.chart.chartArea.right), this.chart.chartArea.left);
|
|
|
|
point._model.controlPointPreviousY = Math.max(Math.min(controlPoints.previous.y, this.chart.chartArea.bottom), this.chart.chartArea.top);
|
|
|
|
|
|
|
|
point._model.controlPointNextX = Math.max(Math.min(controlPoints.next.x, this.chart.chartArea.right), this.chart.chartArea.left);
|
|
|
|
point._model.controlPointNextY = Math.max(Math.min(controlPoints.next.y, this.chart.chartArea.bottom), this.chart.chartArea.top);
|
|
|
|
|
|
|
|
// Now pivot the point for animation
|
|
|
|
point.pivot();
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
draw: function(ease) {
|
2016-04-21 17:11:52 +02:00
|
|
|
var meta = this.getMeta();
|
2016-02-14 23:06:00 +01:00
|
|
|
var easingDecimal = ease || 1;
|
|
|
|
|
|
|
|
// Transition Point Locations
|
2016-04-21 17:11:52 +02:00
|
|
|
helpers.each(meta.data, function(point, index) {
|
2016-02-14 23:06:00 +01:00
|
|
|
point.transition(easingDecimal);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Transition and Draw the line
|
2016-04-21 17:11:52 +02:00
|
|
|
meta.dataset.transition(easingDecimal).draw();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Draw the points
|
2016-04-21 17:11:52 +02:00
|
|
|
helpers.each(meta.data, function(point) {
|
2016-02-14 23:06:00 +01:00
|
|
|
point.draw();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setHoverStyle: function(point) {
|
|
|
|
// Point
|
|
|
|
var dataset = this.chart.data.datasets[point._datasetIndex];
|
|
|
|
var index = point._index;
|
|
|
|
|
|
|
|
point._model.radius = point.custom && point.custom.hoverRadius ? point.custom.hoverRadius : helpers.getValueAtIndexOrDefault(dataset.pointHoverRadius, index, this.chart.options.elements.point.hoverRadius);
|
|
|
|
point._model.backgroundColor = point.custom && point.custom.hoverBackgroundColor ? point.custom.hoverBackgroundColor : helpers.getValueAtIndexOrDefault(dataset.pointHoverBackgroundColor, index, helpers.color(point._model.backgroundColor).saturate(0.5).darken(0.1).rgbString());
|
|
|
|
point._model.borderColor = point.custom && point.custom.hoverBorderColor ? point.custom.hoverBorderColor : helpers.getValueAtIndexOrDefault(dataset.pointHoverBorderColor, index, helpers.color(point._model.borderColor).saturate(0.5).darken(0.1).rgbString());
|
|
|
|
point._model.borderWidth = point.custom && point.custom.hoverBorderWidth ? point.custom.hoverBorderWidth : helpers.getValueAtIndexOrDefault(dataset.pointHoverBorderWidth, index, point._model.borderWidth);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeHoverStyle: function(point) {
|
|
|
|
var dataset = this.chart.data.datasets[point._datasetIndex];
|
|
|
|
var index = point._index;
|
|
|
|
|
|
|
|
point._model.radius = point.custom && point.custom.radius ? point.custom.radius : helpers.getValueAtIndexOrDefault(this.getDataset().radius, index, this.chart.options.elements.point.radius);
|
|
|
|
point._model.backgroundColor = point.custom && point.custom.backgroundColor ? point.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().pointBackgroundColor, index, this.chart.options.elements.point.backgroundColor);
|
|
|
|
point._model.borderColor = point.custom && point.custom.borderColor ? point.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().pointBorderColor, index, this.chart.options.elements.point.borderColor);
|
|
|
|
point._model.borderWidth = point.custom && point.custom.borderWidth ? point.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().pointBorderWidth, index, this.chart.options.elements.point.borderWidth);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|