Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple versions of a script on the same page (d3.js)

I need to have multiple versions of a javascript library on the same page. How can I accomplish this, short of manually refactoring one version to avoid naming conflicts?

There are many examples of how to do this with Jquery (example). This seems to rely on jQuery goodness, however. How can I do this for an arbitrary script?

More detail: I'm using d3.js, and I'm plugging in visualizations others have made using d3. The issue is, one of the vizzes requires one version of d3, the other requires a newer version. Both of these vizzes are supposed to be available on the same page - the user swaps which viz is displayed by clicking a thumbnail, and then js is used to hide one viz and build the other. So, it seems like swapping the script rather than loading both in a no-conflict style could also be an option.

like image 432
mobabo Avatar asked Apr 22 '13 20:04

mobabo


People also ask

Is D3 js obsolete?

The JavaScript ecosystem has completely changed during this time, in terms of libraries, best practices and even language features. Nevertheless, D3 is still here. And it's more popular than ever.

What is better than D3 js?

We have compiled a list of solutions that reviewers voted as the best overall alternatives and competitors to D3js, including Syncfusion Essential Studio Enterprise Edition, Chart. Js, Angular, and DevExpress.

Is D3 js hard to learn?

D3 is a very large set of modules with high customizability, so it can seem overwhelming to learn. The difficulty likely depends on an individual's experience with JavaScript, HTML and basic web development. It can be a steep learning curve for new programmers because there may be a lot of new concepts.

What is D3 v3 min js?

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS.


2 Answers

If you take a look at the main d3 source file: https://github.com/mbostock/d3/blob/master/d3.js

you see it begins:

d3 = function() {
  var d3 = {
    version: "3.1.5"
  };
  //.....

So d3 is just an object. I'm not sure if this is the best method, but I think the following would work:

  1. include one version of d3
  2. put the line: d3versionX = d3;
  3. include the next version
  4. same as 2 with different version number.
  5. put d3 = d3versionX where X is your default version for the visualization when the page loads
  6. put an event handler on the thumbnails that trigger the switching of version, and set the d3 variable to the appropriate version number as the first thing that happens.

update with sample code

See this jsbin for a working example. The relevant code:

  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
  <script>
    d3version4 = d3
    window.d3 = null
  </script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
  <script>
    d3version3 = d3
    window.d3 = null
    // test it worked
    console.log('v3', d3version3.version)
    console.log('v4', d3version4.version)
  </script>
like image 127
Jonah Avatar answered Oct 07 '22 20:10

Jonah


I had a similar problem like yours, some visualization needed d3 v3 while others needed v4. This is how I achieved it.

  1. Included d3 v3 in index.html (since most of my visualizations were on v3)
  2. Include require.js script on index.html //you can use cdn path
  3. Go to JavaScript that needs v4.
  4. write the following code at the top.

    function graphicviz(id) {   
        require.config({
            paths: {
                d3: "https://d3js.org/d3.v4.min"
            }
        });
    
        require(["d3"], function(d3) {
            //... D3 Code Here...
        });
    }
    
    //Call The Function
    graphicviz(id); 
    
like image 41
Bhaskar Devgon Avatar answered Oct 07 '22 18:10

Bhaskar Devgon