javascript - Can I use d3.scales to evenly distribute circles of varying size -


i interested in understanding how use d3.js evenly distribute svg circles of varying size using 1 of d3's scale functions. right now, have code creates circles; , using d3.scale.ordinal() x-scale , circles evenly spaced numerically (from center of 1 circle center of other), not visually. teaching myself javascript , appreciate can offer.

var margin = {top: 20, right: 20, bottom: 20, left: 20},     w = 700 -  margin.left - margin.right,     h = 200 - margin.top - margin.bottom;   var numbers = [86, 57, 112, 293, 75, 1, 45];   var xscale = d3.scale.ordinal()     .domain(d3.range(numbers.length))      .rangeroundbands([0, w], .8);    var rscale = d3.scale.sqrt()     .domain([0, d3.max(numbers)])     .range([10, 60]);        var svg = d3.select("body")     .append("svg")     .attr("width", w + margin.left + margin.right)     .attr("height", h + margin.top + margin.bottom);   svg.selectall("circle")      .data(numbers)     .enter().append("circle")      .attr("cy", h/2)      .attr("cx", function(d,i) { return xscale(i); })      .attr("r", function(d) { return rscale(d); })      .style("fill", "#ccc");   svg.selectall("text")     .data(numbers)     .enter().append("text")     .text(function(d){ return d; })     .attr("y", h/2 + 5 )     .attr("x", function(d,i){ return xscale(i); })     .attr("font-family", "sans-serif")     .attr("font-size", "11px")     .attr("text-anchor", "middle")     .style("fill", "#000000"); 

i think more elegant define scale circle centers - way, use both circles , text. note doesn't replace xscale, rather appears before it.

calculate sum of circlesdiameters using thenumbers` array (it twice sum; see this). call result totaldiams.

now define xcenterscale like

var xcenterscale = d3.scale.ordinal()     .domain(d3.range(numbers.length))      .range([numbers[0], totaldiams - numbers[numbers.length - 1]);  

now center circle i xscale(xcenterscale(i)).


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -