Chartjs-Dynamic number of chart.js charts on the same page

0πŸ‘

βœ…

In the end I did included jQuery, changed canvas id... to canvas class and created a file script.js with following jQuery code where I call window.onload function for every class.

$(function($) {
    $('.chart').each(function (index, element) {
        var ctx = element.getContext('2d');
        window.myBar = new Chart(ctx, {
            type: 'bar',
            data: barChartData,
            options: {
                title: {
                    display: false,
                    text: 'Bierkonsum'
                },
                tooltips: {
                    mode: 'label'
                },
                responsive: true,
                scales: {
                    xAxes: [{
                        stacked: true
                    }],
                    yAxes: [{
                        stacked: true
                    }]
                }
            }
        });

    });
});(jQuery);

1πŸ‘

How about creating a unique ID, checking it’s not been used, using that, like so:

$keys = array();
$from = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

while ($payment = mysqli_fetch_array($r)) {
    do {
        $key = '';
        for ($i = 0; $i < 10; $i++) {
            $key .= substr($from, rand(0, strlen($from) -1), 1);
        }
    } while (in_array($key, $keys));
    $keys[] = $key;

    // use $key for the ID

}

Leave a comment