ChartJS beginAtZero,min,max不起作用

时间:2017-01-26 17:32:09

标签: javascript html html5 charts

我尝试了所有可能的方式,每个表单 - 答案,但任何东西都在我的代码中工作。我希望yAx从零开始,最大值为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
    },

};

Doesn't begin at zero

2 个答案:

答案 0 :(得分:3)

@Nikolas,  这是您设置的参数运作良好的小提琴。

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文档中的基本数据。

答案 1 :(得分:1)

关键是在Chart构造函数中传递选项,而不是数据的一部分

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

以上对我有用!