javascript - 更改 X 和 Y 轴字体颜色

标签 javascript chart.js

如何更改 X 轴和 Y 轴标签的颜色? 我试图在 scaleLabel 中使用 fontColor 但我可能用错了地方?

我尝试在 scale 中将其应用为 can be found on the source code .我还在 scales 下甚至在 xAxes 下尝试过。

var options = {
type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: 'red',
            borderWidth: 1
        }]
    },
    options: {
        scale: {
            scaleLabel:{
                fontColor: 'red'
            }
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
 };

Reproduction online

enter image description here

我一直在检查 the documentation但对我来说似乎不是很清楚。 而且由于 chart.js 没有提供足够的示例,所以有时很难找到东西......

最佳答案

$(function(){
var ctx = document.getElementById("myChart");
//Chart.defaults.global.defaultFontColor='red';
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
       legend:{labels: {fontColor: 'orange'}},
      title: {
            display: true,
            fontColor: 'blue',
            text: 'Custom Chart Title'
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true,
                    fontColor: 'red'
                },
            }],
          xAxes: [{
                ticks: {
                    fontColor: 'green'
                },
            }]
        } 
        
    }
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>

您可以在 ticks/label/legend:labels 中为特定轴使用 fontColor

options: {
        legend: {
             labels: {
                  fontColor: 'orange'
                 }
              },
        title: {
            display: true,
            fontColor: 'blue',
            text: 'Custom Chart Title'
        }     ,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true,
                    fontColor: 'red'
                },
            }],
          xAxes: [{
                ticks: {
                    fontColor: 'green'
                },
            }]
        } 

    }

或更改defaultFontColor 以更改在 Canvas 上绘制的整个文本元素的字体颜色。

 Chart.defaults.global.defaultFontColor='red';

关于javascript - 更改 X 和 Y 轴字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39723147/

相关文章:

Javascript 和 3 参数过滤器

javascript - 用于基于索引动态创建时间的 For 循环逻辑

chart.js - ChartJs 条形图 - 保持条形向左而不是在宽度上均匀分布

javascript - Chart.js:4 Uncaught ReferenceError: require 未在 ionic 2 中定义

javascript - html5可拖动隐藏原始元素

javascript - iE8 中奇怪的网页问题...可能是 javascript

javascript - 在项目中添加 angular postinstall "ngcc..."脚本的原因是什么?

chart.js - 带间隙的水平堆积条

javascript - Chartjs 图例样式

javascript - Chart.js 极地面积图。将标签更改为 45 度 Angular