javascript - 在 vuejs 中单击按钮时显示/隐藏动态数据
问题描述
我想在每次单击按钮时动态显示和隐藏折线图,但是当我单击任何按钮时,数据不会改变。每次我都可以看到相同的数据。这是我的代码:
<template>
<div>
<v-btn color="primary" mr-2 @click="changeTab('week')">Week</v-btn>
<v-btn color="error" mr-2 @click="changeTab('month')">Month</v-btn>
<v-btn color="info" mr-2 @click="changeTab('year')">Year</v-btn>
<div v-if="selectedChartData !=null">
<line-chart
:width="650"
:height="400"
:dataSet= "selectedChartData.ChartData"
:labels= "selectedChartData.ChartLabel"
:chartColorsData="selectedChartData.ChartColors"
:label="selectedChartData.ChartData"
>
</line-chart>
</div>
</div>
</template>
<script>
import LineChart from "Components/Charts/LineChart";
import { buySellChartData } from 'Assets/data/buySellChartData.js'
export default {
components:{
LineChart,
},
data() {
return {
buySellChartData,
selectedButton: 'week',
selectedChartData: null,
};
},
mounted(){
this.selectedChart(this.selectedButton);
},
methods:{
selectedChart(selectedButton){
for(var i=0; i< this.buySellChartData.length; i++){
if(this.buySellChartData[i].tag == selectedButton) {
this.selectedChartData = this.buySellChartData[i];
break;
}
}
},
changeTab(selectedBtn){
this.selectedButton = selectedBtn;
this.selectedChart(this.selectedButton);
}
}
};
</script>
我在单击按钮时将所选数据分配给变量“ selectedChartData ”并传递给折线图组件。在“this.buySellChartData[i].tag”标签的值是“年、周或月”。这是折线图代码:
import { Line } from 'vue-chartjs'
const lineTension = 0.1;
const borderWidth = 3;
const pointRadius = 2;
const pointBorderWidth = 2;
export default {
extends: Line,
props: {
dataSet: {
type: Array
},
label: {
type: Array,
},
labels: {
type: Array
},
chartColorsData:{
type: Array
},
},
data() {
return {
options: {
scales: {
yAxes: [{
gridLines: {
display: true,
drawBorder: true
},
ticks: {
stepSize: 20,
// padding: 5
display:true
}
}],
xAxes: [{
gridLines: {
display: false,
drawBorder: false
},
ticks: {
// padding: 10
display:true
}
}]
},
legend: {
display: false
},
responsive: true,
maintainAspectRatio: false
}
}
},
mounted() {
this.renderChart({
labels: this.labels,
datasets: [
{
label: (this.label[0]).label,
lineTension,
borderColor: this.chartColorsData[0].borderColor,
pointBorderColor: this.chartColorsData[0].borderColor,
pointBorderWidth,
pointRadius,
fill: false,
pointBackgroundColor: '#FFF',
borderWidth,
data: this.dataSet[0].data
},
{
label: this.label[1].label,
lineTension,
borderColor: this.chartColorsData[1].borderColor,
pointBorderColor: this.chartColorsData[1].borderColor,
pointBorderWidth,
pointRadius,
fill: false,
pointBackgroundColor: '#FFF',
borderWidth,
data: this.dataSet[1].data
},
{
label: this.label[2].label,
lineTension,
borderColor: this.chartColorsData[2].borderColor,
pointBorderColor: this.chartColorsData[2].borderColor,
pointBorderWidth,
pointRadius,
fill: false,
pointBackgroundColor: '#FFF',
borderWidth,
data: this.dataSet[2].data
},
]
}, this.options)
}
}
请打开链接查看我正在创建什么样的图表的屏幕截图https://www.awesomescreenshot.com/image/4110976/35de049e785364eec1006c23301dcf2f。那么应该如何在每个按钮单击时显示不同的图表。如果有人需要更多信息,请告诉我。任何帮助将不胜感激。谢谢!
解决方案
如果您更改数据集,vue-chartjs 不会提供实时更新。但是,vue-chartjs 提供了两个 mixin 来实现这一点。
- 反应道具
- 反应数据
因此,将 reactiveProp mixin 添加到您可以在数据集更改或更新时实时更新图表
import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins
export default {
extends: Line,
mixins: [reactiveProp],
props: ['options'],
mounted () {
// this.chartData is created in the mixin.
// If you want to pass options please create a local options object
this.renderChart(this.chartData, this.options)
}
}
推荐阅读
- linux - 如何让 Linux 忽略键盘,同时保持它可供我的程序读取?
- javascript - 我的快速解析设置有什么问题?
- python - 如何提高 Tesseract 的准确性
- python - Python Pandas 中将数据框/列归零的最快方法
- python - 在海龟图形中使用 clear() 函数时如何停止闪烁
- c# - Blazor 创建服务,从 SQL Server 存储过程中提取数据
- matlab - 需要帮助构建 STL 文件
- sql - 如何将计数用于 case 语句
- google-analytics - Google Analytics Report API v4 在没有明显原因的情况下多次返回 503
- node.js - 使用 Nodejs Sharp 模块时出错。模块解析失败:意外字符 '' (1: 0)