d3.js - How do I pass bound data to a function in d3? -


i trying make small multiples chart following mike bostock's example.

this example uses enter().append("svg") create new svg each data point. in each svg create chart.

i have data in csv file looks this:

count, radius   15, 5   10, 3 

with data i'd create 2 svgs (one each data point), first 1 containing 15 circles each radius of 5, , second svg containg 10, each radius of 3. have function drawcircles wish use draw circles based on dataset, i'm having trouble passing data through function.

here's code:

d3.csv("nations.csv", function(data) { var svg = d3.select("body").selectall("svg")   .data(data)   .enter().append("svg")   .attr("width", width + margin.left + margin.right)   .attr("height", height + margin.top + margin.bottom);    drawcircles(function (d) {return +d.count;}, function (d) {return     +d.radius;})  

i can't seem pass d.count , d.radius through arguments drawcircles function. can please help?

here's very d3ish way of doing after:

  d3.csv("some.csv", function(d){       // coerce data numbers       return {         count: +d.count,         radius: +d.radius       }      },     function(data){               // create svg each row of data       var s = d3.selectall("svg")         .data(data)         .enter()         .append("svg")         .attr("width", 600)         .attr("height", 100);        // use sub-selection create circle each count       s.selectall('circle')         .data(function(d){           // bound data array repeating radius           return d3.range(d.count).map(function(_){ return d.radius });         })         .enter()         .append('circle')         // radius same each circle         .attr('r', function(d){           return d;         })         // space circles good;         .attr('cx', function(d,i,j){           return ((d + 2) * 2) * + 10;         })         .attr('cy', 50)         .style('fill', 'steelblue');           }); 

example here.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -