Skip to content

Instantly share code, notes, and snippets.

@apertureless
Last active February 21, 2017 17:40
Show Gist options
  • Save apertureless/a8718e037164849ef7a92fc28a59046a to your computer and use it in GitHub Desktop.
Save apertureless/a8718e037164849ef7a92fc28a59046a to your computer and use it in GitHub Desktop.
Vue-Chartjs
import {Line} from 'vue-chartjs'
import reactiveProp from './mixin.js'
export default Line.extend({
mixins: [reactiveProp],
props: ['chartData', 'options'],
mounted () {
this.renderChart(this.chartData, this.options)
}
})
<template>
<div class="small">
<line-chart :chart-data="datacollection" :options="options"></line-chart>
<button @click="fillData()">Randomize</button>
<button @click="fetchData()">Fetch</button>
</div>
</template>
<script>
import LineChart from './LineChart.js'
import axios from 'axios'
import _ from 'lodash'
export default {
components: {
LineChart
},
data () {
return {
datacollection: {
labels: [],
datasets: [
{
type: 'line',
// label: 'Min',
data: []
},
{
type: 'line',
// label: 'Max',
data: []
},
{
type: 'line',
// label: 'Avg',
data: []
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
xAxes: [{
stacked: false,
ticks: {
beginAtZero: false
},
gridLines: {
display: true
}
}],
yAxes: [{
stacked: false,
ticks: {
beginAtZero: false
},
gridLines: {
display: true
}
}]
}
}
}
},
mounted () {
this.fetchData()
},
methods: {
fetchData () {
// OR API ENPOINt
axios.get('http://localhost:8080/static/data.json')
.then((response) => {
let pricesChartData = _.merge({}, this.datacollection)
pricesChartData.labels = _.keys(response.data.prices_chart)
pricesChartData.datasets[0].data = _.map(response.data.prices_chart, 'min_price_per_day')
pricesChartData.datasets[0].label = 'Max'
pricesChartData.datasets[1].data = _.map(response.data.prices_chart, 'max_price_per_day')
pricesChartData.datasets[1].label = 'Min'
pricesChartData.datasets[2].data = _.map(response.data.prices_chart, 'avg_price_per_day')
pricesChartData.datasets[2].label = 'Avg'
this.datacollection = pricesChartData
})
},
fillData () {
this.datacollection = {
labels: [this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt()],
datasets: [
{
label: 'Data One',
backgroundColor: '#f87979',
data: [this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt()]
}, {
label: 'Data One',
backgroundColor: '#f87979',
data: [this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt(), this.getRandomInt()]
}
]
}
},
getRandomInt () {
return Math.floor(Math.random() * (50 - 5 + 1)) + 5
}
}
}
</script>
<style>
.small {
max-width: 600px;
margin: 150px auto;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment