简体   繁体   English

Chartjs 在回调函数中更改 x 轴的特定标签颜色

[英]Chartjs change the specific label color in x axis in callback function

i develop the program that show some data in bar chart, i use chart.js and config my data, i just want to customize the labels and make specific label bold and color red我开发了在条形图中显示一些数据的程序,我使用chart.js并配置我的数据,我只想自定义标签并使特定标签加粗和红色

i use callback function in (ticks) property to access value of labels我在 (ticks) 属性中使用回调函数来访问标签的值

  xAxes: [{
      ticks: {
          fontFamily: "IRANSans",
          fontSize: 16,
          autoSkip: false,
          callback: function(value) {
              if (value === 'some_value') {
                  return (make color red)
              } else {
                  return (make color blue)
              }
          }
      }
  }]

when the condition equal to some_value i just want to change the color of label please help me to correct my code :) thanks alot:)当条件等于 some_value 时,我只想更改标签的颜色,请帮我更正我的代码:) 非常感谢:)

You can make use of the Plugin Core API .您可以使用插件核心 API It offers different hooks that may be used for executing custom code.它提供了可用于执行自定义代码的不同钩子。 In below code snippet, I use the afterDraw hook to draw text with the desired styles underneath each bar.在下面的代码片段中,我使用afterDraw钩子在每个条形下方绘制具有所需样式的文本

When drawing your own tick labels, you need to instruct Chart.js not to display the default labels.绘制自己的刻度标签时,需要指示 Chart.js 不显示默认标签。 This can be done through the following definition inside the chart options.这可以通过图表选项中的以下定义来完成。

scales: {
  xAxes: [{
    ticks: {
      display: false
    }
  }], 

You also need to define some padding for the bottom of the chart, otherwise you won't see your custom tick labels.您还需要为图表底部定义一些填充,否则您将看不到自定义刻度标签。

layout: {
  padding: {
    bottom: 20
  }
},

Please take a look at the following sample code that illustrates how to change the labels on the x-axis depending on the values.请查看以下示例代码,该代码说明了如何根据值更改 x 轴上的标签。

 new Chart('myChart', { type: 'bar', plugins: [{ afterDraw: chart => { var ctx = chart.chart.ctx; var xAxis = chart.scales['x-axis-0']; var yAxis = chart.scales['y-axis-0']; ctx.save(); ctx.textAlign = 'center'; ctx.font = '12px Arial'; chart.data.labels.forEach((l, i) => { var value = chart.data.datasets[0].data[i]; var x = xAxis.getPixelForValue(l); ctx.fillStyle = value == 60 ? 'red' : 'blue'; ctx.fillText(value, x, yAxis.bottom + 17); }); ctx.restore(); } }], data: { labels: ["January", "February", "March", "April", "May", "June", "July"], datasets: [{ label: "My First Dataset", data: [60, 59, 80, 81, 60, 55, 40], fill: false, backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)", "rgba(75, 192, 192, 0.2)", "rgba(54, 162, 235, 0.2)", "rgba(153, 102, 255, 0.2)", "rgba(201, 203, 207, 0.2)"], borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)", "rgb(153, 102, 255)", "rgb(201, 203, 207)"], borderWidth: 1 }] }, options: { layout: { padding: { bottom: 20 } }, scales: { xAxes: [{ ticks: { display: false } }], yAxes: [{ ticks: { beginAtZero: true } }] } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script> <canvas id="myChart" height="90"></canvas>

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

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