MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    How can I update my chart?

    Scheduled Pinned Locked Moved Troubleshooting
    21 Posts 3 Posters 7.8k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D Offline
      doridol @sdetweil
      last edited by

      @sdetweil
      I changed socketNotificationReceived like this

      socketNotificationReceived: function (notification, payload) {
                  if (notification === "TEXT_RESULT") {
                      this.textDataRecived = payload;
                      this.loaded = true;
                  }
                  
                  this.myChart.data.datasets.data.push(this.textDataRecived)
                  this.myChart.update();
                  // this.updateDom(60000);
              },
      

      And I got error like these

      화면 캡처 2021-07-13 231609.png

      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @doridol
        last edited by

        @doridol stop on the push, and mouse over ‘this’

        check each part of your data path

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        D 1 Reply Last reply Reply Quote 0
        • D Offline
          doridol @sdetweil
          last edited by

          @sdetweil I’m so sorry that I asked same question twice although you answered my questions.
          I found that this.myChart in socketNotificationReceived doens’t pointing the this.myChart in getDom how can I point this.myChart in socketNotificationReceived exactly to this.myChart in getDom??

          S 1 Reply Last reply Reply Quote 0
          • S Offline
            sdetweil @doridol
            last edited by

            @doridol try

            change

            socketNotificationReceived: function (notification, payload) {
            

            to this

            socketNotificationReceived:  (notification, payload) => {
            

            the arrow function should retain the context of ‘this’.

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            D 1 Reply Last reply Reply Quote 0
            • D Offline
              doridol @sdetweil
              last edited by

              @sdetweil I changed my socketNotificationReceived like this

              socketNotificationReceived: (notification, payload) => {
                          if (notification === "TEXT_RESULT") {
                              this.textDataRecived = payload;
                              this.loaded = true;
                          }
                          
                          console.log(this.myChart)
                          // this.myChart.data.datasets.data = this.textDataRecived
                          // this.updateDom(60000);
                      },
              

              I add console.log(this.myChart) to check it point this.myChart in getDom but it print undefined… what did I do wrong??
              Really thanks for your so many helps

              S 1 Reply Last reply Reply Quote 0
              • S Offline
                sdetweil @doridol
                last edited by sdetweil

                @doridol the 1st time thru the chart doesn’t exist yet, right?

                                // gets the data for chart
                                this.textDataRecived = payload;
                                this.loaded = true;
                            }
                            // chart may NOT be drawn yet 
                           // getDom has not been called yet since data arrived
                           if(this.myChart !==  undefined )
                               console.log(this.myChart)
                

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                D 2 Replies Last reply Reply Quote 0
                • D Offline
                  doridol @sdetweil
                  last edited by

                  @sdetweil aaaaah!! I understood! I’ll try it

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    doridol @sdetweil
                    last edited by

                    @sdetweil I tried it but nothing is printed in develop console window :anxious_face_with_sweat:

                    S 1 Reply Last reply Reply Quote 0
                    • S Offline
                      sdetweil @doridol
                      last edited by sdetweil

                      @doridol did u get an update for the chart? (another socketnotification?)

                      also, where do u define myChart?

                      should be after defaults

                      myChart: null,
                      

                      Sam

                      How to add modules

                      learning how to use browser developers window for css changes

                      D 1 Reply Last reply Reply Quote 0
                      • D Offline
                        doridol @sdetweil
                        last edited by

                        @sdetweil I define myChart in getDom like this

                        getDom: function () {
                                    const wrapper = document.createElement("div");
                                    
                                    this.ctx = document.createElement("canvas");
                                    this.ctx.style.width = "700px";
                                    this.ctx.style.height = '700px';
                                    
                                    wrapper.appendChild(this.ctx)
                                    
                                    labels = [2,4,6,8]
                                    
                                    this.myChart = new Chart(this.ctx, {
                        
                                        type: 'line',
                                        data: {
                                            labels: labels,
                                            
                                            datasets: [{
                                                
                                                data: [1,2,3,4],
                        
                                                backgroundColor: 'rgba(255,255,255,0.3)',
                                                borderColor: 'white',
                                                
                                                borderWidth: 2,
                                                
                                                fill: true,
                                                
                                            }]
                                        },
                        
                                        options: {
                        
                                            title: {
                                                    display: true,
                                                    text: 'Air-Quality',
                                            },
                        
                                            scales: {
                        
                                                xAxes: [{
                        
                                                    gridLines: {
                                                        color: 'rgba(255,255,255,0.2)',
                                                    },
                                                }],
                                            },
                                        }
                                    });
                                    return wrapper;
                                }
                            });
                        

                        and I’ll only update myChart in socketNotificationReceived

                        S 1 Reply Last reply Reply Quote 0
                        • S Offline
                          sdetweil @doridol
                          last edited by

                          @doridol that is USE, not really define

                          I don’t like side effects… create on 1st use… ugh.

                          also, you will UPDATE in notification, but need to CREATE in getdom, or somewhere

                          Sam

                          How to add modules

                          learning how to use browser developers window for css changes

                          D 2 Replies Last reply Reply Quote 0
                          • D Offline
                            doridol @sdetweil
                            last edited by

                            @sdetweil oh… it is very difficult to update chart… really thanks for your help I seem I have to study more…

                            1 Reply Last reply Reply Quote 0
                            • D Offline
                              doridol @sdetweil
                              last edited by

                              @sdetweil I have really really last question I changed my socketNotificationReceived like this

                              socketNotificationReceived: function(notification, payload) {
                                          if (notification === "TEXT_RESULT") {
                                              this.textDataRecived = payload;
                                              this.loaded = true;
                                          }
                              
                                          
                                          if(count < 2)
                                          {
                                              this.updateDom();
                                              count = count + 1;
                                          }
                                          else
                                          {
                                              this.myChart.data.datasets.data = this.textDataRecived
                                              console.log(this.myChart.data.datasets.data)
                                              this.myChart.update();
                                          }
                                      },
                              

                              and console.log(this.myChart.data.datasets.data) successfully print this.textDataRecived but my chart is not updated in the screen why???

                              S 1 Reply Last reply Reply Quote 0
                              • S Offline
                                sdetweil @doridol
                                last edited by

                                @doridol i do not know anything about the chart.update() method… I don’t use it.

                                Sam

                                How to add modules

                                learning how to use browser developers window for css changes

                                D 1 Reply Last reply Reply Quote 0
                                • D Offline
                                  doridol @sdetweil
                                  last edited by

                                  @sdetweil okay thanks for you a lot of help:beaming_face_with_smiling_eyes:

                                  S 1 Reply Last reply Reply Quote 0
                                  • S Offline
                                    sdetweil @doridol
                                    last edited by

                                    @doridol i use destroy() and then build another

                                    Sam

                                    How to add modules

                                    learning how to use browser developers window for css changes

                                    D 1 Reply Last reply Reply Quote 0
                                    • D Offline
                                      doridol @sdetweil
                                      last edited by doridol

                                      @sdetweil I finally update my chart with this.myChart.update()!!! it is all for your help now I can get sleep…

                                      1 Reply Last reply Reply Quote 0

                                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                      With your input, this post could be even better 💗

                                      Register Login
                                      • 1
                                      • 2
                                      • 1 / 2
                                      • First post
                                        Last post
                                      Enjoying MagicMirror? Please consider a donation!
                                      MagicMirror created by Michael Teeuw.
                                      Forum managed by Sam, technical setup by Karsten.
                                      This forum is using NodeBB as its core | Contributors
                                      Contact | Privacy Policy