vendredi 17 juin 2016

given 1 chart in d3 fed by 1 file + want to draw X charts given an array of X files + d3

this is my fiddle, which I am building on from this example

This is the main code to draw 1 chart. I am using an aray with 2 files for this example.

//array with 2 files(the same) just for example
arr = ["https://dl.dropboxusercontent.com/u/49714666/data.tsv", "https://dl.dropboxusercontent.com/u/49714666/data.tsv"]

var svg = d3.select("body").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.tsv(arr[0], type, function(error, data) {
  if (error) throw error;

  x.domain(data.map(function(d) {
    return d.letter;
  }));
  y.domain([0, d3.max(data, function(d) {
    return d.frequency;
  })]);

  svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

  svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", ".71em")
    .style("text-anchor", "end")
    .text("Frequency");

  svg.selectAll(".bar")
    .data(data)
    .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) {
      return x(d.letter);
    })
    .attr("width", x.rangeBand())
    .attr("y", function(d) {
      return y(d.frequency);
    })
    .attr("height", function(d) {
      return height - y(d.frequency);
    });


  svg.selectAll(".barText")
    .data(data)
    .enter().append("text")
    .attr("class", "barText")
    .attr("x", function(d) {
      return x(d.letter);
    })
    .attr("y", function(d) {
      return y(d.frequency);
    })
    .text(function(d) {
      return d.frequency;
    });

});

Now if I want to add a 2nd chart I have to add the following code, and this works. Note I have to declare a new svg svg2 and then append to this.

var svg2 = d3.select("body").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.tsv(arr[1], type, function(error, data) {
  if (error) throw error;

  x.domain(data.map(function(d) {
    return d.letter;
  }));
  y.domain([0, d3.max(data, function(d) {
    return d.frequency;
  })]);

  svg2.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

  svg2.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", ".71em")
    .style("text-anchor", "end")
    .text("Frequency");

  svg2.selectAll(".bar")
    .data(data)
    .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) {
      return x(d.letter);
    })
    .attr("width", x.rangeBand())
    .attr("y", function(d) {
      return y(d.frequency);
    })
    .attr("height", function(d) {
      return height - y(d.frequency);
    });


  svg2.selectAll(".barText")
    .data(data)
    .enter().append("text")
    .attr("class", "barText")
    .attr("x", function(d) {
      return x(d.letter);
    })
    .attr("y", function(d) {
      return y(d.frequency);
    })
    .text(function(d) {
      return d.frequency;
    });

});

Now this is not great practice as I am rewriting alot the same code. But I want to be able to draw X charts giving an array of X elements of filenames. The files would have the same format.

Can anyone advise, point me in the right direction, on how to do this?

I have endeavoured to find a solution but I have come unstuck. I thought using a for loop, but I think the d3.tsv function is my issues as it is waiting for the data for the file, a callback?.

Aucun commentaire:

Enregistrer un commentaire