I want to switch the chart from using the data stored in one object property to another (from data stored under the key value to the key value2) by clicking a button (@click).
I don't know how to use @click for updating the data.
I'm using Axios to get the data using async await.
My JSON data:
// Pulled from
// https://raw.githubusercontent.com/narudolimudom/Hospital/main/examplefile.json
[
{
"id": 1,
"name": "A",
"value": 20,
"value2": 50,
"value3": "90",
"value4": "100"
},
{
"id": 2,
"name": "B",
"value": 47,
"value2": 48,
"value3": "66",
"value4": "87"
},
. . .
]
I want to go from using resp.data[index].value in my chart to using resp.data[index].value2.
This is my code:
Test.vue
<template>
<div class="container">
<button>Click</button>
<barchart v-if="loaded" :chartdata="chartData" :options="options" />
</div>
</template>
<script>
import axios from "axios";
import barchart from "./Render.vue";
export default {
name: "LineChartContainer",
components: { barchart },
data: () => ({
loaded: false,
chartData: {
labels: [],
datasets: [
{
label: "Bar Chart",
borderWidth: 1,
pointBorderColor: "#2554FF",
data: [],
},
],
},
options: {.....},
}),
async mounted() {
this.loaded = false;
try {
const resp = await axios.get(
"https://raw.githubusercontent.com/narudolimudom/Hospital/main/examplefile.json"
);
for (let index = 0; index < resp.data.length; index++) {
this.chartData.labels.push(resp.data[index].name);
this.chartData.datasets[0].data.push(resp.data[index].value);
}
this.loaded = true;
} catch (e) {
console.error(e);
}
},
};
</script>
Render.vue
<script>
import { Bar } from "vue-chartjs";
export default {
extends: Bar,
props: {
chartdata: {
type: Object,
default: null,
},
options: {
type: Object,
default: null,
},
},
mounted() {
this.renderChart(this.chartdata, this.options);
},
};
</script>