d3.js - D3 - How to bind the same data to multiple object? -
i trying draw multiple shapes using same data in selection, let's have:
mydata = [ { shape: 'rect', attr: { width: 100, height: 100, x: 100, y:100 } } , { shape: 'circle', attr: {cx, cy, etc...} } ] node.selectall('*').data([mydata]); mydata.obj.foreach(function(obj) { // append different shapes based on data var shape = node.enter().append(obj.shape); object.keys(obj.attrs).foreach(function(attr) { // bind attrs shapes based on data shape.attr(attr, obj.attrs[attr]); }); }); here node 'g' element, mydata single data object. goal modify child shapes inside g based on mydata, later if bind mydata , call function again, can updated. believe somehow mydata bound first appended shape. there way bind same data multiple shapes?
perhaps, want here .datum() function in d3. 1 of it's specific use-cases bind same datum multiple dom elments (i.e. bind 1 datum entire d3 selection).
for example, d3.selectall('div').datum({foo: 'bar'}) bind same object {foo: 'bar'} of <div>...</div> elements existing on document.
quoting directly https://github.com/mbostock/d3/wiki/selections#datum
selection.datum([value])...ifvaluespecified, sets element's bound data specified value on selected elements. ifvalueconstant, elements given same data [sic]
(somewhat ironically, refers constant datum "data" in explanation of .datum() function!)
however, literal answer question; there may more design-oriented answer question might explain "conventional" d3-way of handling overall objective... in case, should consult tutorials like
Comments
Post a Comment