简体   繁体   English

Chart.js更改标签颜色

[英]Chart.js change label color

I am using chart.js library to make polar chart but not able to change the color of the label please help me how can I change the color of the label. 我正在使用chart.js库制作极坐标图,但无法更改标签的颜色,请帮助我如何更改标签的颜色。 My chart look like http://headsocial.com:8080/upload/145448587471822e0922d67c9dd6aae46d70bfeef1623.png , but I want to change the color to grey currently it is showing like orange 我的图表看起来像http://headsocial.com:8080/upload/145448587471822e0922d67c9dd6aae46d70bfeef1623.png ,但我想将颜色更改为灰色,目前显示为橙色

function show_polar_chart_data1(data, id){
        var jsonData = jQuery.parseJSON(data);
        var data = [
        {
            value: jsonData.IDENTITY_Avg,
            color: "#8258FA",
            highlight: "#8258FA",
            label: "IDENTITY("+jsonData.IDENTITY+")"
        },
        {
            value: jsonData.ROLE_Avg,
            color: "#34ED13",
            highlight: "#34ED13",
            label: "ROLE("+jsonData.ROLE+")"
        },
        {
            value: jsonData.ATTITUDE_Avg,
            color: "#FFFF00",
            highlight: "#FFFF00",
            label: "ATTITUDE("+jsonData.ATTITUDE+")"
        },
        {
            value: jsonData.AGILITY_Avg,
            color: "#FF0000",
            highlight: "#FF0000",
            label: "AGILITY("+jsonData.AGILITY+")"
        },
        {
            value: jsonData.FAIRNESS_Avg,
            color: "#00FFFF",
            highlight: "#00FFFF",
            label: "FAIRNESS("+jsonData.FAIRNESS+")"
        },
        {
            value: jsonData.CONFLICT_Avg,
            color: "#EE9A4D",
            highlight: "#EE9A4D",
            label: "CONFLICT("+jsonData.CONFLICT+")"
        }

    ];

    var ctx = document.getElementById("chart").getContext("2d");
    var polarChart = new Chart(ctx).PolarArea(data, {
        scaleOverride: true,
        scaleStartValue: 0,
        scaleStepWidth: 1,
        scaleShowLabels: false,
        scaleSteps: 10,

        onAnimationComplete: function () {
            this.segments.forEach(function (segment) {
                var outerEdge = Chart.Arc.prototype.tooltipPosition.apply({
                    x: this.chart.width / 2,
                    y: this.chart.height / 2,
                    startAngle: segment.startAngle,
                    endAngle: segment.endAngle,
                    outerRadius: segment.outerRadius * 2 + 10,
                    innerRadius: 0
                })

              var normalizedAngle = (segment.startAngle + segment.endAngle) / 2;
                while (normalizedAngle > 2 * Math.PI) {
                    normalizedAngle -= (2 * Math.PI)
                }

                if (normalizedAngle < (Math.PI * 0.4) || (normalizedAngle > Math.PI * 1.5))
                    ctx.textAlign = "start";
                else if (normalizedAngle > (Math.PI * 0.4) && (normalizedAngle < Math.PI * 0.6)) {
                    outerEdge.y += 5;
                    ctx.textAlign = "center";
                }
                else if (normalizedAngle > (Math.PI * 1.4) && (normalizedAngle < Math.PI * 1.6)) {
                    outerEdge.y - 5;
                    ctx.textAlign = "center";
                }
                else
                    ctx.textAlign = "end";

                ctx.fillText(segment.label, outerEdge.x, outerEdge.y);

            })
            done();
        }
    });

        });
    }
}

Just add 只需添加

 ctx.fillStyle = 'black';

before your ctx.fillText... 在您的ctx.fillText...之前ctx.fillText...

Have you tried adding: 您是否尝试添加:

scaleFontColor: "<the color you want to add>"

to the chart initialization like this: 像这样对图表进行初始化:

var polarChart = new Chart(ctx).PolarArea(data, {
   scaleFontColor: "<the color you want to add>"
   ...

Check this SO answer: Change label font color for a line chart using Chart.js 检查此SO答案: 使用Chart.js更改折线图的标签字体颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM