Created
December 18, 2016 04:16
-
-
Save AshMenhennett/a9482be198045d196b3b02d6f3f74b9c to your computer and use it in GitHub Desktop.
Example of using Chart.js with Vue (Laravel).
This file contains 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
/** resources/assets */ | |
/** | |
* First we will load all of this project's JavaScript dependencies which | |
* include Vue and Vue Resource. This gives a great starting point for | |
* building robust, powerful web applications using Vue and Laravel. | |
*/ | |
require('./bootstrap'); | |
var VueResource = require('vue-resource'); | |
/** | |
* Next, we will create a fresh Vue application instance and attach it to | |
* the page. Then, you may begin adding components to this application | |
* or customize the JavaScript scaffolding to fit your unique needs. | |
*/ | |
Vue.component('chart-here', require('./components/ChartComponent.vue')); | |
Vue.use(VueResource); | |
const app = new Vue({ | |
el: '#app' | |
}); |
This file contains 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> | |
<!-- resources/assets/js/components --> | |
<div> | |
<canvas id="canvas"></canvas> | |
</div> | |
</template> | |
<script> | |
import {Chart} from 'Chart.js'; | |
export default { | |
props: { | |
labels: String, | |
dataProp: String | |
}, | |
methods: { | |
renderChart() { | |
new Chart(document.getElementById('canvas').getContext('2d'), { | |
type: 'bar', | |
data: { | |
labels: JSON.parse(this.labels), | |
datasets: [{ | |
label: '# of Laravel apps made by Jack Russels', | |
data: JSON.parse(this.dataProp), | |
backgroundColor: [ | |
'#2ecc71', | |
'#e74c3c', | |
'#8e44ad', | |
'#d35400', | |
'#16a085' | |
] | |
}] | |
}, | |
options: { | |
title: { | |
display: true, | |
fontSize: 22, | |
text: 'Example' | |
} | |
} | |
}); | |
} | |
}, | |
mounted() { | |
this.renderChart(); | |
} | |
} | |
</script> |
This file contains 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
/** / */ | |
const elixir = require('laravel-elixir'); | |
require('laravel-elixir-vue-2'); | |
elixir((mix) => { | |
mix.sass('app.scss'); | |
mix.webpack('app.js'); | |
mix.version(['css/app.css', 'js/app.js']); | |
}); |
This file contains 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
<!-- resources/views/chart --> | |
@extends('layouts.app') | |
@section('content') | |
<chart-here | |
labels="{{ json_encode(['2014', '2015', '2016', '2017', '2020']) }}" | |
data-prop="{{ json_encode(['99', '66', '87', '49', '92']) }}"> | |
</chart-here> | |
@endsection |
Author
AshMenhennett
commented
Dec 18, 2016
Thanks but how we can do it in a single file html page with vue.js?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment