I have been experimenting with chart.js
and vue.js
.
The component I created is called MessageGraph
, and it is structured like this (with data extracted from the documentation):
<template>
<canvas id="myChart" width="400" height="400"></canvas>
</template>
<script>
import Chart from 'chart.js';
export default {
created() {
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>
Upon running the code, I encountered this error message in the console:
app.c47f281….js:71222 [Vue warn]: Error in created hook:
(found in <MessageGraph> at /Users/Janssen/Code/forumv2/resources/assets/js/components/graph/MessageGraph.vue)
warn @ app.c47f281….js:71222
handleError @ app.c47f281….js:72107
callHook @ app.c47f281….js:72939
Vue._init @ app.c47f281….js:74420
...
TypeError: Cannot read property 'length' of null
at Object.acquireContext (app.c47f281….js:14468)
at new Chart.Controller (app.c47f281….js:7776)
at new Chart (app.c47f281….js:10193)
at VueComponent.created (app.c47f281….js:2106)
...
To incorporate the component into my app.js file, I added the following line:
Vue.component('messageGraph', require('./components/graph/MessageGraph.vue'));
In my app.blade file, I included the following snippet:
<message-graph></message-graph>
Can you identify what might be causing this issue?