【问题标题】:ChartJS beginAtZero, min, max doesn't workChartJS beginAtZero,min,max 不起作用
【发布时间】:2017-01-26 17:32:09
【问题描述】:

我尝试了所有可能的方法,每个表单答案,但任何东西都可以在我的代码中使用。我希望 yAxes 从零开始,最大值为 100,但我的所有图表都以其他值开始(见图)。我能做什么?

var options = {
    responsive: true,
    scales: {
        yAxes: [{
            display: true,
            ticks: {
                beginAtZero: true,
                max: 100,
                min: 0
            }
        }]
    },
    title: {
        display: true,
        text: name
    },
    tooltips: {
        mode: 'index',
        intersect: false,
    },
    hover: {
        mode: 'nearest',
        intersect: true
    },

};

【问题讨论】:

  • 一个 js fiddle 会有所帮助...请添加。

标签: javascript html charts


【解决方案1】:

关键是在图表构造函数中传递选项而不是部分数据

new Chart(ctx, {
    type: 'bar',
    data: {{ chart_data|safe }},
    options: {
        scales: {
            yAxes: [{
                display: true,
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});

以上对我有用!

【讨论】:

  • 请记住,beginAtZero 必须高于图表 3.5.1 中的“ticks”。上面的代码是图表2
【解决方案2】:

@尼古拉斯, 这是您设置的参数运行良好的小提琴。

https://jsfiddle.net/shemdani/zkb215up/2/

    var options = {
    responsive: true,
    scales: {
        yAxes: [{
            display: true,
            ticks: {
                beginAtZero: true,
                max: 100,
                min: 0
            }
        }]
    },
    title: {
        display: true,
        text: name
    },
    tooltips: {
        mode: 'index',
        intersect: false,
    },
    hover: {
        mode: 'nearest',
        intersect: true
    },

};

var data = {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [32, 59, 36, 25, 68, 71],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    }

var ctx = document.getElementById("myChart");

var chartInstance = new Chart(ctx, {
    type: 'line',
    data: data,
    options:options
});

请检查并查看您做错了什么。我使用了 charjs 文档中的基本数据。

【讨论】:

    【解决方案3】:

    适用于 3.2.0 版:

      var options = {
      // plugins, responsive etc...
    
          scales: {
              y: {  
                 min: 0
                 }
              },
    
      //tooltips specifications...
      }
    

    y 轴将从 0 开始:

    【讨论】:

    • 它对我有用,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多