我试图用vue-chartjs制作一个vue。在更新chartData prop时,我已经关注了有关重新渲染图表的所有相关文档。只有在调整浏览器窗口大小后,我的图表才会更新。
我的parenttestchart.vue文件:
<template>
<div>
<h1>Chart datacollection update test</h1>
<p>This component demonstrates fetching data from the server.</p>
<p v-if="!datacollection"><em>Loading...</em></p>
<b-form-select
v-model="testthingSelect.selected"
text="Select testthing"
variant="primary"
:options="testthingSelect.options"
:on-change="changetestthing"
class="m-md-2">
</b-form-select>
<test-chart v-cloak :chart-data="datacollection" :chart-option="options"></test-chart>
</div>
</template>
<script>
import TestChart from './TestChart'
export default {
components: {
TestChart
},
data() {
return {
yAxisValues: [],
datacollection: {},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: false
},
gridLines: {
display: true
}
}],
xAxes: [{
type: "date",
display:true,
gridLines: {
display: false
}
}]
},
legend: {
display: true
},
responsive: true,
maintainAspectRatio: false
},
testthingSelect: {
selected: "EIA/COAL",
disabled: false,
readonly: false,
visible: true,
color: "",
options: [
{
value: null,
text: 'Select a testthing'
},
{
value: "item1",
text: "item1"
},
{
value: "item1",
text: "item1"
},
{
value: "item1",
text: "item1"
}
]
}
}
},
methods: {
changetestthing: async function () {
try {
let response = await this.$http.get(url for my data);
this.datacollection.datasets = [];
this.datacollection.labels = [];
...required data transformations
this.$set(this.datacollection, "labels", labels);
this.$set(this.datacollection, "datasets", datasets);
// this.datacollection.labels = labels;
// this.datacollection.datasets = datasets;
} catch (error) {
console.log(error)
}
}
},
watch: {
'commoditySelect.selected': function () { this.changeCommodity(); }
},
async created() {
// ES2017 async/await syntax via babel-plugin-transform-async-to-generator
// TypeScript can also transpile async/await down to ES5
try {
let response = await this.$http.get(url for my data);
this.datacollection.datasets = [];
this.datacollection.labels = [];
...required data transformations
this.$set(this.datacollection, "labels", labels);
this.$set(this.datacollection, "datasets", datasets);
// this.datacollection.labels = labels;
// this.datacollection.datasets = datasets;
} catch (error) {
console.log(error)
}
} catch (error) {
console.log(error)
}
}
}
我的TestChart.vue文件
<script>
import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins
//Exporting this so it can be used in other components
export default {
extends: Line,
mixins: [reactiveProp],
props: ['chartData', 'options'],
mounted() {
this.renderChart(this.chartData, this.options)
}
}
</script>
因此,这在选择和更改父vue中的数据收集方面起作用。除非我重新调整窗口大小,否则图表不会重新渲染。当parenttestchart.vue最初加载时,我收到以下错误:&#39; Uncaught TypeError:无法读取属性&#39; transition&#39; of null&#39; 。但这似乎没有任何效果,因为调整窗口大小将呈现图表的初始显示。更新data changetestthing方法会正确更新datacollection(chartData),但除非我调整窗口大小,否则它不会重新渲染。
如何强制图表重新渲染?如何在父级中获得对TestChart组件的引用? this.TestChart未定义。我只是很好奇从参考中得到参考,我可以手动调用&#39; renderChart&#39;在上面。感谢。
***更新
我能够通过以下修改来更新图表:
我从changetestthing方法中删除了以下行:
this.datacollection.datasets = null;
this.datacollection.labels = null;
并将datacollection值赋值语句更改为:
this.datacollection = Object.assign({}, this.datacollection, { labels: labels, datasets: datasets });
我无法肯定地说这是最好的做法。根据vue.js文档(https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats)
new properties added to the object will not trigger changes. In such
cases, create a fresh object with properties from both the original
object and the mixin object
所以我相信没有将datacollection.labels和.datasets属性设置为null,然后使用Object.assign触发更改。我还不确定为什么他们没有使用mixins或手表触发。任何评论都赞赏。
答案 0 :(得分:2)
一种方法是将观察者添加到图表中,
props: ['chartData', 'options'],
mounted () {
this.renderChart(this.chartData, this.options)
},
watch: {
'chartData' (to, from) {
this.renderChart(this.chartData, this.options)
}
},
参见示例CodePen
编辑 - 观察者无法正常工作
您的代码与正在运行的CodePen之间的区别在于CodePen会改变传递给图表的变量,因此它的观察者会做出反应。
您的代码会改变变量的.datasets
属性 - 这个ref有一个已知问题:Mixins don't seem to trigger a refresh of the chart #44。
尝试替换
this.datacollection.datasets = [];
this.datacollection.labels = [];
这个(两个地方)
this.datacollection = { datasets: [], labels: [] };
答案 1 :(得分:2)
Billy Jean最后的笔记为我工作了PIECHART.JS:
import {
Pie,
mixins
} from 'vue-chartjs'
const {
reactiveProp
} = mixins
export default Pie.extend({
mixins: [reactiveProp],
props: ['options'],
watch: {
'options': {
handler(newOption, oldOption) {
this._chart.destroy()
this.renderChart(this.chartData, this.options)
},
deep: true
}
},
mounted() {
this.renderChart(this.chartData, {responsive: true, maintainAspectRadio:
false})
}
})
这是我的观点.vue文件:
let datasets = []
let colorlist = [ '#4e5fa4', '#5e5fa4', '#6e5fa4',
'#7e5fa4', '#8e5fa4', '#a08be4', '#b08bd4']
datasets.push({
label: this.data_by_city_label,
data: this.data_by_city_data,
fill: false,
backgroundColor: colorlist,
pointBorderWidth: 1,
borderWidth: 1,
pointRadius: 0
})
this.CityDatac = Object.assign({}, this.datasets, {labels: this.data_by_city_label, datasets: datasets})
有了这个,我可以将我的数据发送到PIECHART js并在我的view.vue中生成 如此:
<pie-chart-city :chartData="CityDatac" :width="300" :height="250"></pie-chart-city>
希望能帮助VUE.js中的任何人 - &gt; Vue的-chartjs.js
答案 2 :(得分:0)
Alexis Poo发布的代码为我指明了正确的方向。
就我而言,我需要稍微修改watch
对象。
在更改选项时尝试通过调用this._chart.destroy()
破坏图表时,我收到一条错误消息:cannot read property 'destroy' of undefined
。
要解决此问题,我必须改为this.$data._chart.destroy()
。
希望有帮助。