I have displayed my chart using API in vuejs, now I want to filter date and display chart respectively. I have my Chart.vue which contains my chart information, So now I want to add a date filter just like the image below:
So whenever I click any of the input my chart should change accordingly. For example if I click on 3 months, the chart should display last 3 months data.
Here is my code Chart.vue
<template>
<div class="chart-container" style="position: relative; height: 25vh; width:100%;">
<canvas id="DisplayChart" ></canvas>
</div>
</template>
<script>
import moment from 'moment'
export default {
name: 'Chart_from_API',
data () {
return {
myChart: []
}
},
mounted () {
this.display()
},
methods: {
display () { this.$http.get('https://api.wirespec.dev/wirespec/stackoverflow/fetchchartdataforvuejs') //Your API has to be given here
.then((response) => {
const result = response.data
const ctx = document.getElementById('DisplayChart').getContext('2d')
const Chart_data = []
for (let i = 0; i < result.date.length; i++) {
Chart_data.push({
x_axis: moment(result.date[i], 'X').toDate(), //To Convert Unix Timestamp into Date
y_axis: result.challenge[i]
})
}
// eslint-disable-next-line init-declarations,prefer-const
let myChart
if (myChart !== undefined) {
myChart.destroy()
}
// eslint-disable-next-line no-undef
myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [
{
label: 'Chart_from_API',
data: Chart_data,
borderColor: '#EA5455',
lineTension: 0
}
]
},
options: {
lineTension: 0,
maintainAspectRatio: false,
legend: {
display: false
},
scales: {
yAxes: [
{
scaleLabel: {
display: false
},
ticks: {
beginAtZero: true,
// eslint-disable-next-line no-unused-vars
callback (value) {
return `${value }k` // y-axis value will append k to it
}
}
}
],
xAxes: [
{
type: 'time',
time: {
unit: 'month'
},
scaleLabel: {
display: true,
labelString: ''
}
}
]
}
}
})
})
.catch((error) => {
console.log(error)
})
}
}
}
And here is my main component App.vue:
<template>
<div class="vx-row">
<div class="vx-col w-full mb-base">
<vx-card>
<div class="pull-right" style="float:right;">
<!-- Timeframe buttons -->
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<div class="btn-group btn-group-toggle timeframeButtons" id="timeframe_button">
<label class="btn btn-sm btn-secondary active">
<input type="radio" name="options" value="all" autocomplete="off" checked> Overall
</label>
<label class="btn btn-sm btn-secondary">
<input type="radio" name="options" value="6" autocomplete="off"> 6 months
</label>
<label class="btn btn-sm btn-secondary">
<input type="radio" name="options" value="3" autocomplete="off"> 3 months
</label>
<label class="btn btn-sm btn-secondary">
<input type="radio" name="options" value="1" autocomplete="off"> 1 month
</label>
</div>
</div>
</div>
<Chart></Chart>
</vx-card>
</div>
</div>
</template>
<script>
import Chart from './Chart.vue'
export default {
component: {Chart}
}
</script>
I don't have any idea how to do it in my case, so please someone help me with this. Please send me what changes should be done completely at least for one option so that I can understand the logic behind it.
