Chart.js,更改特定刻度的颜色

时间:2019-11-26 13:58:21

标签: javascript chart.js

我有一个图表,刻度几乎无法区分。

有没有一种方法可以定位特定的刻度线,并更改颜色,字体大小或背景,使其看起来更明显?

我还可以设置解决方案,那就是隐藏每个指示小时的刻度线,但保留工具提示。

我尝试将空字符串替换为时间,但不幸的是,这也禁用了工具提示,并且无法告知时间颗粒值。

( chart.js ver2.8.0) enter image description here

代码:

 var chartDatesArray = ["NOV 25","01:00","02:00","03:00"... "23:00","Nov 26"];
 var chartCountArray = [10,20,30,40...50,60];

var myChart = new Chart(ctx, {
type: 'line',
data: {
    labels: chartDatesArray,
    datasets: [
        {
            data: chartCountArray,
            backgroundColor: gradient,
            borderColor: "rgb(27,163,198)",
            fill: true,
            lineTension: 0,
            radius: 1,
            borderWidth: 2,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 1,
            pointHitRadius: 10,
        },
    ],
},
options: {


    scales: {
        xAxes: [{
            ticks: {
                display: true,
                fontSize: 12,
            },
            gridLines: {
                display: true,
                drawBorder: false,
            }
        }],
        yAxes: [{
            ticks: {
                precision: 0,
                display: true,
                fontSize: 12,
                beginAtZero: true
            },
            gridLines: {
                display: true,
                drawBorder: true,
            }
        }]
    },

    legend: {
        display: false,
        labels: {
            fontSize: 12,
        },

    },
    tooltips: {

        enabled: true,
        intersect: false,
    },
    display: true,
    responsive: true,
    maintainAspectRatio: false,
},
});

1 个答案:

答案 0 :(得分:1)

您可以为xAxes fontColor使用数组,请参见示例:

<canvas id="graph-canvas" width="800" height="400"></canvas>

    <script>
  var canvas = document.getElementById('myChart');
var data = {
    labels: ["January", "February", "March", "April", "May", "June"],
    datasets: [
        {
            label: "My First dataset",
            backgroundColor:
            [ "rgba(255,99,132,0.4)",
            "rgba(255,99,132,0.4)",
            "rgba(255,99,132,0.4)",
            "rgba(25,25,255,0.4)",
            "rgba(25,25,255,0.4)",
            "rgba(25,25,255,0.4)"],
            hoverBorderColor: "rgba(255,99,132,1)",
            data: [65, 59, 20, 81, 56, 55],
        }
    ]
};
var option = {
     scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true,
                    fontColor: 'red'
                },
            }],
          xAxes: [{
                ticks: {
                    fontColor:['green','red','blue','yellow','green','red'],
                    callback: function(value, index, values) {
                    if(index === 3) {
                      return '**'+value+'**';
                    } 
                    else {
                        return value;
                        }
                    }
                },
            }]
  }
};

var myBarChart = Chart.Line(canvas,{
    data:data,
  options:option
});

</script>

这里是see mysql doc here