Search code examples
chart.jspyramid

Using Chart.js with Pyramid Web Framework


Please could you help me with the setup required to show a chart using chart.js with Pyramid web framework (chameleon template)

In the

<head>

I'm pointing to the files I downloaded from chartjs.org:

/static/npm/node_modules/chart.js/dist/Chart.css

and

/static/npm/node_modules/chart.js/dist/Chart.js

I'm trying their example below in a template, but no chart is displayed:

<canvas id="myChart" width="400" height="400"></canvas>
<script>
var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            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
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});
</script>

Solution

  • Check the browser console to make sure that the .css and .js load and do not 404. You need to define a static view. See Pyramid docs for more information.