javascript - Set position of polygons and paths in d3 -
javascript - Set position of polygons and paths in d3 -
i have several external svg files want read in , position. of svg files polygons, others paths. of them, specific random amount translate files when read them in. want assign position couple of shapes. (jsfiddle).
class="lang-js prettyprint-override">var width = 300, height = 300; var samplesvg = d3.select("body") .append("svg") .attr( {width: width, height: height} ); var shapes = [ {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-01.svg", number: 1, color: 'red'}, {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-02.svg", number: 2, color: 'yellow'}, {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-03.svg", number: 3, color: 'orange'}, {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-04.svg", number: 4, color: 'green'}, {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-05.svg", number: 5, color: 'blue'}, {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-06.svg", number: 6, color: 'purple'}, {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-07.svg", number: 7, color: 'red'}, {url:"https://dl.dropboxusercontent.com/u/2467665/shapes/shapes-08.svg", number: 8, color: 'orange'}]; var q = queue(); shapes.foreach(function(shape) { q.defer(d3.xml, shape.url, "image/svg+xml"); }); q.awaitall(function (error, results) { samplesvg.selectall('g.shape').data(shapes) // g tag created positioning shape @ random location .enter().append('g').attr('class', 'shape') .attr('transform', function () { homecoming 'translate(' + math.random() * (w - 50) + ',' + math.random() * (h - 50) + ')' }) .each(function (d, i) { // loaded svg file appended within g tag this.appendchild(results[i].documentelement); d3.select(this).select('svg').select("*") .attr("stroke", function () {return d.color;}) .attr('transform', function () { if (d.number > 6) { var newx = 200, newy = 200; //this want set position homecoming 'scale(0.3) translate(0,0)'} else {return 'scale(0.1)'} ;}) }) });
basically, want shapes have different random positions except 2 shapes (those d.number > 6
) want appear @ (200,200).
is there way either reset translate
, run 1 time again new amounts, or set position of both paths , polygons?
still unclear, think you're looking for:
q.awaitall(function (error, results) { samplesvg.selectall('g.shape') .data(shapes) .enter() .append('g') .attr('class', 'shape') .each(function (d, i) { // loaded svg file appended within g tag this.appendchild(results[i].documentelement); d3.select(this).select('svg').select("*") .attr("stroke", d.color); }) .attr('transform', function (d, i) { // in general, in here we're setting transform of <g>, contains svg. so: if (d.number > 6) { var newx = 200, newy = 200; // homecoming here scale , translate <g> (and svg within it) homecoming 'scale(0.3) translate(0,0)'; } else { homecoming 'translate(' + math.random() * (w - 50) + ',' + math.random() * (h - 50) + ') scale(0.1)'; } }); });
javascript svg d3.js
Comments
Post a Comment