(beginner D3 is here)
I have the following snippet:
// myShape (node) group // NB: the function arg is crucial here! nodes are known by id, not by index! myShape = myShape.data(nodes, function(d) { return d.nodeId; }); // update existing nodes (reflexive & selected visual states) myShape.selectAll('circle') .style('fill', function(d) { return (d === selected_node) ? d3.rgb(colors(d.nodeType)).brighter().toString() : colors(d.nodeType); }) .classed('reflexive', function(d) { return d.reflexive; }); // add new nodes var g = myShape.enter().append('svg:g'); g.append('svg:circle') .attr('r', 12)
But I would like to make it more flexible: instead of using only circles, I would like to use circles and polygons. This will be selected in property d:
var d = [ { nodeId: 1, nodeType : 'type1' , shape: 'circle' }, { nodeId: 2, nodeType : 'type2' , shape: 'triangle' }, ];
This means depending on d.shape. I have to set 'svg: circle' or 'svg: polygon' and then set the radius (for the circle) or the point (for the polygons). I tried to set the svg form as follows:
g.append(function (d) { if (d.shape === 'circle' ) { return 'svg:circle'; } else { return 'svg:polygon'; } } )
But this does not work: I get:
Uncaught Error: NotFoundError: DOM Exception 8
append doesn't seem to accept a function? How to set svg form based on node -by-node?
EDIT
I prepared this jsbin to show what I want to do.
dangonfast
source share