score:2
Accepted answer
You are accessing the canvas element in the created()
hook, which is called before your template is attached to the DOM.
To solve your problem you need to put your code into the mounted()
hook of your component. But still, it is not guaranteed that the template is in document. So you have to use $vm.nextTick() to ensure the DOM is ready. (source).
Also, I recommend using vue's Child component refs, which is more vue-like than document.getElementById()
(it works too).
Vue.component('message-graph', {
template: '<canvas ref="chart" width="400" height="400"></canvas>',
mounted() {
this.$nextTick(() => {
let myChart = new Chart(this.$refs.chart, {
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
}
}]
}
}
})
})
}
})
let vue = new Vue({
el: '#app',
template: '<div class="myApp"><message-graph></message-graph></div>'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.0/vue.js"></script>
<div id="app"></div>
Source: stackoverflow.com
Related Query
- Chart js plugin annotation not working in vue
- Time Series Line chart js in react not working
- Click event of stacked line chart not working
- ChartJS - radar chart options not working
- Bubble Chart in Chartjs not working
- Css style not working well when resizing chart height in angular application
- code works fine on jsfiddle but one function is not working on website
- Chartjs not working with d3 from csv source
- Error: "barWithErrorBars" is not a chart type. Vue - chartjs-chart-error-bars
- in chart js tooltip font size is not working
- Chart JS not working with date
- ChartJS not working in Vue
- Background color of the chart area in chartjs not working
- Chartjs with Vue, Bar Chart Border Radius not working
- Chart.js bubble chart pointStyle not working
- Charts.js - Colors for stacked bar on multi-series line/bar chart are not working
- Charts.js - Bar chart different colors for value intervals not working
- Chart JS not working with Phonegap
- Legend color not working with randomly generated background colors in chartjs pie chart
- Vertical align bar chart from chart.js not working
- chart js download to png with canvas.toDataURL() not working in IE and firefox
- vue-chartjs not working properly in flexbox and when resize chart goes blank
- chart.js legend not working for pie chart
- Chart JS not working with Dynamic Data
- ChartJS autoskip:False not working on line chart
- Destroying chart.js is not working when chart created inside function - chart.destroy() is not a function
- Chartjs backgroundColor for line chart does not appear in Vue app
- Chart JS tick options not working for y axis
- Trying to create a custom tooltip in a doughnut chart using chartsjs, but it is not working
- Chart js not working properly in Safari
More Query from same tag
- Chart.js - Getting data from HTML
- How to loop over Chart.js with Django list view
- Dashboard grid - Chart.JS and CSS- issue widening two small horizontal bar graphs - noob question
- Chart.js not show in my function (function used in ajax) jquery
- Chart.js Tooltip Information
- Adding multiple dataset and colors not being assigned in ng2-charts
- How to hide the points in a combined Line- and BarChart drawn by Chart.js V2.0
- I can't change the legend position in Laravel Charts & ChartJS
- dyamically constructing javascript object
- Chart.Js radar - hide label numbers
- How to hide radar chart index labels (chart.js)
- Add a custom label to the top or bottom of a stacked bar chart
- how to change the Y-axis values from float number to integer in chartjs?
- Multiple charts in background ChartJS
- chart js label not showing
- Reanimate Data Values in CharJS
- Embed <canvas> tag in Laravel Markdown?
- How to get certain text from json object value
- pass array to chart.js option
- How to change color by clicking on the chart bar?
- donut chart not rendering with csv data chart.js
- Dataset push in for loop only 1 time instead of 3
- how resetZoom() en multiple charts (ng2charts or chartjs)?
- How to apply data dynamically on initialisation for chart.js in angular?
- Chart.js plugin datalabels - formatter- Add space between thousands
- Chart.js same Y axis on left and right
- React Js and Chart Js (Line Chart)
- Chart.JS Chart top left corner is blocked by some visual nodejs
- How to customize Title position with ChartJS
- ChartJS unable to register global plugin