javascript - Chart.js 标签颜色

标签 javascript chart.js

我正在使用 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
                }
            }]
        }
    }
});

如有任何帮助,我们将不胜感激。

最佳答案

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

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>

关于javascript - Chart.js 标签颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37292423/

相关文章:

charts - Chartjs 堆叠栏为所有堆叠提供单独的工具提示

javascript - 从 setTimeout 函数中返回值

javascript - api返回的React js多个组件

javascript - 如何使用javascript获取上一个和下一个兄弟id

javascript - Chart.js - 根据某个 y 值绘制水平线

javascript - Chartjs v2.7 - 具有时间序列(财务)数据的组合图;条形宽度不规则

javascript - Vue.js - 组件数据不更新

javascript - 表单验证和 Node JS - 避免厄运金字塔

javascript - 图表JS不显示图表

javascript - 实时更新 chart.js 条形图