Skip to content Skip to sidebar Skip to footer

Dynamically Generating Multiple D3 Svg Graphs

I've got an array of objects called graphData (size varies). Each element contains all the information required to create a d3 graph, and I am able to successfully draw the graphs

Solution 1:

Mike Bostock recommends implementing charts as reusable closures with methods. This would be an ideal implementation in your case as you want to have

  • multiple graphs with different data
  • potential reloading with new data (hopefully this is what you mean by dynamic?)

In broad strokes, what you want to do is wrap your code above into a function in very much the same way Mike describes in the post above, and then have data be an attribute of your closure. So here is some badly hacked code:

// your implementation herevar chart = function(){...}

var graphData = d3.json('my/graphdata.json', function(error, data){
  // now you have your data
});

// let's say you have a div called graphsvar myGraphs = d3.select('.graphs')
  .data(graphData)
  .enter()
  .append('g')
  //now you have g elements for each of your datums in the graphData array//we use the saved selection above and call the chart function on each of the elements in the selection
myGraphs.call(chart);

//note that internally in your `chart` closure, you have to take in a selection//object and process it(data is already bound to each of your selections from above):functionchart(selection) {
    selection.each(function(data) {
//...

Here is some more good reading on the topic.

Solution 2:

Well you can try the following approach.

var graphData = data.graph;

//forEach will return each element for the callback, you can then make use  of the e1 to draw the graph.
graphData.forEach(function(e1){


   //graph code goes here.

});

Solution 3:

providing this as your source array

//it's just a single circle in 3, 4var stuff = [3, 4];
var source = [ [stuff, stuff], [stuff] ];

a bit of Array stuff

Array.prototype.max = function() {
    returnMath.max.apply(null, this);
};

Array.prototype.min = function() {
    returnMath.min.apply(null, this);
};

setup:

var dim = [];

source.forEach(function(elem){
    elem.forEach(function(circle){
        dim.push(circle.min());
        dim.push(circle.max());
    });
});

var min = dim.min();
var max = dim.max();

var x = d3.scale.linear()
        .domain([min, max])
        .scale([yourscale]);
var y = d3.scale.linear()
        .domain([min, max])
        .scale([yourscale]);

d3.select('body').selectAll('div')
    .data(source) //first step: a div with an svg foreach array in your array
    .enter()
    .append('div')
    .append('svg')
    .selectAll('circle') //second step: a circle in the svg for each item in your array
    .data(function(d){
        return d; //returns one of the [stuff] arrays
    }).enter()
    .append('circle')
    .attr('r', 5)
    .attr('cx', function(d){
        return x(d[0]);
    })
    .attr('cy', function(d){
        return y(d[1]);
    })
    .style('fill','blue');

Post a Comment for "Dynamically Generating Multiple D3 Svg Graphs"