Forum Discussion
alpayne
7 years agoFrequent Visitor
Network (force-directed) visual using power BI d3 custom visual
Hi All, I'm having trouble getting a force-directed visual to display in Power BI, I think its related to how the data is referenced. I can get it working outside of Power Bi but just can't get it ...
Anonymous
4 years agoNot applicable
I contacted the developer and was able to get the answer on how to do this. More info was needed in the declaration pbi.dsv line.
var margin = {top: 10, right: 30, bottom: 30, left: 40},
width = pbi.width - margin.left - margin.right,
height = pbi.height - margin.top - margin.bottom;
// append the svg object to chart
var svg = d3.select("#chart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
var force = d3.layout.force()
.gravity(0.1)
.distance(100)
.charge(-700)
.size([width,height]);
var color = function(group) {
if (group==1) {return "#aaa"}
else if (group==2) {return "#fbc280"}
else {return "#405275"}};
var links = [];
var nodes = [];
pbi.dsv(function(d) {return {name:d.name, group:Number(d.group),source:Number(d.source), target:Number(d.target)};}, function(data) {
for(var i = 0;i < 10; i++){
links.push({"source":data[i].source,
"target":data[i].target
})
nodes.push({"name":data[i].name,
"group":data[i].group
})
};
var dat = {"nodes":nodes, "links":links}
force
.nodes(dat.nodes)
.links(dat.links)
.start();
var link = svg.selectAll(".link")
.data(dat.links)
.enter()
.append("line")
.attr("class","link");
var node = svg.selectAll(".node")
.data(dat.nodes)
.enter()
.append("g")
.attr("class", "node")
.call(force.drag);
node
.append('circle')
.attr('r', 13)
.attr('fill', function (d) {
return color(d.group);});
node
.append("text")
.attr("dx", -18)
.attr("dy", 8)
.style("font-family", "allan")
.style("font-size", "18px")
.text(function (d) {
return d.name});
force.on("tick", function () {
link
.attr("x1", function (d) {
return d.source.x;})
.attr("y1", function (d) {
return d.source.y;})
.attr("x2", function (d) {
return d.target.x;})
.attr("y2", function (d) {
return d.target.y;});
node
.attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";});
});
});
To get the links to show you will also need to add some CSS to that tab:
.link {
stroke-width: 1px;
stroke: #ccc;
}