最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change label font color for a line chart using Chart.js - Stack Overflow

programmeradmin2浏览0评论

I'm using Chart.js to generate some charts. The line chart requires labels. I can't seem to figure out a way to change the color of those labels.

var chartGood = "rgba(50,182,93,0.5)";
var lineChartData = {
        labels : ["3/14","3/15","3/16","3/17","3/18","3/19","3/20","3/21","3/22","3/23"],
        datasets : [
            {
                fillColor : chartGood,
                strokeColor : "rgba(255,255,255,1)",
                pointColor : "rgba(50,182,93,1)",
                pointStrokeColor : "#fff",
                data : [12, 21, 28, 29, 31, 55, 52, 50, 49, 59]
            }
        ]
    }

var myLine = new Chart(document.getElementById("cpu-chart").getContext("2d")).Line(lineChartData);

I've tried:

labelColor : "#fff",

legend : {
    font : {
        color : "#fff"
    }
}

label : {
    font : {
        color : "#fff"
    }
}

And a few other combinations but nothing seems to work. I thought I found what I was looking for in the docs

//String - Scale label font colour  
    scaleFontColor : "#fff",

but that didn't solve my issue either.

I'm using Chart.js to generate some charts. The line chart requires labels. I can't seem to figure out a way to change the color of those labels.

var chartGood = "rgba(50,182,93,0.5)";
var lineChartData = {
        labels : ["3/14","3/15","3/16","3/17","3/18","3/19","3/20","3/21","3/22","3/23"],
        datasets : [
            {
                fillColor : chartGood,
                strokeColor : "rgba(255,255,255,1)",
                pointColor : "rgba(50,182,93,1)",
                pointStrokeColor : "#fff",
                data : [12, 21, 28, 29, 31, 55, 52, 50, 49, 59]
            }
        ]
    }

var myLine = new Chart(document.getElementById("cpu-chart").getContext("2d")).Line(lineChartData);

I've tried:

labelColor : "#fff",

legend : {
    font : {
        color : "#fff"
    }
}

label : {
    font : {
        color : "#fff"
    }
}

And a few other combinations but nothing seems to work. I thought I found what I was looking for in the docs

//String - Scale label font colour  
    scaleFontColor : "#fff",

but that didn't solve my issue either.

Share Improve this question edited Nov 16, 2015 at 23:06 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Mar 19, 2014 at 17:00 Ryan RichRyan Rich 12.1k8 gold badges23 silver badges31 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

Yes, the scaleFontColor option changes the color of labels.

You probably tried to add it to the data object, that's why it didn't work. Actually it should be passed as a second parameter of the Line function like this:

var myLine = new Chart(document.getElementById("cpu-chart").getContext("2d"))
    .Line(lineChartData, { scaleFontColor: "#ff0000" });

Edit:

Similarly, to change the font size use scaleFontSize.

Example:

scaleFontSize: 16

-- 2024 -- the new way

{ 
 options: {
    plugins: {
      legend: {
        labels: {
          color: "white",
        },
      },
    },
  },
}
发布评论

评论列表(0)

  1. 暂无评论