chart = {
const svg = d3.create("svg")
.style("width", width+margin.left+margin.right)
.style("height", "780")
.attr("font-size", 10)
.attr("font-family", "sans-serif")
.attr("align", "center")
.attr("justify-content", "center")
.attr("text-anchor", "middle")
let allNodesNames = graph.nodes.sort(function(x, y){
return nodes_order.indexOf(x.group) - nodes_order.indexOf(y.group) || d3.descending(x.weight,y.weight); }).map(function(d){return d.label});
var zoomer = d3.zoom().scaleExtent([0.5 , 10]).on("zoom", zoom)
svg.call(zoomer)
.on("zoom", null)
.on("zoom", pan);
const arcGroup = svg
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
const nodeLabels = arcGroup.selectAll("nodeLabels")
.data(graph.nodes.sort(function(x, y){
return nodes_order.indexOf(x.group) - nodes_order.indexOf(y.group) || d3.descending(x.weight,y.weight); }))
.enter().append("text")
.attr("x", d => xScale(d.group)-20)
.attr("y", function(d) {
return node_orders_num[allNodesNames.indexOf(d.label)]
})
.attr("fill", d=> color(d.group))
.style("text-anchor", "end")
.text(d => d.label)
// create the nodes
const nodes = arcGroup.selectAll("nodes")
.data(graph.nodes.sort(function(x, y){
return nodes_order.indexOf(x.group) - nodes_order.indexOf(y.group)|| d3.descending(x.weight,y.weight); }))
.enter().append("circle")
.attr("r", function(d) {
d.weight =
graph.links.filter(function(l) { return l.source == d.id || l.target == d.id}).length;
var minRadius = 2;
return d3.scaleSqrt()
.domain( [minDegree, maxDegree] )
.range( [4, 12] )((d.weight ))
})
.attr("cx", d => xScale(d.group))
.attr("cy", function(d) {
return node_orders_num[allNodesNames.indexOf(d.label)]
})
.attr("fill", d=> color(d.group))
.attr("id", d => d.id)
function buildArc(d) {
// let start = yScale(idToNode[d.source].label);
let start = node_orders_num[allNodesNames.indexOf(idToNode[d.source].label)]
let start2 = xScale(idToNode[d.source].group);
// let end = yScale(idToNode[d.target].label);
let end = node_orders_num[allNodesNames.indexOf(idToNode[d.target].label)]
let end2 = xScale(idToNode[d.target].group)
const arcPath = ['M', start2, start, 'A', Math.abs(start - end)/9+5, ',', Math.abs(start-end)/10.5+5, 0,0,",",
start < end ? 1: 0, end2, end].join('
return arcPath;
}
svg.append("svg:defs").selectAll("marker")
.data(types)
.join("marker")
.attr("id", d => `arrow-${d}`)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 14)
.attr("refY", 0)
.attr("markerWidth", 4)
.attr("markerHeight", 3)
.attr("orient", "auto")
.append("svg:path")
.attr("fill", color_predicate)
.attr("d", 'M0,-5L10,0L0,5');
// arcs
const arcs = arcGroup.selectAll("path")
.data(graph.links)
.enter()
.append("path")
.style("fill", "none")
.attr("stroke", d => color_predicate(d.predicate))
.style("stroke-linecap", "round")
// .attr("marker-end", d => `url(${new URL(`#arrow-${d.predicate}`, location)})`)
.style("stroke-opacity", 0.8);
arcs.text(d => d.sentence)
arcs.attr("d", d => buildArc(d))
nodes.raise()
nodeLabels.raise()
nodes.on('mouseover', function(d,i) {
// d3.select(this).style("fill", d=>color(d.group));
arcs
.style('stroke', function (arcd) {
return arcd.source === d.id || arcd.target === d.id ? d=> color_predicate(d.predicate): d=> color_predicate(d.predicate);})
.style('stroke-width', function (arcd) {
return arcd.source === d.id || arcd.target === d.id ? 3 : 0.8;})
.style('stroke-opacity', function (arcd) {
return arcd.source === d.id || arcd.target === d.id ? 1 : 0.4;})
let nodes_selected = linkedNodes(d.id)
nodeLabels.transition().duration(200)
// .style("fill", d=> color(d.group))
.style("fill", function (nodeLabeld) {
return nodeLabeld.id == d.id || nodes_selected.includes(nodeLabeld.id) ? color(nodeLabeld.group) : "#ccc";})
.style("font-size", function (nodeLabeld) {
return nodeLabeld.id == d.id ? '11.5' : '9';})
.style('font-weight', function (nodeLabeld) {
return nodeLabeld.id == d.id || nodes_selected.includes(nodeLabeld.id) ? 'bold' : 'normal';})
// nodes.style('fill', nodes_selected.includes(d.id)? color(d.group) : "#ccc")
nodes.transition().duration(300)
.style('fill', function (nodeLaFill) {
return nodeLaFill.id == d.id || nodes_selected.includes(nodeLaFill.id) ? color(nodeLaFill.group) : "#ccc";})
d.try = 100
});
nodes.on('mouseout', function (d) {
nodes.style("fill", d=> color(d.group))
nodeLabels.style("fill", d=> color(d.group))
nodeLabels.style( "font-size", "9")
nodeLabels.style('font-weight', "normal")
nodes.transition().duration(300).style("fill", d=> color(d.group));
arcs.style('stroke', d=>color_predicate(d.predicate));
arcs.style('stroke-width', 1);
arcs.style("stroke-opacity", 0.8);
});
function zoom() {
arcGroup.attr("transform", d3.event.transform);
}
function pan() {
zoomer.translateBy(svg.transition().duration(100), d3.event.wheelDeltaX, d3.event.wheelDeltaY);
}
return svg.node();
}