jeudi 30 juin 2016

draw X charts given an array of X files + add graph title using elements in array + d3


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

And now I have this fiddle where it uses an array to draw 1 chart per each element in the array

arr = ["https://dl.dropboxusercontent.com/u/49714666/data.tsv", "https://dl.dropboxusercontent.com/u/49714666/data2.tsv"]

for (this_file in arr) {

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

    //draw chart code 

    //add a graph title
    svg.append("text")
      .attr("x", (width / 2))
      .attr("y", 0 - (margin.top / 2))
      .attr("text-anchor", "middle")
      .style("font-size", "12px")
      //.style("text-decoration", "underline")  
      .text(arr[this_file] + " - 1");


  });

}

But the way this works is that each chart title gets the name from the 2nd name in the array. What I want is the 1st elment name to be the title of chart 1 and the 2nd element to be the title of chart 2... How can this be done? Or do I have a slight misunderstanding here?

My current workaround is to use a var=ii to get this to work. here is the fiddle but I am just wondering is there a better way, or maybe I need to rewrite it?


Aucun commentaire:

Enregistrer un commentaire