当chartData被替换、更新等时,vue.js vue-chartjs图表不更新

p4tfgftt  于 2022-11-06  发布在  Chart.js
关注(0)|答案(3)|浏览(343)

我正在尝试使用vue-chartjs制作一个vue。我已经遵循了所有相关文档中关于在chartData属性更新时重新呈现图表的说明。我的图表只有在调整浏览器窗口大小后才会更新。
我父母测试图表.vue文件:

  1. <template>
  2. <div>
  3. <h1>Chart datacollection update test</h1>
  4. <p>This component demonstrates fetching data from the server.</p>
  5. <p v-if="!datacollection"><em>Loading...</em></p>
  6. <b-form-select
  7. v-model="testthingSelect.selected"
  8. text="Select testthing"
  9. variant="primary"
  10. :options="testthingSelect.options"
  11. :on-change="changetestthing"
  12. class="m-md-2">
  13. </b-form-select>
  14. <test-chart v-cloak :chart-data="datacollection" :chart-option="options"></test-chart>
  15. </div>
  16. </template>
  17. <script>
  18. import TestChart from './TestChart'
  19. export default {
  20. components: {
  21. TestChart
  22. },
  23. data() {
  24. return {
  25. yAxisValues: [],
  26. datacollection: {},
  27. options: {
  28. scales: {
  29. yAxes: [{
  30. ticks: {
  31. beginAtZero: false
  32. },
  33. gridLines: {
  34. display: true
  35. }
  36. }],
  37. xAxes: [{
  38. type: "date",
  39. display:true,
  40. gridLines: {
  41. display: false
  42. }
  43. }]
  44. },
  45. legend: {
  46. display: true
  47. },
  48. responsive: true,
  49. maintainAspectRatio: false
  50. },
  51. testthingSelect: {
  52. selected: "EIA/COAL",
  53. disabled: false,
  54. readonly: false,
  55. visible: true,
  56. color: "",
  57. options: [
  58. {
  59. value: null,
  60. text: 'Select a testthing'
  61. },
  62. {
  63. value: "item1",
  64. text: "item1"
  65. },
  66. {
  67. value: "item1",
  68. text: "item1"
  69. },
  70. {
  71. value: "item1",
  72. text: "item1"
  73. }
  74. ]
  75. }
  76. }
  77. },
  78. methods: {
  79. changetestthing: async function () {
  80. try {
  81. let response = await this.$http.get(url for my data);
  82. this.datacollection.datasets = [];
  83. this.datacollection.labels = [];
  84. ...required data transformations
  85. this.$set(this.datacollection, "labels", labels);
  86. this.$set(this.datacollection, "datasets", datasets);
  87. // this.datacollection.labels = labels;
  88. // this.datacollection.datasets = datasets;
  89. } catch (error) {
  90. console.log(error)
  91. }
  92. }
  93. },
  94. watch: {
  95. 'commoditySelect.selected': function () { this.changeCommodity(); }
  96. },
  97. async created() {
  98. // ES2017 async/await syntax via babel-plugin-transform-async-to-generator
  99. // TypeScript can also transpile async/await down to ES5
  100. try {
  101. let response = await this.$http.get(url for my data);
  102. this.datacollection.datasets = [];
  103. this.datacollection.labels = [];
  104. ...required data transformations
  105. this.$set(this.datacollection, "labels", labels);
  106. this.$set(this.datacollection, "datasets", datasets);
  107. // this.datacollection.labels = labels;
  108. // this.datacollection.datasets = datasets;
  109. } catch (error) {
  110. console.log(error)
  111. }
  112. } catch (error) {
  113. console.log(error)
  114. }
  115. }
  116. }

我TestChart.vue文件

  1. <script>
  2. import { Line, mixins } from 'vue-chartjs'
  3. const { reactiveProp } = mixins
  4. //Exporting this so it can be used in other components
  5. export default {
  6. extends: Line,
  7. mixins: [reactiveProp],
  8. props: ['chartData', 'options'],
  9. mounted() {
  10. this.renderChart(this.chartData, this.options)
  11. }
  12. }
  13. </script>

因此,这是在选择和更改父vue中的数据集合方面的工作。除非我调整窗口大小,但图表不会重新呈现。当parenttestchart.vue最初加载时,我收到以下错误:'未捕获的类型错误:无法读取null'的属性'transition'。但这似乎不会产生任何影响,因为调整窗口大小将呈现图表的初始显示。更新数据changetestthing方法确实会正确更新datacollection(chartData),但除非我调整窗口大小,否则它不会重新呈现。
如何强制图表重新呈现?如何在父级中获取对TestChart组件的引用??这。TestChart未定义。我只是想从引用中获取引用,以防我可以手动对其调用'renderChart'。谢谢。

***更新

我能够通过以下修改更新图表:
我从changetestthing方法中删除了以下代码行:

  1. this.datacollection.datasets = null;
  2. this.datacollection.labels = null;

并将数据集合赋值语句更改为:

  1. this.datacollection = Object.assign({}, this.datacollection, { labels: labels, datasets: datasets });

我不能肯定地说这是最好的实践。

  1. new properties added to the object will not trigger changes. In such
  2. cases, create a fresh object with properties from both the original
  3. object and the mixin object

所以我相信通过不设置datacollection.labels和.datasets属性为null,然后使用Object.assign,这些更改就会被触发。我仍然不确定为什么它们没有被mixin或手动监视触发。感谢您的评论。

svdrlsy4

svdrlsy41#

一种有效的方法是在图表中添加一个观察器,

  1. props: ['chartData', 'options'],
  2. mounted () {
  3. this.renderChart(this.chartData, this.options)
  4. },
  5. watch: {
  6. 'chartData' (to, from) {
  7. this.renderChart(this.chartData, this.options)
  8. }
  9. },

请参见示例CodePen

编辑-观察程序不工作

您的代码和工作的CodePen之间的区别在于,CodePen会改变传递到图表中的变量,因此它的观察器会做出React。
您的程式码会变更变数的.datasets属性-这个ref有一个已知的问题:混合似乎不会触发图表#44的刷新。
尝试替换

  1. this.datacollection.datasets = [];
  2. this.datacollection.labels = [];

与此(两个位置)

  1. this.datacollection = { datasets: [], labels: [] };
展开查看全部
2w3rbyxf

2w3rbyxf2#

比利·简最后一个音符对我很管用

  1. import {
  2. Pie,
  3. mixins
  4. } from 'vue-chartjs'
  5. const {
  6. reactiveProp
  7. } = mixins
  8. export default Pie.extend({
  9. mixins: [reactiveProp],
  10. props: ['options'],
  11. watch: {
  12. 'options': {
  13. handler(newOption, oldOption) {
  14. this._chart.destroy()
  15. this.renderChart(this.chartData, this.options)
  16. },
  17. deep: true
  18. }
  19. },
  20. mounted() {
  21. this.renderChart(this.chartData, {responsive: true, maintainAspectRadio:
  22. false})
  23. }
  24. })

这是在我的视图.vue文件:

  1. let datasets = []
  2. let colorlist = [ '#4e5fa4', '#5e5fa4', '#6e5fa4',
  3. '#7e5fa4', '#8e5fa4', '#a08be4', '#b08bd4']
  4. datasets.push({
  5. label: this.data_by_city_label,
  6. data: this.data_by_city_data,
  7. fill: false,
  8. backgroundColor: colorlist,
  9. pointBorderWidth: 1,
  10. borderWidth: 1,
  11. pointRadius: 0
  12. })
  13. this.CityDatac = Object.assign({}, this.datasets, {labels: this.data_by_city_label, datasets: datasets})

有了它,我可以将我的数据发送到PIECHART js,并在我的视图中生成。

  1. <pie-chart-city :chartData="CityDatac" :width="300" :height="250"></pie-chart-city>

希望这对VUE.js -〉Vue-chartjs.js中的任何人都有帮助

展开查看全部
r7s23pms

r7s23pms3#

Alexis Poo发布的代码为我指明了正确的方向。
在我的例子中,我需要稍微修改watch对象。
当试图通过调用this._chart.destroy()销毁图表时,当选项更改时,我收到一条错误消息:cannot read property 'destroy' of undefined .
为了解决这个问题,我必须改为执行this.$data._chart.destroy()
希望能有所帮助。

相关问题