Skip to content

Instantly share code, notes, and snippets.

@bedus-creation
Last active June 5, 2019 10:10
Show Gist options
  • Save bedus-creation/21fce27bda11495b95bb74df2ba6f5ef to your computer and use it in GitHub Desktop.
Save bedus-creation/21fce27bda11495b95bb74df2ba6f5ef to your computer and use it in GitHub Desktop.
Setup chart Js in vue

Installation

npm install chart.js --save
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;
<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