本文介绍了Chart.js带有客户X轴标签的气泡图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,我可以将X轴标签自定义为单词而不是数字刻度吗?
我尝试在我的数据中插入labels属性,但它不起作用。

Is there anyway I can customize the X-axis label to words instead of numbers scale?I tried inserting the labels attribute in my data, but it doesn't work.

这是我的代码:

var bubbleChartData = 
    {

        datasets: [
        {
            label: "My First dataset",
            backgroundColor: randomColor(),
            data: [4, 2, 3, 4, 5]
        }],
        labels: [
            "Bad",
            "Average",
            "Good",
            "Very Good",
            "Perfect"
        ]   
    };

    window.onload = function() {
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myChart = new Chart(ctx, {
            type: 'bubble',
            data: bubbleChartData,
            xAxisID: 'testing',
            options: {

                responsive: true,
                title:{
                    display: true,
                    text:'Chart.js Bubble Chart'
                },


            }
        });
    };

这就是我得到的:

推荐答案

将xAxes的回调用于您的选项:

Use a callback for xAxes into your options:

options: {
  responsive: true,
  title:{
      display: true,
      text:'Chart.js Bubble Chart'
  },    
  scales: {
    xAxes: [{
      ticks: {
        callback: function(value, index, values) {
          return 'value is ' + value;
        }
      }
    }]
  }
}

这篇关于Chart.js带有客户X轴标签的气泡图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 14:50