npm install chart.js --save
Last active
June 5, 2019 10:10
-
-
Save bedus-creation/21fce27bda11495b95bb74df2ba6f5ef to your computer and use it in GitHub Desktop.
Setup chart Js in vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const planetChartData = { | |
type: 'pie', | |
data: { | |
labels: ['Mercury', 'Venus'], | |
datasets: [ | |
{ | |
label: 'Number of Moons', | |
data: [10, 20], | |
backgroundColor: [ | |
'#51cda0', // Blue | |
'#df7970', | |
], | |
borderColor: [ | |
'#36495d', | |
'#36495d', | |
], | |
borderWidth: 3 | |
} | |
] | |
}, | |
options: { | |
responsive: true, | |
lineTension: 1, | |
scales: { | |
yAxes: [{ | |
ticks: { | |
beginAtZero: true, | |
padding: 25, | |
} | |
}] | |
} | |
} | |
} | |
export default planetChartData; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<canvas :id="id"></canvas> | |
</template> | |
<script> | |
import Chart from 'chart.js'; | |
import planetChartData from '../../../../data/chart-data.js'; | |
export default{ | |
props:["id"], | |
data(){ | |
return { | |
planetChartData: planetChartData, | |
} | |
}, | |
mounted() { | |
this.createChart(this.id, this.planetChartData); | |
}, | |
methods: { | |
createChart(chartId, chartData) { | |
const ctx = document.getElementById(chartId); | |
const myChart = new Chart(ctx, { | |
type: chartData.type, | |
data: chartData.data, | |
options: chartData.options, | |
}); | |
} | |
} | |
} | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment