gpt4 book ai didi

javascript - Chart.js(雷达图)每个 scaleLine 的不同 scaleLineColor

转载 作者:行者123 更新时间:2023-11-29 14:45:43 25 4
gpt4 key购买 nike

我想尝试使用 Chart.js 创建一个雷达图,每个 scaleLine 都有不同的颜色,或者在 scaleLine 之间有颜色。我想知道这是否可能?

来自:

enter image description here

收件人:

enter image description here

我目前有一个工作图,但似乎没有改变个别比例线的方法。

亲切的问候利

最佳答案

你可以像这样扩展雷达图类型来做到这一点

Chart.types.Radar.extend({
name: "RadarAlt",
initialize: function (data) {
Chart.types.Radar.prototype.initialize.apply(this, arguments);

var originalScaleDraw = this.scale.draw;
var ctx = this.chart.ctx;
this.scale.draw = function () {
var lineWidth = this.lineWidth;
// this bypasses the line drawing in originalScaleDraw
this.lineWidth = lineWidth;

originalScaleDraw.apply(this, arguments);

ctx.lineWidth = this.lineWidth;
var scale = this;
// now we draw
Chart.helpers.each(scale.yLabels, function (label, index) {
// color of each radial line - you could replace this by an array lookup (if you limit your scaleSteps)
ctx.strokeStyle = "hsl(" + index / scale.yLabels.length * 360 + ", 80%, 70%)";

// copy of the chart.js code
ctx.beginPath();
for (var i = 0; i < scale.valuesCount; i++) {
pointPosition = scale.getPointPosition(i, scale.calculateCenterOffset(scale.min + (index * scale.stepValue)));
if (i === 0) {
ctx.moveTo(pointPosition.x, pointPosition.y);
} else {
ctx.lineTo(pointPosition.x, pointPosition.y);
}
}
ctx.closePath();
ctx.stroke();
});
}
}
});

然后像这样调用它

var ctx = document.getElementById("myChart").getContext("2d");
var myRadarChart = new Chart(ctx).RadarAlt(data, {
scaleLineWidth: 10
});

// this is requried if you have animation: false
// myRadarChart.update();

fiddle - http://jsfiddle.net/x3ftqx5r/


当然,明智的做法是更改亮度值而不是色相值:-)

enter image description here

关于javascript - Chart.js(雷达图)每个 scaleLine 的不同 scaleLineColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com