简体   繁体   English

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

[英]Chart.js, change color of specific ticks

I have a chart that ticks are barely distinguishable.我有一个图表,刻度几乎无法区分。

Is there a way to target a specific ticks and change the color, font-size or background, to appear more visible?有没有办法针对特定刻度并更改颜色、字体大小或背景,以使其更显眼?

Solution I could also set for, would be way to hide every tick that indicates hours but leave the tool-tip.我也可以设置的解决方案是隐藏指示小时的每个刻度但保留工具提示的方法。

I've tried to push empty string in place of time, but that unfortunately also disables tool-tips and there is no way to tell the time of particulate value.我试图用空字符串代替时间,但不幸的是,这也禁用了工具提示,并且无法告诉微粒值的时间。

( chart.js ver2.8.0) (chart.js ver2.8.0) 在此处输入图片说明

CODE:代码:

 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,
},
});

you can use a array for xAxes fontColor see example:您可以为 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>

Here is a fiddle这是一个小提琴

For Chart JS 3.5.1 I've got this in my config:对于 Chart JS 3.5.1,我的配置中有这个:

{
    options: {
        scales: { 
            x: {
                ticks: {
                    color: ['black', 'black', 'blue']
                }
            }
        }
    }
}

I'm using TypeScript and it looks like the typing is incorrect so I had to // @ts-igone it so it can accept a list of strings.我正在使用 TypeScript,看起来输入不正确,所以我不得不// @ts-igone它,以便它可以接受字符串列表。

Here is the result:结果如下:

result of providing a list of strings for color提供颜色字符串列表的结果

Hopefully this will prove useful for someone in the future but this is the way I was able to do it:希望这对将来的某人有用,但这是我能够做到的方式:

scales: {
      x: {
        type:     'linear',
        position: 'bottom',
        title: {
          text:    'Minutes from start',
          display: true
        }   
      },
      y: {
        type: 'linear',
        display: true,
        position: 'left',
        suggestedMax: 14,
        title: {
          text:    'pH',
          display: true
        },
        ticks: {
          color: (c) => {if(c['tick']['value'] == 7) return 'red'; else return 'black';}, // this here
        },
      },

In the ticks section of a scale you can create a function in the color property that'll return a color based on the tick在刻度的刻度部分,您可以在 color 属性中创建一个函数,该函数将根据刻度返回颜色

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

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