I would like to try and create a radar chart using Chart.js which has various colours for each scaleLine, or coloured between the scaleLines. I was wondering if this was possible?
From:
To:
I currently have a working graph, though there doesn’t seem to be a method to change individual scale lines.
Kind Regards Leigh
Advertisement
Answer
You can extend the radar chart type to do this, like so
JavaScript
x
38
38
1
Chart.types.Radar.extend({
2
name: "RadarAlt",
3
initialize: function (data) {
4
Chart.types.Radar.prototype.initialize.apply(this, arguments);
5
6
var originalScaleDraw = this.scale.draw;
7
var ctx = this.chart.ctx;
8
this.scale.draw = function () {
9
var lineWidth = this.lineWidth;
10
// this bypasses the line drawing in originalScaleDraw
11
this.lineWidth = lineWidth;
12
13
originalScaleDraw.apply(this, arguments);
14
15
ctx.lineWidth = this.lineWidth;
16
var scale = this;
17
// now we draw
18
Chart.helpers.each(scale.yLabels, function (label, index) {
19
// color of each radial line - you could replace this by an array lookup (if you limit your scaleSteps)
20
ctx.strokeStyle = "hsl(" + index / scale.yLabels.length * 360 + ", 80%, 70%)";
21
22
// copy of the chart.js code
23
ctx.beginPath();
24
for (var i = 0; i < scale.valuesCount; i++) {
25
pointPosition = scale.getPointPosition(i, scale.calculateCenterOffset(scale.min + (index * scale.stepValue)));
26
if (i === 0) {
27
ctx.moveTo(pointPosition.x, pointPosition.y);
28
} else {
29
ctx.lineTo(pointPosition.x, pointPosition.y);
30
}
31
}
32
ctx.closePath();
33
ctx.stroke();
34
});
35
}
36
}
37
});
38
and then call it like so
JavaScript
1
8
1
var ctx = document.getElementById("myChart").getContext("2d");
2
var myRadarChart = new Chart(ctx).RadarAlt(data, {
3
scaleLineWidth: 10
4
});
5
6
// this is requried if you have animation: false
7
// myRadarChart.update();
8
Fiddle – http://jsfiddle.net/x3ftqx5r/
Of course, the sane thing would be to change the lightness value instead of the hue value 🙂