windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Updating a chart from chart.js in vue

Asked By: Anonymous

I have a function in my vue applications that creates a chart.This is the chart function:

startChart: function (canvas, type) {

    // init chart.js
    var ctx = document.getElementById("myChart");
    var myDoughnut = new Chart(ctx, {
        type: 'doughnut',
        data: {
            labels: this.getAnalyticList[this.getRowIndex].chartNames,
            datasets: [{
                data: this.getAnalyticList[this.getRowIndex].chartData,
                backgroundColor: [
                    'rgba(255, 99, 132, 0.6)',
                    'rgba(54, 162, 235, 0.6)',
                    'rgba(255, 206, 86, 0.6)',
                    'rgba(75, 192, 192, 0.6)',
                    'rgba(153, 102, 255, 0.6)',
                    'red',
                    'yellow',
                    'blue',
                    'green',
                    'orange',
                    'teal',
                    'violet',
                    'pink',
                    'purple'
                ]
            }]
        },
        options: {
            responsive: true,
            defaultFontColor: '#ffffff',

        }
    })
}

and i then started it in the vue mounted hook using this line

this.startChart(this.$refs.canvas, 'Doughnut');

What i want to do is take the row index of a table loaded above the chart and that would then be passed into the getAnalyticList value that loads the charts so i can grab different arrays from the same list. I was working on creating an update function i got off this js fiddle from this stack article. I’ve been trying to adapt the function he created into vue but am having no luck here is the function:

changeData: function () {
            var myChart = Chart.doughnut(this.$refs.canvas, {
                data: data,
            });
            myChart.data.datasets.data = this.getAnalyticList[this.getRowIndex].chartData;
            myChart.data.labels = this.getAnalyticList[this.getRowIndex].chartNames;
            myChart.update();
        }

Any suggestions would be appreciated.


Solution

Answered By: Anonymous

I think the only problem with your code is that you’re not updating the dataset correctly.
With myChart.data.datasets.data = ... your adding data to datasets object – it should be myChart.data.datasets[0].data = ....

I’ve also added the chart object to Vue.js model in startChart with-out reactivity because it needs to be updated manually. Just don’t add it to data hook. So you can reference the chart with this.myChart in your updating method.

I’ve renamed getRowIndex in the demo because rowIndex is more readable – you could do the same with the getAnalyticsLists. I would name it with get-prefix if it’s a method that is getting something.

Scroll down in the demo to toggle the data with the next button. That button increments rowIndex and rolls over at the end.

Please have a look at the demo below or at this jsfiddle.

_x000D_

_x000D_

const chartComponent = {_x000D_
  data() {_x000D_
      return {_x000D_
        //myChart: undefined, // don't add chart here -> reactivity could be a problem_x000D_
        getAnalyticList: [{_x000D_
          chartNames: [_x000D_
            'Red',_x000D_
            'Yellow',_x000D_
            'Blue'_x000D_
          ],_x000D_
          chartData: [10, 20, 30]_x000D_
        },_x000D_
        {_x000D_
          chartNames: [_x000D_
            'Red',_x000D_
            'Yellow',_x000D_
            'Blue'_x000D_
          ],_x000D_
          chartData: [40, 10, 20]_x000D_
        }_x000D_
        ],_x000D_
        rowIndex: 0_x000D_
      }_x000D_
    },_x000D_
    template: `_x000D_
    <div>_x000D_
    <canvas id="myChart" width="200px" height="200px"></canvas>_x000D_
    <button @click="incRow">next</button>_x000D_
    {{rowIndex}}_x000D_
    </div>_x000D_
  `,_x000D_
    methods: {_x000D_
      startChart: function() {_x000D_
        var ctx = document.getElementById("myChart");_x000D_
        var myDoughnut = this.myChart =  new Chart(ctx, {_x000D_
          type: 'doughnut',_x000D_
          data: {_x000D_
            labels: this.getAnalyticList[this.rowIndex].chartNames,_x000D_
            datasets: [{_x000D_
              data: this.getAnalyticList[this.rowIndex].chartData,_x000D_
              backgroundColor: [_x000D_
                'rgba(255, 99, 132, 0.6)',_x000D_
                'rgba(54, 162, 235, 0.6)',_x000D_
                'rgba(255, 206, 86, 0.6)',_x000D_
                'rgba(75, 192, 192, 0.6)',_x000D_
                'rgba(153, 102, 255, 0.6)',_x000D_
                'red',_x000D_
                'yellow',_x000D_
                'blue',_x000D_
                'green',_x000D_
                'orange',_x000D_
                'teal',_x000D_
                'violet',_x000D_
                'pink',_x000D_
                'purple'_x000D_
              ]_x000D_
            }]_x000D_
          },_x000D_
          options: {_x000D_
            responsive: true,_x000D_
            defaultFontColor: '#ffffff',_x000D_
                        _x000D_
          }_x000D_
        })_x000D_
      },_x000D_
      incRow () {_x000D_
        if (this.rowIndex < this.getAnalyticList.length - 1) {_x000D_
            this.rowIndex++;_x000D_
        }_x000D_
        else {_x000D_
            this.rowIndex = 0;_x000D_
        }_x000D_
        this.changeData()_x000D_
      },_x000D_
      changeData () {_x000D_
        this.myChart.data.datasets[0].data =_x000D_
            this.getAnalyticList[this.rowIndex].chartData;_x000D_
        this.myChart.data.labels = this.getAnalyticList[this.rowIndex].chartNames;_x000D_
        this.myChart.update();_x000D_
        console.log('changed', this.myChart.data)_x000D_
      }_x000D_
    },_x000D_
    mounted() {_x000D_
      console.log('started')_x000D_
      this.startChart();_x000D_
    }_x000D_
}_x000D_
_x000D_
new Vue({_x000D_
  el: '#app',_x000D_
  components: {_x000D_
    chart: chartComponent_x000D_
  }_x000D_
})

_x000D_

div {_x000D_
   width: 400px;_x000D_
   height: 400px;_x000D_
}

_x000D_

<a href="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js">https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js</a>_x000D_
<a href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js">https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js</a>_x000D_
<div id="app">_x000D_
  <chart></chart>_x000D_
</div>

_x000D_

_x000D_

x000D

techinplanet staff

instagram volgers kopen volgers kopen buy windows 10 pro buy windows 11 pro


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno
Alanya escort Manavgat escort Fethiye escort Kemer escort Didim escort Çanakkale escort Aydın escort Muğla escort Tekirdağ escort Manisa escort Balıkesir escort Trabzon escort Elazığ escort Ordu escort Kütahya escort Isparta escort Rize escort Kahramanmaraş escort Yalova escort Giresun escort Yozgat escort Tokat escort Şanlıurfa escort Sivas escort Batman escort Erzurum escort Sinop escort Kırşehir escort Karaman escort Kırıkkale escort Bolu escort Amasya escort Niğde escort Uşak escort Edirne escort Çorum escort Osmaniye escort Zonguldak escort Van escort Erzincan escort Söke escort Bodrum escort Çerkezköy escort Akhisar escort Bandırma escort Ayvacık escort Akçaabat escort Karakoçan escort Altınordu escort Tavşanlı escort Eğirdir escort Ardeşen escort Afşin escort Altınova escort Bulancak escort Sorgun escort Erbaa escort Viranşehir escort Zara escort Kozluk escort Aziziye escort Ayancık escort Kaman escort Ermenek escort Keskin escort Gerede escort Göynücek escort Bor escort Banaz escort Havsa escort Osmancık escort Bahçe escort Alaplı escort Başkale escort Kemah escort Nazilli escort Fethiye escort Çorlu escort Alaşehir escort Altıeylül escort Biga escort Araklı escort Kovancılar escort Fatsa escort Simav escort Yalvaç escort Çayeli escort Dulkadiroğlu escort Çiftlikköy escort Espiye escort Sarıkaya escort Niksar escort Suruç escort Yıldızeli escort Sason escort Horasan escort Boyabat escort Mucur escort Sarıveliler escort Yahşihan escort Göynük escort Gümüşhacıköy escort Çamardı escort Eşme escort İpsala escort Sungurlu escort Hasanbeyli escort Çaycuma escort İpekyolu escort Refahiye escort Kuşadası escort Marmaris escort Süleymanpaşa escort Turgutlu escort Susurluk escort Gelibolu escort Of escort Ünye escort Domaniç escort Fındıklı escort Elbistan escort Çınarcık escort Tirebolu escort Akdağmadeni escort Turhal escort Eyyübiye escort Suşehri escort Yakutiye escort Gerze escort Mengen escort Merzifon escort Ulukışla escort Sivaslı escort Keşan escort Kadirli escort Ereğli escort Özalp escort Tercan escort Efeler escort Didim escort Çine escort Dalaman escort Menteşe escort Milas escort Ortaca escort Seydikemer escort Ergene escort Kapaklı escort Malkara escort Salihli escort Şehzadeler escort Soma escort Yunusemre escort Ayvalık escort Bigadiç escort Burhaniye escort Gönen escort Karesi escort Çan escort Yenice escort Ortahisar escort Yomra escort Perşembe escort Pazar escort Onikişubat escort Pazarcık escort Türkoğlu escort Eynesil escort Görele escort Piraziz escort Yağlıdere escort Çayıralan escort Boğazlıyan escort Zile escort Siverek escort Karaköprü escort Haliliye escort Akçakale escort Şarkışla escort Gemerek escort Oltu escort Palandöken escort Mudurnu escort Suluova escort Taşova escort Toprakkale escort Kilimli escort Tuşba escort Üzümlü escort