10 #ifndef EMP_D3_LAYOUT_H 11 #define EMP_D3_LAYOUT_H 18 #include "../../tools/tuple_struct.h" 19 #include "../JSWrap.h" 33 EMP_BUILD_INTROSPECTIVE_TUPLE(
double, x,
73 template <
typename NODE_TYPE = JSONTreeNode>
88 EM_ASM_ARGS({js.objects[$0] = d3.tree();}, this->
id);
104 EM_ASM_ARGS({js.objects[$0] = d3.tree();}, this->
id);
119 this->data = dataset;
145 var root = d3.hierarchy(js.objects[$1][0]);
146 js.objects[$0](root);
147 var nodes = root.descendants();
148 var links = root.descendants().slice(1);
152 var node = js.objects[$3].selectAll(
"g.node")
153 .data(nodes,
function(d) {
return d.name; });
155 var nodeExit = node.exit();
156 var nodeEnter = node.enter().append(
"g")
157 .attr(
"class",
"node")
158 .attr(
"transform",
function(d) {
159 return "translate(" + d.y +
"," + d.x +
")"; });
161 node.attr(
"transform",
function(d) {
162 return "translate(" + d.y +
"," + d.x +
")"; });
164 var link = js.objects[$3].selectAll(
"path.link")
165 .data(links,
function(d) {
return d.name; });
167 var linkExit = link.exit();
169 var linkEnter = link.enter().insert(
"path",
"g")
170 .attr(
"class",
"link")
171 .attr(
"d",
function(d) {
return "M" + d.y +
"," + d.x
172 +
"C" + (d.y + d.parent.y) / 2 +
"," + d.x
173 +
" " + (d.y + d.parent.y) / 2 +
"," + d.parent.x
174 +
" " + d.parent.y +
"," + d.parent.x;})
175 .attr(
"fill",
"none")
176 .attr(
"stroke",
"black")
177 .attr(
"stroke-width", 1);
179 link.attr(
"class",
"link")
180 .attr(
"d",
function(d) {
return "M" + d.y +
"," + d.x
181 +
"C" + (d.y + d.parent.y) / 2 +
"," + d.x
182 +
" " + (d.y + d.parent.y) / 2 +
"," + d.parent.x
183 +
" " + d.parent.y +
"," + d.parent.x;});
185 js.objects[$4] = nodeEnter;
186 js.objects[$5] = nodeExit;
187 js.objects[$6] = linkEnter;
188 js.objects[$7] = linkExit;
189 }, this->
id, data->
GetID(), make_line->
GetID(), svg.
GetID(), node_enter, node_exit, link_enter, link_exit);
190 std::cout <<
"Done generating" <<
std::endl;
198 EM_ASM_ARGS({js.objects[$0].size([$1,$2]);}, this->
id, w, h);
emp::array< Selection, 4 > GenerateNodesAndLinks(Selection svg)
Definition: layout.h:135
D3::LinkGenerator * make_line
Function used to make the lines for the edges in the tree.
Definition: layout.h:82
Layout()
Definition: layout.h:29
TreeLayout(JSONDataset *dataset)
Definition: layout.h:86
TreeLayout()
Default constructor - if you use this you need connect a dataset with SetDataset. ...
Definition: layout.h:102
JSONDataset * data
Pointer to the data - must be in hierarchical JSON format.
Definition: layout.h:79
void SetDataset(JSONDataset *dataset)
Change this TreeLayout's data to [dataset].
Definition: layout.h:118
int id
Definition: d3_init.h:45
void SetSize(int w, int h)
Set the width of the tree area to [w] and the height to [h].
Definition: layout.h:197
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
Definition: svg_shapes.h:325
Definition: selection.h:936
int NextD3ID()
Definition: d3_init.h:31
Tools to build common SVG shapes.
Layout(int id)
Definition: layout.h:28
Tools to maintain data in D3.
int GetID() const
Definition: d3_init.h:96