简体   繁体   English

更改标签颜色Y和X轴chart.js

[英]Change label color Y and X axis chart.js

I have tried to change to change the chart label colour to white for the Y and X axis. 我试图将Y和X轴的图表标签颜色更改为白色。 I tried to add the code with fontColour from other threads here on stackoverflow but won't get it to work. 我试图在此处从stackoverflow上的其他线程中添加带有fontColour的代码,但无法正常工作。

Here is my code: 这是我的代码:

  var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
  var lineChartData = {
    labels : ['January','February','March','April','May','June','July'],
    datasets : [
      {
        label: 'My First dataset',
        fontColor : '#fff' ,
        backgroundColor : 'rgba(220,220,220,0.2)',
        borderColor : 'rgba(220,220,220,1)',
        pointBackgroundColor : 'rgba(220,220,220,1)',
        pointBorderColor : '#fff',
        data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
      }
    ]
  }

  var options = {
    maintainAspectRatio: false,
    legend: {
      fontColor: "white",
    },
    scales: {
      xAxes: [{
        ticks: {
          fontColor: "white",
        }
      }],
      yAxes: [{
        ticks: {
          fontColor: "white",
          beginAtZero: true,
          maxTicksLimit: 5,
          stepSize: Math.ceil(250 / 5),
          max: 250
        }
      }]
    }
  };
  var ctx = document.getElementById('canvas-1');
  var chart = new Chart(ctx, {
    type: 'line',
    data: lineChartData,
    options: {
      responsive: true
    }
  });

You need to assign the options object to options property, when constructing the chart. 构造图表时,需要将options对象分配给options属性。

 var randomScalingFactor = function() { return Math.round(Math.random() * 100) }; var lineChartData = { labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'], datasets: [{ label: 'My First dataset', fontColor: '#fff', backgroundColor: 'rgba(220,220,220,0.2)', borderColor: 'rgba(220,220,220,1)', pointBackgroundColor: 'rgba(220,220,220,1)', pointBorderColor: '#fff', data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()] }] } var options = { responsive: true, maintainAspectRatio: false, legend: { fontColor: "white", }, scales: { xAxes: [{ ticks: { fontColor: "white", } }], yAxes: [{ ticks: { fontColor: "white", beginAtZero: true, maxTicksLimit: 5, stepSize: Math.ceil(250 / 5), max: 250 } }] } }; var ctx = document.getElementById('canvas-1'); var chart = new Chart(ctx, { type: 'line', data: lineChartData, options: options //<- assign this }); 
 canvas { background: #111 } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script> <canvas id="canvas-1"></canvas> 

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

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