javascript Chart.js标签颜色

ds97pgxw  于 2023-02-07  发布在  Java
关注(0)|答案(3)|浏览(135)

我正在使用chart.js创建一个条形图,似乎无法更改标签颜色或图例颜色。我知道如何更改刻度颜色,但我不确定将"scaleFontColor"放在哪里,如果这确实是我需要使用的。
这里有一个链接,它现在看起来像。http://imgur.com/nxaH1mk
下面是我的代码:

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    scaleFontColor: "white",
    type: "bar",
    data: {
        labels: <?php echo json_encode($timeSlice); ?>, 
        datasets: [{
            label: "A Label",
            backgroundColor: "rgba(159,170,174,0.8)",
            borderWidth: 1,
            hoverBackgroundColor: "rgba(232,105,90,0.8)",
            hoverBorderColor: "orange",
            scaleStepWidth: 1,
            data: <?php echo json_encode($myCount); ?>
        }]
    },
    options: {
        legend: {
            fontColor: "white"
        },
        scales: { 
            yAxes: [{
                ticks: {
                    fontColor: "white",
                    stepSize: 1,
                    beginAtZero: true
                }
            }]
        }
    }
});

任何帮助都将不胜感激。

8fq7wneg

8fq7wneg1#

啊,我解决了,很抱歉问你这个问题。但是我想我会留下一个答案,以防其他人遇到我的问题。

var ctx = document.getElementById("barChart");
    var myChart = new Chart(ctx, {
        type: "bar",
        data: {
            labels: ["label 1", "label 2"], 
            datasets: [{
                label: "My Label",
                backgroundColor: "rgba(159,170,174,0.8)",
                borderWidth: 1,
                hoverBackgroundColor: "rgba(232,105,90,0.8)",
                hoverBorderColor: "orange",
                scaleStepWidth: 1,
                  data: [4, 5]
            }]
        },
        options: { 
            legend: {
                labels: {
                    fontColor: "blue",
                    fontSize: 18
                }
            },
            scales: {
                yAxes: [{
                    ticks: {
                        fontColor: "green",
                        fontSize: 18,
                        stepSize: 1,
                        beginAtZero: true
                    }
                }],
                xAxes: [{
                    ticks: {
                        fontColor: "purple",
                        fontSize: 14,
                        stepSize: 1,
                        beginAtZero: true
                    }
                }]
            }
        }
    });
<!-- Edit:
   chart.js recently released new version v3.x
   which is not backwards compatible with v2.x
   -->

<!--<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>-->

   <!-- above link gets you latest version of chart.js (at v3.3.2 now)
        hence snippet didn't work propperly anymore :-(
   -->

<script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.4"></script>

   <!-- above link gets you v2.9.4
        and snippet works agian  :-)
   -->

<div>
    <canvas id="barChart" height="140"></canvas>
</div>
plicqrtu

plicqrtu2#

来自@PhantomSalt的好问题和好答案
他的答案与....... chart.jsv2.xx配合得非常好
我修改了他的好代码,使之可以与.. chart.jsv3.xx一起工作
v3.xxv2.xx不向后兼容)

// var ctx = document.getElementById("barChart")
const ctx = document.getElementById("barChart").getContext("2d"); // added '.getContext("2d")'

const myChart = new Chart(ctx, {
  type: "bar",
  data: {
    labels: ["label 1", "label 2"],
    datasets: [{
      label: "My Label",
      backgroundColor: "rgba(159,170,174,0.8)",
      borderWidth: 1,
      hoverBackgroundColor: "rgba(232,105,90,0.8)",
      hoverBorderColor: "orange",
      scaleStepWidth: 1,
      data: [4, 5]
    }]
  },
  options: {
    plugins: {  // 'legend' now within object 'plugins {}'
      legend: {
        labels: {
          color: "blue",  // not 'fontColor:' anymore
          // fontSize: 18  // not 'fontSize:' anymore
          font: {
            size: 18 // 'size' now within object 'font {}'
          }
        }
      }
    },
    scales: {
      y: {  // not 'yAxes: [{' anymore (not an array anymore)
        ticks: {
          color: "green", // not 'fontColor:' anymore
          // fontSize: 18,
          font: {
            size: 18, // 'size' now within object 'font {}'
          },
          stepSize: 1,
          beginAtZero: true
        }
      },
      x: {  // not 'xAxes: [{' anymore (not an array anymore)
        ticks: {
          color: "purple",  // not 'fontColor:' anymore
          //fontSize: 14,
          font: {
            size: 14 // 'size' now within object 'font {}'
          },
          stepSize: 1,
          beginAtZero: true
        }
      }
    }
  }
});
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <!-- gets you the latest version of Chart.js, now at v3.3.2 -->

<div>
  <canvas id="barChart" height="140"></canvas>
</div>
vdzxcuhz

vdzxcuhz3#

这对我改变标签颜色起作用

data:{
 datasets:[{
  color: '#232323'
 }]
}

相关问题