An exploration of dynamic reactivity at the template level within the Meteor

In my current project, I am facing a challenge where I need to present data in a dashboard both as a chart using the perak:c3 package and as a table using the aslagle:reactive-table package. The issue arises because the data is retrieved from a MongoDB collection in a format that works well with c3 for plotting but needs to be converted into a local collection for use with reactive-table, as mentioned in this previous question's answer.

Whenever I update the dataset to be displayed, I expect both the chart and the table to reflect these changes. However, altering the values in the local collection causes a delay, resulting in a freeze on the page before the updated data is shown.

To easily replicate the problem, I have set up a sample project on GitHub here. By selecting a dataset in your browser, you can experience firsthand what I am describing.

https://i.sstatic.net/BgxTY.png

To observe the desired reactive behavior in the chart, visit client/templates/dashboard/dashboard.html and comment out the table template {{> dashboardTable}}

https://i.sstatic.net/XFdLU.png

Then, switch the dataset to witness how the chart smoothly updates without any freezing. My goal is to ensure that both templates, dashboardChart and dashboardTable, render independently of each other.


UPDATE

After taking Michael Floyd’s advice to introduce a timeout, there was some improvement, observed via the following code snippet:

Meteor.setTimeout(function(){createLocalCollection(data)},200);

However, while the chart redraws smoothly, once the table population completes, the chart undergoes another drawing process. It seems like it transitions to an intermediate state that is unclear to me. A video showcasing this phenomenon can be viewed here.

Answer №1

Here is a different perspective that I wanted to share as a separate answer.

Implement the onRendered callback in d3 to trigger the local collection update.

Instead of just having:

chart = c3.generate({
  bindto: '#dataset-chart',

inside dashboard_chart.js, modify it to include:

chart = c3.generate({
  onrendered: createLocalCollection(),
  bindto: '#dataset-chart',

Remember to remove createLocalCollection(data) from your event handler.

To simplify passing data context through the onrendered handler in d3, update your createLocalCollection function to utilize the reactive variable

datasetID</code defined earlier for identifying the current dataset:</p>

<pre><code>var createLocalCollection = function() {
    var values = My_First_Collection.find({datasetID: datasetID.get()}).fetch();
    var tempDoc = {};
    local.remove({});
    tempDoc = {};
    for (var i in values[0].value) {
      tempDoc.value = values[0].value[i];
      tempDoc.date = values[0].date[i];
      local.insert(tempDoc);
    }
};

This approach allows D3 to signal when chart rendering completes, enabling instant chart updates followed by table population without dealing with timeouts.

Answer №2

In the world of JavaScript, it's important to remember that it operates on a single thread. This means that tasks will be executed sequentially. To overcome this limitation, consider using defer and delaying the code responsible for updating your local collection with Meteor.setTimeout(). By doing so, you can ensure that the chart updates first before the table is updated. This approach helps avoid situations where rendering functions such as d3 may cause delays in screen updates due to lengthy JavaScript processes.

I personally tested this method and found that it significantly improved the performance of my charts.

Meteor.setTimeout(function(){createLocalCollection(data)},500);

Reducing the delay time to 100 milliseconds allowed the chart to update smoothly, although I encountered an issue where the menu did not fully fade out until the local collection had finished updating.

Another useful tip when dealing with tables and local collections is to update the local collection only when the corresponding non-local document needs to be displayed on the screen. This approach ensures that the reactive table loads lazily, enhancing overall performance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Javascript code to eliminate the final appearance of a comma within a span element

I am trying to add object keys to a span tag with the class result, separating them with commas. However, I am struggling to remove the last comma in the text. Result: <span class="result"></span> JavaScript: for (key in obj) { $(".resul ...

Wordpress problem with Bootstrap JavaScript code involving data-toggle="collapse"

Currently facing an issue with my project. This is my first attempt at creating a WordPress blog, inspired by the HTML site www.humantools.com.mx and building a blog for it at www.humantools.com.mx/blog. There's a strange problem: when logged in, the ...

Transform Array into JSON using AngularJS

My array consists of three elements var data = []; data["username"]=$scope.username; data["phoneNo"]=$scope.phoneNo; data["altPhoneNo"]=$scope.altPhoneNo; I need to send this data to the server in JSON format, so I used var jsonData = JSON.str ...

Link to Reply Comment Feature in Wordpress

It seems like I've tried numerous solutions to fix my issue but haven't had any luck, so now I'm seeking help here. Despite trying methods that have worked for others, they do not seem to work for me. My problem lies with nested comments on ...

Storing data in MongoDB using JavaScript on a web platform

Imagine a straightforward website with a common structure <html> <head></head> <body> <script type="text/javascript"> </script> </body> </html> Can data be written to MongoDB fr ...

"MongoDB fails to save changes when attempting to remove an item from an updated

I've been using Angular Fullstack for a web application. When I send my data using $http.post(), I include the following object: { title: "Some title", tags: ["tag1", "tag2", "tag3"] } However, when I edit the object and try to update it with $http ...

Troubleshooting: Bootstrap 4 Flexbox demos failing to function

I am currently in the process of developing a basic Express webserver that serves a landing page constructed with bootstrap. However, I am encountering issues with getting any Flexbox examples to properly function. Displayed below is my landing page. Is t ...

Tips for showcasing the currently active item in a Bootstrap dropdown menu using jQuery

I am trying to highlight the menu item that is selected in a drop-down menu. I attempted the following code, which works if I prevent the default behavior of the link element, but I do not want to do that. So I tried using local storage instead, but it d ...

Update the color of buttons after being clicked - Bootstrap

If I want to change the class "btn-success" to "btn-warning" or update the color code to "#ffc107" upon a successful process, how can I achieve that? Button ; <button type="submit" name="teklifver" value="Teklif Ver" class="btn btn-block btn-success" ...

Real-world demonstration of multiple screens using three.js for an immersive virtual experience

Check out these awesome examples of multiscreen capabilities: webgl_multiple_canvases_circle webgl_multiple_canvases_complex webgl_multiple_canvases_grid These examples even reference the Google Liquid Galaxy project: liquidGalaxy I am exploring how t ...

Transforming a two-digit year into a four-digit year

Our entry form provides users with a calendar drop-down option, but they are also able to manually type in dates. We recently discovered that if someone enters a 2-digit year, it automatically changes to 1912 instead of 2012 (as dates cannot be in the past ...

Exploring different techniques to display images in React JS using mapping or looping?

This is my custom JavaScript file that includes images for my project. import React, { Component } from 'react'; import './Stopka.css'; class Stopka extends Component { render() { return ( <div clas ...

Selecting items from a list at random using the power of math.random() and math.floor()

When attempting to randomize the popping of elements in a list stored in the baby variable, only baby[1] is being popped. How can I make it pop random elements? <body> <h1 id="1">bebe</h1> <h1 id="2">t ...

Incorporating isotope with the stylish fancybox

Hello, I am a French speaker so please excuse any mistakes in my English. I hope my explanations are clear: I have been trying to integrate Fancybox2 with isotope, but despite extensive research, I am still unable to get them to work together. The issue I ...

Error: The 'document' object is not recognized in this context (React)

I attempted to display my main page using routes but I am still encountering the same ReferenceError. Below is my code snippet: import React from "react"; import ReactDOM from "react-dom/client"; import App from "../components/app ...

Creating promises using the latest Breeze Angular Service: A comprehensive guide

In the process of updating my angular/breeze application, I wanted to incorporate the new breeze angular service for improved performance and functionality. The documentation guided me on removing the Q.js file and other dependencies, but I've encount ...

Secure access to an API using a certificate within a Vue.js application running on localhost

My vue.js app is built using vue-cli. The application is hosted at dev.example.com and the REST API can be found at dev.example.com/api/v1/. The backend has added an SSL certificate for security on the development environment. However, when I try to make a ...

The inserted button's click event does not trigger the contentEditable DIV

I have a contentEditable DIV that I manage using the directive below: <div class="msg-input-area" [class.focused]="isMsgAreaFocused" contenteditable [contenteditableModel]="msgText" (contenteditableModelChang ...

The combination of Three.js and React

Hello everyone! I am completely new to Three.js and I'm currently attempting to integrate it with React. While looking for resources, I came across this helpful medium article on the topic: Starting with React 16 and Three.js in 5 minutes My goal is ...

Troubleshooting: WordPress integration with PhoneGap not processing JSON data

I've been following a tutorial and I'm encountering some issues with the examples provided. Despite my attempts to run the examples, I am not seeing any results: app.html <!DOCTYPE HTML> <html> <header> <script src="https:/ ...