Chart.js(雷达图)每个 scaleLine 的不同 scaleLineColor [英] Chart.js (Radar Chart) different scaleLineColor for each scaleLine

查看:13
本文介绍了Chart.js(雷达图)每个 scaleLine 的不同 scaleLineColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

发件人:

收件人:

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

亲切的问候利

解决方案

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

Chart.types.Radar.extend({名称:雷达Alt",初始化:函数(数据){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;//这会绕过 originalScaleDraw 中的线条绘制this.lineWidth = 线宽;originalScaleDraw.apply(this, arguments);ctx.lineWidth = this.lineWidth;变比例=这个;//现在我们绘制Chart.helpers.each(scale.yLabels, function (label, index) {//每条径向线的颜色 - 你可以用数组查找替换它(如果你限制你的 scaleSteps)ctx.strokeStyle = "hsl(" + index/scale.yLabels.length * 360 + ", 80%, 70%)";//chart.js 代码的副本ctx.beginPath();for (var i = 0; i < scale.valuesCount; i++) {pointPosition = scale.getPointPosition(i, scale.calculateCenterOffset(scale.min + (index * scale.stepValue)));如果(我 === 0){ctx.moveTo(pointPosition.x, pointPosition.y);} 别的 {ctx.lineTo(pointPosition.x, pointPosition.y);}}ctx.closePath();ctx.stroke();});}}});

然后这样称呼它

var ctx = document.getElementById("myChart").getContext("2d");var myRadarChart = new Chart(ctx).RadarAlt(data, {比例线宽:10});//如果你有动画,这是必需的:false//myRadarChart.update();

<小时>

小提琴 -

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

解决方案

You can extend the radar chart type to do this, like so

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

and then call it like so

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/


Of course, the sane thing would be to change the lightness value instead of the hue value :-)

这篇关于Chart.js(雷达图)每个 scaleLine 的不同 scaleLineColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆