Chartjs-Adapting a JavaScript code to be dynamic

0👍

If you format your dropdown box as such: (so the user_id in the value of each option)

<select name="pick-chart" class="pick-chart">
    <option value="6">Raphael</option>
    ...
</select>

You could rewrite your pick-chart.change function to something like:

$(".pick-chart").change(function (e) {
    e.preventDefault();
    var val = $(this).val();
    if (val != 0) {
        $.ajax({
            method: "GET",
            url: endpoint,
            success: function(data){
                console.log(data)
                //Labels coming from website.views//

                info_array = data.info_array
                current_user = data.current_user
                config.data.datasets[0].data = info_array[val];
                config.data.datasets[1].data = info_array[current_user];
                mainChart.update();
            }
        });
    } else {
        config.data.datasets[0].data = [];
    }
    mainChart.update();
});

Assuming your data really is dynamic and could change while the user stays at your page. Also assuming that you want to compare current_user (which should also be a user_id) in dataset 1, like your fixed data, to a selected other user in dataset 0.

Leave a comment