【问题标题】:ChartJS beginAtZero, min, max doesn't workChartJS beginAtZero、最小值、最大值不起作用
【发布时间】:2022-08-08 21:34:18
【问题描述】:

我尝试了每一种可能的方式,每一种形式的答案,但在我的代码中任何东西都有效。我希望 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
  • 在对一个模糊答案的模糊评论中,我们找到了整个互联网都缺失的答案,而 ChartJs 的思想很难找到。谢谢你。
【解决方案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 开始:

    【讨论】:

    • 它对我有用,谢谢!
    【解决方案4】:

    我尝试了不同的解决方案,并在对我有用的选项中添加 barThickness: 25

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2020-01-01
      • 2018-08-14
      相关资源
      最近更新 更多