我想尝试使用Chart.js创建一个雷达图,它为每个scaleLine具有各种颜色,或者在scaleLines之间着色。我想知道这是否可能?

从:

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

至:

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

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

亲切的问候

最佳答案

您可以像这样扩展雷达图类型

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();

小提琴-http://jsfiddle.net/x3ftqx5r/

当然,理智的事情是更改亮度值,而不是色调值:-)

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

07-24 16:54