Error trying to switch between different charts

6 views
Skip to first unread message

cosmos multi

unread,
Dec 18, 2020, 8:43:36 AM12/18/20
to Django users
I am developing an application which shows the data created in a chartjs graph, what I am doing it does well, since all the data is displayed in the way I want, but I have a problem and that is that now I am trying to do that according to the type of graph that a user wants, this is changed, the problem is that the graph is changed but in case of having multiple graphs only the first graph is changed, the others continue with the graph by default.
<canvas id="{{ project.slug }}" width="400" height="400"></canvas>

<script type="text/javascript">
    var faltante = 100 - {{ project.porcent }};

    var data = {
        labels: ['{{ project.name }}', 'Falta'],
        datasets: [{
            data: [{{ project.porcent }}, faltante],
            backgroundColor: ['#252850', '#f44611'],
        }],
    };

    var ctx = document.getElementById('{{ project.slug }}').getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'pie',
        data: data
    });
    
    var chartType = document.getElementById('chartType');
    
    chartType.addEventListener('change', () => {
        myChart.destroy();
        myChart = new Chart(ctx, {
            type: chartType.value,
            data: data
        });
    });
</script>

Reply all
Reply to author
Forward
0 new messages