Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active April 16, 2021 16:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyWay/39f70c3da562c52e2787 to your computer and use it in GitHub Desktop.
Save JeffreyWay/39f70c3da562c52e2787 to your computer and use it in GitHub Desktop.
import Chart from 'chart.js';
export default {
template: '<canvas width="600" height="400"></canvas>',
props: {
labels: {},
values: {},
color: {
default: 'rgba(220,220,220,0.2)'
}
},
ready() {
var data = {
labels: this.labels,
datasets: [
{
fillColor: this.color,
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: this.values
},
]
};
new Chart(
this.$el.getContext('2d')
).Line(data);
}
}
import Vue from 'vue';
import Graph from './components/Graph';
new Vue({
el: 'body',
components: { Graph }
});
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
</head>
<body>
<div class="container">
<graph :labels="['January', 'February', 'March']"
:values="[10, 42, 4]"
color="blue"
></graph>
</div>
<div class="container">
<graph :labels="['April', 'May', 'June']"
:values="[100, 420, 99]"
color="red"
></graph>
</div>
<script src="/js/main.js"></script>
</body>
</html>
@nezaboravi
Copy link

@kofoedandreas it does not matter how you import or include vue.js as long as its included.
new Chart() is changed on Chart.js side therefore Jeffery code is outdated
This is how youshould init it now with 2.5 version of Chart.js
let myChart = new Chart(this.$el.getContext('2d'), {
type: 'line',
data: data
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment