function graph_frame() {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
let spacing = 2
let node_radius = width/(num_states*spacing )
let x_scale = spacing *node_radius;
let x_shift = node_radius*spacing /2 ;
let y_scale = height/num_times;
let y_shift = 0;
function updateLinks(links){
svg.selectAll("line").remove();
let link = svg.selectAll("line");
links.forEach(link => {
link.sourceX_dspl = link.sourceX*x_scale+x_shift;
link.sourceY_dspl = link.sourceY*y_scale+y_shift;
link.targetX_dspl = link.targetX*x_scale+x_shift;
link.targetY_dspl = link.targetY*y_scale+y_shift;
});
link = link.data(links)
.join(enter => enter.append("line")
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr('text', 'A')
//Deal with skips by representing as dashed lines
.style("stroke-dasharray", d=>{
if (d.label=='skip'){
return ("3, 3")}
else{
return ("3, 0")
}})
.style('opacity', d=>{
if (d.label=='skip'){
return (.5)}
else{
return (1)
}})
//X Y location of links
.attr("x1", d => d.sourceX_dspl )
.attr("y1", d => d.sourceY_dspl )
.attr("x2", d => d.targetX_dspl )
.attr("y2", d => d.targetY_dspl ),
// exit => exit.remove()
);
}
function updateTrails( t){
svg.selectAll("rect").remove();
let trail = svg.selectAll("rect");
let trail_shift = node_radius/4
trail = trail
.data( make_nodelayer(idx))
.join(
enter => enter.append("rect")
.attr("x", d => d.x*x_scale+x_shift- node_radius)
.attr("y", d => y_shift-trail_shift)
.attr('width', 2* node_radius)
.attr('height', d=>(t*y_scale)-trail_shift)
.attr('stroke', 'white')
.attr('fill', "transparent")
.style('opacity', .5),
// exit => exit.remove()
);
}
function updateNodes(idx){
svg.selectAll("circle").remove();
let node = svg.selectAll("circle");
node = node
.data( make_nodelayer(idx))
.join(
enter => enter.append("circle")
.attr("stroke", "white")
.attr("fill", "#6699FF")
.attr("r", node_radius)
.attr("cx", d => d.x*x_scale+x_shift)
.attr("cy", d => d.y*y_scale+y_shift),
// .attr("id", (d,i) => "v_"+i),
// exit => exit.remove()
);
}
//Time dependent elemnts (index-dependent)
return t => {
//Assume t is going to be in increments of .333 seconds
var idx = Math.round((t*30)); //unit of input in ms
var links = data_from_cadenza['links'][idx];
//perform updates
updateTrails( idx);
mutable inputtime=idx
if (links.length >0){
updateLinks(links);
}
updateNodes(idx);
return svg.node();
} ;
}