admin 管理员组

文章数量: 1184232

I tried every possible way, every form-answer but anything works inmy code. I want yAxes begin at zero and max value is 100 but all my chart begin with other values (see pic). What can I do?

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
    },

};

I tried every possible way, every form-answer but anything works inmy code. I want yAxes begin at zero and max value is 100 but all my chart begin with other values (see pic). What can I do?

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
    },

};

Share Improve this question edited Jan 26, 2017 at 19:31 Nikolas asked Jan 26, 2017 at 17:32 NikolasNikolas 1881 gold badge2 silver badges10 bronze badges 1
  • 1 A js fiddle will help... please add. – spooky Commented Jan 26, 2017 at 19:34
Add a comment  | 

5 Answers 5

Reset to default 17

key is to pass options in the Chart constructor instead of part of data

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

above works for me!

Working for version 3.2.0:

  var options = {
  // plugins, responsive etc...

      scales: {
          y: {  
             min: 0
             }
          },

  //tooltips specifications...
  }

The y-axis will start at 0:

@Nikolas, Here is the fiddle where the parameters you set works well.

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
});

Please check and see what you are doing wrong. I used the basic data from charjs documentation.

I tried different solutions and adding barThickness: 25 in options worked for me.

I tested this code recently with version 4 and it worked perfectly :

new Chart(document.getElementById("line-chart"), {
    type: 'line',
    data: {
        labels: [], // SAMPLE LABELS
        datasets: [{
            data: [], // SAMPLE DATA
            label: "",
            borderColor: "#3cba9f",
            fill: false
        }]
    },
    options: {
        scales: {
            y: {
                beginAtZero: true, // Set this to true to start the Y-axis at 
                max: 100
            },
            x: {
                // If you want to configure the X-axis, you can do it here
            }
        },
        title: {
            display: true,
            text: ''
        }
    }
});
 <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js"></script>

本文标签: javascriptChartJS beginAtZero min max doesn39t workStack Overflow