Chartjs-Inject a bar chart into a webpage via content script

0👍

After changed manifest.json to the following , it worked ! One could download the working sample here.

analysisResult=`
<canvas id="myChart"></canvas>
`

document.body.insertAdjacentHTML('afterBegin',analysisResult)


var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
            label: '购买记录',
            data: [12, 19, 3, 5, 2, 3],
             // backgroundColor: 'rgb(255, 99, 132)',
            backgroundColor: 'rgba(54, 162, 235, 0.2)',
            borderColor:'rgba(54, 162, 235, 1)',
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }
});

Leave a comment