in my opinion, one of the best solutions is to create an array with graphs, the elements of which will contain a flag that allows you to adjust the display of each graph.
The final version will look approximately like this.
<template>
<div class="monitor-container">
<div
class="monitor_interface-chart-container"
v-for="chart in charts"
:key="chart.id"
>
<BaseChart v-if="chart.isShown" />
<i
v-if="!chart.isShown"
@click="addChart(chart.id)"
class="pi pi-plus-circle new_chart-icon"
>+</i
>
<i
v-else
@click="removeChart(chart.id)"
class="pi pi-plus-circle new_chart-icon"
>-</i
>
</div>
</div>
</template>
<script>
import BaseChart from "@/components/charts/BaseChart.vue";
export default {
components: {
BaseChart,
},
data() {
return {
charts: [
{
id: 1,
isShown: true,
},
{
id: 2,
isShown: true,
},
{
id: 3,
isShown: true,
},
{
id: 4,
isShown: true,
},
{
id: 5,
isShown: true,
},
{
id: 6,
isShown: true,
},
],
};
},
methods: {
addChart(id) {
const chart = this.charts.find((chart) => chart.id === id);
if (chart) {
chart.isShown = true;
}
},
removeChart(id) {
const chart = this.charts.find((chart) => chart.id === id);
if (chart) {
chart.isShown = false;
}
},
},
};
</script>
This solution is applicable for a fixed array length. If you plan to create an unlimited number of graphs, then the code will have to be changed.
In this case, we cannot use the index as a key when iterating through v-for. When adding a new element, you need to generate a unique id and add a new element to the array. To delete an element, you just need to filter the current array by the element id.
<template>
<div class="monitor-container">
<div
class="monitor_interface-chart-container"
v-for="chart in charts"
:key="chart.id"
>
<BaseChart />
<i @click="removeChart(chart.id)" class="pi pi-plus-circle new_chart-icon"
>-</i
>
</div>
<i @click="addChart()" class="pi pi-plus-circle new_chart-icon">+</i>
</div>
</template>
<script>
import BaseChart from "@/components/charts/BaseChart.vue";
export default {
components: {
BaseChart,
},
data() {
return {
charts: [],
};
},
methods: {
addChart(id) {
const biggestId = this.charts.reduce(
(acc, rec) => (acc.id > rec.id ? acc.id : rec.id),
0
);
this.charts.push({ id: biggestId + 1 });
},
removeChart(id) {
this.charts = this.charts.filter((chart) => chart.id !== id);
},
},
};
</script>