Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js is always increasing height on chart.resize()

I added chart.resize() inside of addData and removeData function, but it is always increasing height and it is never decreasing!

I am updating the chart when the user click on item of the menu. Some items have more than 100 datas e some others items have only 2 data and when have few data, the chart is always increasing height:

horizontalBar

Why is this happening?

<canvas id="chart"></canvas>
var chartData = {
            labels: [<?php echo '"'.implode('","',  $chart_labels ).'"' ?>],
            datasets: [
                {
                    data: [<?php echo implode(',',  $chart_numbers ) ?>],
                    backgroundColor: '#184b8f',
                    hoverBackgroundColor: '#ef3e42'
                }
            ]
        };

        var barOptions = {
            responsive: true,
            maintainAspectRatio: false,
            tooltips: {
                enabled: false
            },
            hover: {
                animationDuration: 0
            },
            legend: {
                display: false
            },
            scales : {
                xAxes : [{
                    gridLines : {
                        display: false
                    },
                    ticks: {
                        beginAtZero: true,
                        color: '#cfcfcf'
                    }
                } ],
                yAxes : [ {
                    gridLines : {
                        display: false
                    },
                    ticks: {
                        fontSize: 13,
                        fontFamily: "'Open Sans'",
                        fontWeight: 600,
                    }
                }]
            },
            animation: {
            duration: 500,
            easing: "easeOutQuart",
            }
        };

        function removeData(chart) {
            chart.data.labels.pop();
            chart.data.datasets.forEach((dataset) => {
                dataset.data.pop();
            });
            chart.resize();
            chart.update();
        }

        function addData(chart, label, data) {
            chart.data.labels.push(label);
            chart.data.datasets.forEach((dataset) => {
                dataset.data.push(data);
            });
            chart.resize();
            chart.update();
        }

        var ctx = document.getElementById("chart").getContext("2d");
        var myBar = new Chart(ctx, {
                        type: 'horizontalBar',
                        data: chartData,
                        options: barOptions
                    });
like image 675
Maicon Furtado Avatar asked Aug 01 '17 00:08

Maicon Furtado


People also ask

How to make a chart have a fixed height?

To make this work correctly you must remove the embedded (and unnecessary) style="display: block" Instead define a class for the enclosing div, and define its height in CSS. Once you do that, the chart should have responsive width but fixed height.

How to resize charts when printing using CSS media queries?

CSS media queries allow changing styles when printing a page. The CSS applied from these media queries may cause charts to need to resize. However, the resize won't happen automatically. To support resizing charts when printing, you need to hook the onbeforeprint event and manually trigger resizing of each chart. Copied!

How to set the height of the parent element of chart?

Now, what you can do is set the height of the parent element of the chart, so here div .chart is the parent of div #myChart . So, specifying the height of div .chart should just work fine. Consider the following code. <div class="chart" style="height: 500px;"> <canvas id="myChart"></canvas> </div>

How to set the size of a chartjs diagram?

Setting the Size of a chartjs diagram can be simply achieved by setting the with of a surrounding container: <div style="width:400px;"> <canvas id="myChart"></canvas> </div> But an important detail is, that you may need to resize your diagram after creation:


2 Answers

I had the same thing. You have to put an empty div around the canvas.

like image 160
idontknow Avatar answered Oct 19 '22 13:10

idontknow


I've found some users had the same issue in this post: https://github.com/jtblin/angular-chart.js/issues/84

The solution for me was to change the chart's options to maintainAspectRatio: false.

    myChart.setOptions({
        responsive: true,
        maintainAspectRatio: false
    });
like image 3
Mikel Granero Avatar answered Oct 19 '22 13:10

Mikel Granero