While trying to use the tooltip for each ring in a multiple ring donut chart using d3js , while hovering on the ring it just shows the values for the first child . Below is the code which shows the rings as well as the tooltip . What would I be doing in order to get all the correct item on hover.How do I iterate through the json to get the correct tooltip.
var dataset = [{
"name": "Population Quater",
"code": "POP_QUATER",
"parent": "POP_BY_QUAT",
"children": [{
"name": "POP_CYQ1",
"code": "POP_CYQ1",
"parent": "POP_QUATER",
"value": "6772",
"label": "CYQ1",
"children": []
}, {
"name": "POP_CYQ2",
"code": "POP_CYQ2",
"parent": "POP_QUATER",
"value": "6716",
"label": "CYQ2",
"children": []
}, {
"name": "POP_CYQ3",
"code": "POP_CYQ3",
"parent": "POP_QUATER",
"value": "6714",
"label": "CYQ3",
"children": []
}, {
"name": "POP_CYQ4",
"code": "POP_CYQ4",
"parent": "POP_QUATER",
"value": "6703",
"label": "CYQ4",
"children": []
}, {
"name": "POP_LYQ1",
"code": "POP_LYQ1",
"parent": "POP_QUATER",
"value": "6721",
"label": "LYQ1",
"children": []
}, {
"name": "POP_LYQ2",
"code": "POP_LYQ2",
"parent": "POP_QUATER",
"value": "6671",
"label": "LYQ2",
"children": []
}, {
"name": "POP_LYQ3",
"code": "POP_LYQ3",
"parent": "POP_QUATER",
"value": "6708",
"label": "LYQ3",
"children": []
}, {
"name": "POP_LYQ4",
"code": "POP_LYQ4",
"parent": "POP_QUATER",
"value": "6734",
"label": "LYQ4",
"children": []
}]
}, {
"name": "Transient Pop",
"code": "TRANSIENT_POP",
"parent": "POP_BY_QUAT",
"label": "Transient Pop",
"children": [{
"name": "TRANSIENT_LYQ1",
"code": "TRANSIENT_LYQ1",
"parent": "TRANSIENT_POP",
"value": "54",
"label": "LYQ1",
"children": []
}, {
"name": "TRANSIENT_LYQ2",
"code": "TRANSIENT_LYQ2",
"parent": "TRANSIENT_POP",
"value": "86",
"label": "LYQ2",
"children": []
}, {
"name": "TRANSIENT_LYQ3",
"code": "TRANSIENT_LYQ3",
"parent": "TRANSIENT_POP",
"value": "219",
"label": "LYQ3",
"children": []
}, {
"name": "TRANSIENT_LYQ4",
"code": "TRANSIENT_LYQ4",
"parent": "TRANSIENT_POP",
"value": "191",
"label": "LYQ4",
"children": []
}, {
"name": "TRANSIENT_CYQ1",
"code": "TRANSIENT_CYQ1",
"parent": "TRANSIENT_POP",
"value": "52",
"label": "CYQ1",
"children": []
}, {
"name": "TRANSIENT_CYQ2",
"code": "TRANSIENT_CYQ2",
"parent": "TRANSIENT_POP",
"value": "91",
"label": "CYQ2",
"children": []
}, {
"name": "TRANSIENT_CYQ3",
"code": "TRANSIENT_CYQ3",
"parent": "TRANSIENT_POP",
"value": "222",
"label": "CYQ3",
"children": []
}, {
"name": "TRANSIENT_CYQ4",
"code": "TRANSIENT_CYQ4",
"parent": "TRANSIENT_POP",
"value": "186",
"label": "CYQ4",
"children": []
}]
}, {
"name": "Seasonal Pop",
"code": "SEASONAL_POP",
"parent": "POP_BY_QUAT",
"label": "Seasonal Pop",
"children": [{
"name": "SEASONAL_LYQ1",
"code": "SEASONAL_LYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "LYQ1",
"children": []
}, {
"name": "SEASONAL_LYQ2",
"code": "SEASONAL_LYQ2",
"parent": "SEASONAL_POP",
"value": "24",
"label": "LYQ2",
"children": []
}, {
"name": "SEASONAL_LYQ3",
"code": "SEASONAL_LYQ3",
"parent": "SEASONAL_POP",
"value": "152",
"label": "LYQ3",
"children": []
}, {
"name": "SEASONAL_LYQ4",
"code": "SEASONAL_LYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "LYQ4",
"children": []
}, {
"name": "SEASONAL_CYQ1",
"code": "SEASONAL_CYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "CYQ1",
"children": []
}, {
"name": "SEASONAL_CYQ2",
"code": "SEASONAL_CYQ2",
"parent": "SEASONAL_POP",
"value": "22",
"label": "CYQ2",
"children": []
}, {
"name": "SEASONAL_CYQ3",
"code": "SEASONAL_CYQ3",
"parent": "SEASONAL_POP",
"value": "161",
"label": "CYQ3",
"children": []
}, {
"name": "SEASONAL_CYQ4",
"code": "SEASONAL_CYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "CYQ4",
"children": []
}]
}];
var width = 460,
height = 300,
cwidth = 25;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var pie = d3.pie()
.sort(null).value(function(d) {
return d.value; //since score is the parameter for the pie
});
var arc = d3.arc();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var gs = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("class", "arc").on("mouseover", function() {
tooltip.style("display", null);
})
.on("mouseout", function() {
tooltip.style("display", "none");
})
.on("mousemove", function(d, i) {
tooltip.transition().duration(200)
.style("opacity", 0.9);
tooltip.select("div").html(d.children[i].name + ":" + " <strong>" + d.children[i].value + "</strong>")
.style("position", "fixed")
.style("text-align", "center")
.style("width", "120px")
.style("height", "45px")
.style("padding", "2px")
.style("font", "12px sans-serif")
.style("background", "lightsteelblue")
.style("border", "0px")
.style("border-radius", "8px")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
});
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0.5);
tooltip.append("rect")
.attr("width", 30)
.attr("height", 20)
.attr("fill", "#ffffff")
.style("opacity", 0.5);
tooltip.append("div")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "1.5em")
.attr("font-weight", "bold");
var path = gs.selectAll("path")
.data(function(d, i) {
return pie(d.children).map(function(payload) {
return {
payload: payload,
parentIndex: i
}
})
})
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", function(d, i, j) {
return arc.innerRadius(10 + cwidth * d.parentIndex).outerRadius(cwidth * (d.parentIndex + 1))(d.payload);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
var dataset = [{
"name": "Population Quater",
"code": "POP_QUATER",
"parent": "POP_BY_QUAT",
"children": [{
"name": "POP_CYQ1",
"code": "POP_CYQ1",
"parent": "POP_QUATER",
"value": "6772",
"label": "CYQ1",
"children": []
}, {
"name": "POP_CYQ2",
"code": "POP_CYQ2",
"parent": "POP_QUATER",
"value": "6716",
"label": "CYQ2",
"children": []
}, {
"name": "POP_CYQ3",
"code": "POP_CYQ3",
"parent": "POP_QUATER",
"value": "6714",
"label": "CYQ3",
"children": []
}, {
"name": "POP_CYQ4",
"code": "POP_CYQ4",
"parent": "POP_QUATER",
"value": "6703",
"label": "CYQ4",
"children": []
}, {
"name": "POP_LYQ1",
"code": "POP_LYQ1",
"parent": "POP_QUATER",
"value": "6721",
"label": "LYQ1",
"children": []
}, {
"name": "POP_LYQ2",
"code": "POP_LYQ2",
"parent": "POP_QUATER",
"value": "6671",
"label": "LYQ2",
"children": []
}, {
"name": "POP_LYQ3",
"code": "POP_LYQ3",
"parent": "POP_QUATER",
"value": "6708",
"label": "LYQ3",
"children": []
}, {
"name": "POP_LYQ4",
"code": "POP_LYQ4",
"parent": "POP_QUATER",
"value": "6734",
"label": "LYQ4",
"children": []
}]
}, {
"name": "Transient Pop",
"code": "TRANSIENT_POP",
"parent": "POP_BY_QUAT",
"label": "Transient Pop",
"children": [{
"name": "TRANSIENT_LYQ1",
"code": "TRANSIENT_LYQ1",
"parent": "TRANSIENT_POP",
"value": "54",
"label": "LYQ1",
"children": []
}, {
"name": "TRANSIENT_LYQ2",
"code": "TRANSIENT_LYQ2",
"parent": "TRANSIENT_POP",
"value": "86",
"label": "LYQ2",
"children": []
}, {
"name": "TRANSIENT_LYQ3",
"code": "TRANSIENT_LYQ3",
"parent": "TRANSIENT_POP",
"value": "219",
"label": "LYQ3",
"children": []
}, {
"name": "TRANSIENT_LYQ4",
"code": "TRANSIENT_LYQ4",
"parent": "TRANSIENT_POP",
"value": "191",
"label": "LYQ4",
"children": []
}, {
"name": "TRANSIENT_CYQ1",
"code": "TRANSIENT_CYQ1",
"parent": "TRANSIENT_POP",
"value": "52",
"label": "CYQ1",
"children": []
}, {
"name": "TRANSIENT_CYQ2",
"code": "TRANSIENT_CYQ2",
"parent": "TRANSIENT_POP",
"value": "91",
"label": "CYQ2",
"children": []
}, {
"name": "TRANSIENT_CYQ3",
"code": "TRANSIENT_CYQ3",
"parent": "TRANSIENT_POP",
"value": "222",
"label": "CYQ3",
"children": []
}, {
"name": "TRANSIENT_CYQ4",
"code": "TRANSIENT_CYQ4",
"parent": "TRANSIENT_POP",
"value": "186",
"label": "CYQ4",
"children": []
}]
}, {
"name": "Seasonal Pop",
"code": "SEASONAL_POP",
"parent": "POP_BY_QUAT",
"label": "Seasonal Pop",
"children": [{
"name": "SEASONAL_LYQ1",
"code": "SEASONAL_LYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "LYQ1",
"children": []
}, {
"name": "SEASONAL_LYQ2",
"code": "SEASONAL_LYQ2",
"parent": "SEASONAL_POP",
"value": "24",
"label": "LYQ2",
"children": []
}, {
"name": "SEASONAL_LYQ3",
"code": "SEASONAL_LYQ3",
"parent": "SEASONAL_POP",
"value": "152",
"label": "LYQ3",
"children": []
}, {
"name": "SEASONAL_LYQ4",
"code": "SEASONAL_LYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "LYQ4",
"children": []
}, {
"name": "SEASONAL_CYQ1",
"code": "SEASONAL_CYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "CYQ1",
"children": []
}, {
"name": "SEASONAL_CYQ2",
"code": "SEASONAL_CYQ2",
"parent": "SEASONAL_POP",
"value": "22",
"label": "CYQ2",
"children": []
}, {
"name": "SEASONAL_CYQ3",
"code": "SEASONAL_CYQ3",
"parent": "SEASONAL_POP",
"value": "161",
"label": "CYQ3",
"children": []
}, {
"name": "SEASONAL_CYQ4",
"code": "SEASONAL_CYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "CYQ4",
"children": []
}]
}];
var width = 460,
height = 300,
cwidth = 25;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var pie = d3.pie()
.sort(null).value(function(d) {
return d.value; //since score is the parameter for the pie
});
var arc = d3.arc();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var gs = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("class", "arc").on("mouseover", function() {
tooltip.style("display", null);
});
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0.5);
tooltip.append("rect")
.attr("width", 30)
.attr("height", 20)
.attr("fill", "#ffffff")
.style("opacity", 0.5);
tooltip.append("div")
.attr("x", 15)
.attr("dy", "1.2em")
.style("text-anchor", "middle")
.attr("font-size", "1.5em")
.attr("font-weight", "bold");
var path = gs.selectAll("path")
.data(function(d, i) {
return pie(d.children).map(function(payload) {
return {
payload: payload,
parentIndex: i
}
})
})
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", function(d, i, j) {
return arc.innerRadius(10 + cwidth * d.parentIndex).outerRadius(cwidth * (d.parentIndex + 1))(d.payload);
})
.on("mouseout", function() {
tooltip.style("display", "none");
})
.on("mousemove", function(d, i) {
console.log(d)
tooltip.transition().duration(200)
.style("opacity", 0.9);
tooltip.select("div").html(d.payload.data.name + ":" + " <strong>" + d.payload.data.value + "</strong>")
.style("position", "fixed")
.style("text-align", "center")
.style("width", "120px")
.style("height", "45px")
.style("padding", "2px")
.style("font", "12px sans-serif")
.style("background", "lightsteelblue")
.style("border", "0px")
.style("border-radius", "8px")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
The event handlers were bound to the g tags instead of the path tags :)
try this
Related
Below example converted from this v3 version, it try to use force layout to draw some nodes and links, but it can not show the nodes or links!
console.log("--------")
console.clear();
var w = 600,
h = 600,
nodeCircles,
linkLines,
root;
var force = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(w / 2, h / 2))
.on('tick',tick)
var zoomer = d3.zoom()
.scaleExtent([0.9,3])
.on("zoom", zoom);
function zoom(event) {
vis.attr("transform",
"translate(" + event.translate + ")"
+ " scale(" + event.scale + ")" );
}
var graph = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("class", "graph")
.call(zoomer);
var rect = graph.append("rect")
.attr("width", w)
.attr("height", h)
.attr('fill','none')
.attr('stroke','black')
.style("pointer-events", "all");
var vis = graph.append("svg:g")
.attr("class", "plotting-area");
function update() {
var nodes = d3.hierarchy(root)
const links = d3.tree()
.size([h, w])(nodes)
.links();
nodes = flatten(root)
force
.nodes(nodes)
.on("tick", tick);
force.force("link")
.links(links);
// Update the links…
linkLines = vis.selectAll("line.link")
.data(links, function (d) {
return d.target.index;
});
linkLines.enter().insert("svg:line", ".node")
.attr("class", "link")
.attr('stroke','black')
linkLines.exit().remove();
nodeCircles = vis.selectAll("circle.node")
.data(nodes, function (d) {
return d.id;
})
.attr("fill", color)
.attr('stroke','black')
nodeCircles.enter().append("svg:circle")
.attr("class", "node")
.attr("r", function (d) {
return d.children ? 4.5 : Math.sqrt(d.size) / 10;
})
.style("fill", color);
nodeCircles.exit().remove();
}
function color(d) {
return d.children ? "#c6dbef" : d.group;
}
function tick() {
linkLines.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;
});
nodeCircles.attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
});
}
function readfile(json) {
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h/2;
update();
};
function flatten(root) {
var nodes = [],
i = 0;
function recurse(node) {
if (node.children) node.size = node.children.reduce(function (p, v) {
return p + recurse(v);
}, 0);
if (!node.id) node.id = ++i;
nodes.push(node);
return node.size;
}
root.size = recurse(root);
return nodes;
}
var data = {
"dist": 0.00193506541936,
"name": "N3",
"children": [ {
"dist": 0.00488832259274,
"name": "N4",
"children": [ {
"dist": 0.00421186204991,
"name": "N5",
"children": [ {
"dist": 0.0163437491651,
"name": "N6",
"children": [{
"dist": 0.417946674158,
"group": "blue",
"name": "CHEMBL1644419",
"size": 2000.0000000000866
}, {
"dist": 0.00543077796308,
"name": "N8",
"children": [{
"dist": 0.0297671023157,
"name": "N9",
"children": [{
"dist": 0.359033872667,
"group": "red",
"name": "ASD03540222",
"size": 2000.0000000000866
}, {
"dist": 0.362485114675,
"group": "red",
"name": "ASD01150858",
"size": 2000.0000000000866
}]
},{
"dist": 0.0224504491652,
"name": "N12",
"children": [{
"dist": 0.00178517153851,
"name": "N13",
"children": [{
"dist": 0.364388220986,
"group": "blue",
"name": "CHEMBL197161",
"size": 2000.0000000000866
}, {
"dist": 0.0425243314393,
"name": "N15",
"children": [{
"dist": 0.336193167816,
"group": "blue",
"name": "CHEMBL1644268",
"size": 2000.0000000000866
}, {
"dist": 0.325677335782,
"group": "red",
"name": "CHEMBL593637",
"size": 2000.0000000000866
}]
}]
}, {
"dist": 0.0246952948163,
"name": "N18",
"children": [{
"dist": 0.358771918732,
"group": "blue",
"name": "CHEMBL569878",
"size": 2000.0000000000866
}, {
"dist": 0.36317930078,
"group": "blue",
"name": "CHEMBL434267",
"size": 2000.0000000000866
}]
}]
}]
}]
}, {
"dist": 0.00389420127272,
"name": "N21",
"children": [{
"dist": 0.010842478752,
"name": "N22",
"children": [{
"dist": 0.415105380276,
"group": "blue",
"name": "CHEMBL1275732",
"size": 2000.0000000000866
}, {
"dist": 0.0337121910412,
"name": "N24",
"children": [{
"dist": 0.0664262587727,
"name": "N25",
"children": [{
"dist": 0.315587114957,
"group": "red",
"name": "ASD00170175",
"size": 2000.0000000000866
}, {
"dist": 0.310918909139,
"group": "red",
"name": "CHEMBL292368",
"size": 2000.0000000000866
}]
}, {
"dist": 0.376684365543,
"group": "red",
"name": "ASD00170052",
"size": 2000.0000000000866
}]
}]
}, {
"dist": 0.00704072756384,
"name": "N29",
"children": [{
"dist": 0.0103358142929,
"name": "N30",
"children": [{
"dist": 0.412662682696,
"group": "blue",
"name": "CHEMBL14370",
"size": 2000.0000000000866
}, {
"dist": 0.427337317304,
"group": "blue",
"name": "CHEMBL94700",
"size": 2000.0000000000866
}]
}, {
"dist": 0.0142382687979,
"name": "N33",
"children": [{
"dist": 0.403816741996,
"group": "blue",
"name": "CHEMBL545557",
"size": 2000.0000000000866
}, {
"dist": 0.0752386947609,
"name": "N35",
"children": [{
"dist": 0.316746014667,
"group": "blue",
"name": "CHEMBL506342",
"size": 2000.0000000000866
}, {
"dist": 0.314832932701,
"group": "red",
"name": "CHEMBL1256402",
"size": 2000.0000000000866
}]
}]
}]
}]
} ]
}]
} ] };
readfile(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.7.0/d3.min.js"></script>
I am trying to make the same radius for all child nodes using d3.pack().
In my experience, the problem is that d3 pack generates the radius according to child nodes count or some other value.
Here is data structure sample.
{
"id": "CoC-home",
"name": "HOME",
"type": "circle",
"value": 1,
"children": [
{
"id": "CT-home-1",
"name": "Sales Systems, Digital & Omnichannel",
"type": "circle",
"value": 1,
"children": [
{
"id": "ET-1-home",
"name": "Omnichannel",
"type": "circle",
"value": 0.25,
"children": [
{
"id": "EG-1-home",
"name": "CRM",
"type": "circle",
"value": 1
},
{
"id": "EG-2-home",
"name": "New Business Models",
"type": "circle",
"value": 1
},
{
"id": "EG-3-home",
"name": "Business Development Stores",
"type": "circle",
"value": 1
},
{
"id": "EG-4-home",
"name": "Business Development Customer & Advisor Service",
"type": "circle",
"value": 1
}
]
},
{
"id": "ET-2-home",
"name": "Business Development Advisor Sales",
"type": "circle",
"value": 0.2,
"children": [
{
"id": "EG-5-home",
"name": "Business Development Advisor Sales Kobold",
"type": "circle",
"value": 1
},
{
"id": "EG-6-home",
"name": "Business Development Advisor Sales Temial",
"type": "circle",
"value": 1
},
{
"id": "EG-7-home",
"name": "Recruitment and Training & Learning",
"type": "circle",
"value": 1
},
{
"id": "EG-8-home",
"name": "Income System",
"type": "circle",
"value": 1
}
]
},
{
"id": "ET-3-home",
"name": "Advisor Solutions",
"type": "circle",
"value": 0.25,
"children": [
{
"id": "EG-9-home",
"name": "Commission Engine",
"type": "circle",
"value": 1
},
{
"id": "EG-10-home",
"name": "Advisor Lifecycle & LMS",
"type": "circle",
"value": 1
},
{
"id": "EG-11-home",
"name": "Solutions Small Countries",
"type": "circle",
"value": 1
}
]
},
{
"id": "ET-4-home",
"name": "Digital",
"type": "circle",
"value": 0.2,
"children": [
{
"id": "EG-12-home",
"name": "eBusiness",
"type": "circle",
"value": 1
},
{
"id": "EG-13-home",
"name": "Cleaning",
"type": "circle",
"value": 1
},
{
"id": "EG-14-home",
"name": "Cookidoo",
"type": "circle",
"value": 1
},
{
"id": "EG-15-home",
"name": "Digital Experience Design",
"type": "circle",
"value": 1
},
{
"id": "EG-16-home",
"name": "Community",
"type": "circle",
"value": 1
},
{
"id": "EG-17-home",
"name": "Big Data & Analytics",
"type": "circle",
"value": 1
},
{
"id": "EG-18-home",
"name": "Insights Engine",
"type": "circle",
"value": 1
},
{
"id": "EG-19-home",
"name": "Digital Marketing & Lead Management",
"type": "circle",
"value": 1
},
{
"id": "EG-20-home",
"name": "Recipe Business",
"type": "circle",
"value": 1
}
]
},
{
"id": "ET-5-home",
"name": "Program Management",
"type": "circle",
"value": 0.2,
"children": [
{
"id": "EG-22-home",
"name": "Program Mgmt. Customer Service",
"type": "circle",
"value": 1
},
{
"id": "EG-23-home",
"name": "TBD",
"type": "circle",
"value": 1
}
]
}
]
},
{
"id": "CT-home-2",
"name": "Marketing",
"type": "circle",
"value": 1,
"children": [
{
"id": "ET-6-home",
"name": "Brand Marketing",
"type": "circle",
"value": 0.25
},
{
"id": "ET-7-home",
"name": "Communication",
"type": "circle",
"value": 0.25
},
{
"id": "ET-8-home",
"name": "Market Research & Insights",
"type": "circle",
"value": 0.25
},
{
"id": "ET-9-home",
"name": "Operative Marketing Coordination",
"type": "circle",
"value": 0.25
}
]
},
{
"id": "CT-home-4",
"name": "Sales",
"type": "circle",
"value": 1,
"children": [
{
"id": "ET-15-home",
"name": "IDB",
"type": "circle",
"value": 1
},
{
"id": "ET-16-home",
"name": "KS/TM Country xxx",
"type": "circle",
"value": 1
},
{
"id": "ET-17-home",
"name": "KS/TM Country xxx",
"type": "circle",
"value": 1
}
]
},
{
"id": "CT-home-5",
"name": "Complementary Product Portfolio",
"type": "circle",
"value": 1
},
{
"id": "CT-home-6",
"name": "Recognition & Events",
"type": "circle",
"value": 1
},
{
"id": "CT-home-7",
"name": "Finance Markets & Business Partnering",
"type": "circle",
"value": 1
},
{
"id": "CT-home-8",
"name": "Operations",
"type": "circle",
"value": 1,
"children": [
{
"id": "ET-20-home",
"name": "Intl. Trade",
"type": "circle",
"value": 1
},
{
"id": "ET-21-home",
"name": "Master Data",
"type": "circle",
"value": 1
},
{
"id": "ET-21-home",
"name": "Purchasing",
"type": "circle",
"value": 1
},
{
"id": "ET-21-home",
"name": "Operational Logistics",
"type": "circle",
"value": 1
},
{
"id": "ET-21-home",
"name": "Performance Management",
"type": "circle",
"value": 1
},
{
"id": "ET-21-home",
"name": "Repair",
"type": "circle",
"value": 1
},
{
"id": "ET-21-home",
"name": "Planning",
"type": "circle",
"value": 1
},
{
"id": "ET-21-home",
"name": "Projects",
"type": "circle",
"value": 1
}
]
}
]
},
There are all 4 levels in the json data and each level node has the different count of children nodes.
If the number of child nodes for each level is same, then its radius is same.
Here's what I'm getting:
h
Here is my code snippet
nodeG.append('g')
.filter(d => d.type === 'circle' | d.type === 'inner-circle')
.each(function (d) {
drawHexagons(
d3.select(this),
d,
{
width: radiusAccessor(d) * 2,
height: radiusAccessor(d) * 2
}
)
})
const radiusAccessor = (d) => {
return d.id === 'exec' ?
50 :
d.type === 'circle' ?
110 :
d.type === 'inner-circle' ?
60 : 50;
}
function drawHexagons(nodeElement, data, options) {
const width = options.width
const height = options.height
const pack = data => d3.pack()
.size([width, height])
.padding(3)
(d3.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value))
const root = pack(data);
let focus = root;
const node = nodeElement.append("g")
.selectAll("circle")
.data(root.descendants().slice(1))
.join("circle")
.attr("id", d => d.data.id)
.attr("class", d => {
return d.data.groupId
})
.attr("fill", d => (d.data.groupId === "corporate_governance" | d.data.groupId === "communications") ? color(d.depth + 1) : color(d.depth))
.attr("transform", d => `translate(${(d.x - root.x)},${(d.y - root.y)})`)
.attr("r", d => {
return d.r;
})
.style("visibility", d => {
return d.depth > 0 ? "hidden" : "visible"
})
}
I just want to make all child node to take the same radius regardless of its child nodes number.
For clarity: I just want to take the same radius for each level nodes regardless each node has children or not. For example, all node's radius for level 1 is 50, radius for level 2 is 30 and etc.
Do you have any idea?
Thank for your reading.
I just want to take the same radius for each level nodes regardless each node has children or not. For example, all node's radius for level 1 is 50, radius for level 2 is 30 and etc
This cannot be achieved with circle packing. The goal of circle packing is to child circles into the smallest bounding circle. You could make all of one generation have the same size regardless of child count, but to specify each generation/level's won't work.
If parent A has one child of radius r and parent A's sibling, parent, B another has two children of radius r, then naturally, the smallest bounding circle for parent A will not be the same as parent B. For each additional child of fixed radius r, the minimum bounding circle's radius will increase.
Consequently, any circle packing algorithm won't work - you cannot optimally pack a different number of children with the same radius into larger parent circles sharing some larger radius: there will be wasted space in the parent with less children.
I'm not aware of any alternative algorithm to achieve this, using rectangles would likely be the easiest to implement a solution manually - as rectangles are easily stacked making overlap prevention easy. A circular solution without math could be a nested d3-force layout, but could be cumbersome from a code and overhead perspective.
Adopting this example, I get an axis that is incorrect. The axis shows labels for 50 000, 00 000, 50 000, etc, but my data is from 0 - 513836. This is what the axis looks like:
Here's my code and data:
data = [{
"value": "478176",
"date": "2020-06-28"
}, {
"value": "478278",
"date": "2020-06-29"
}, {
"value": "478559",
"date": "2020-06-30"
}, {
"value": "478559",
"date": "2020-07-01"
}, {
"value": "479175",
"date": "2020-07-02"
}, {
"value": "479175",
"date": "2020-07-03"
}, {
"value": "479175",
"date": "2020-07-04"
}, {
"value": "479379",
"date": "2020-07-05"
}, {
"value": "479633",
"date": "2020-07-06"
}, {
"value": "480010",
"date": "2020-07-07"
}, {
"value": "480531",
"date": "2020-07-08"
}, {
"value": "480794",
"date": "2020-07-09"
}, {
"value": "480794",
"date": "2020-07-10"
}, {
"value": "480794",
"date": "2020-07-11"
}, {
"value": "480995",
"date": "2020-07-12"
}, {
"value": "481161",
"date": "2020-07-13"
}, {
"value": "481161",
"date": "2020-07-14"
}, {
"value": "481934",
"date": "2020-07-15"
}, {
"value": "482218",
"date": "2020-07-16"
}, {
"value": "482218",
"date": "2020-07-17"
}, {
"value": "482218",
"date": "2020-07-18"
}, {
"value": "482851",
"date": "2020-07-19"
}, {
"value": "483331",
"date": "2020-07-20"
}, {
"value": "484302",
"date": "2020-07-21"
}, {
"value": "484569",
"date": "2020-07-22"
}, {
"value": "484734",
"date": "2020-07-23"
}, {
"value": "484734",
"date": "2020-07-24"
}, {
"value": "484734",
"date": "2020-07-25"
}, {
"value": "485123",
"date": "2020-07-26"
}, {
"value": "485597",
"date": "2020-07-27"
}, {
"value": "486763",
"date": "2020-07-28"
}, {
"value": "487392",
"date": "2020-07-29"
}, {
"value": "487392",
"date": "2020-07-30"
}, {
"value": "487392",
"date": "2020-07-31"
}, {
"value": "487392",
"date": "2020-08-01"
}, {
"value": "487392",
"date": "2020-08-02"
}, {
"value": "487909",
"date": "2020-08-03"
}, {
"value": "488496",
"date": "2020-08-04"
}, {
"value": "488496",
"date": "2020-08-05"
}, {
"value": "490538",
"date": "2020-08-06"
}, {
"value": "490538",
"date": "2020-08-07"
}, {
"value": "490538",
"date": "2020-08-08"
}, {
"value": "491490",
"date": "2020-08-09"
}, {
"value": "492511",
"date": "2020-08-10"
}, {
"value": "494185",
"date": "2020-08-11"
}, {
"value": "494730",
"date": "2020-08-12"
}, {
"value": "495052",
"date": "2020-08-14"
}, {
"value": "495052",
"date": "2020-08-15"
}, {
"value": "495610",
"date": "2020-08-16"
}, {
"value": "496394",
"date": "2020-08-17"
}, {
"value": "496394",
"date": "2020-08-18"
}, {
"value": "498006",
"date": "2020-08-19"
}, {
"value": "498631",
"date": "2020-08-20"
}, {
"value": "498631",
"date": "2020-08-21"
}, {
"value": "498631",
"date": "2020-08-22"
}, {
"value": "499186",
"date": "2020-08-23"
}, {
"value": "499561",
"date": "2020-08-24"
}, {
"value": "500491",
"date": "2020-08-25"
}, {
"value": "500491",
"date": "2020-08-26"
}, {
"value": "501295",
"date": "2020-08-27"
}, {
"value": "501295",
"date": "2020-08-28"
}, {
"value": "501295",
"date": "2020-08-29"
}, {
"value": "501748",
"date": "2020-08-30"
}, {
"value": "502284",
"date": "2020-08-31"
}, {
"value": "503353",
"date": "2020-09-01"
}, {
"value": "503719",
"date": "2020-09-02"
}, {
"value": "504046",
"date": "2020-09-03"
}, {
"value": "504046",
"date": "2020-09-04"
}, {
"value": "504046",
"date": "2020-09-05"
}, {
"value": "504204",
"date": "2020-09-06"
}, {
"value": "504697",
"date": "2020-09-07"
}, {
"value": "505659",
"date": "2020-09-08"
}, {
"value": "506346",
"date": "2020-09-12"
}, {
"value": "507109",
"date": "2020-09-14"
}, {
"value": "508650",
"date": "2020-09-16"
}, {
"value": "509708",
"date": "2020-09-17"
}, {
"value": "509708",
"date": "2020-09-18"
}, {
"value": "509708",
"date": "2020-09-19"
}, {
"value": "510154",
"date": "2020-09-20"
}, {
"value": "510226",
"date": "2020-09-21"
}, {
"value": "511136",
"date": "2020-09-22"
}, {
"value": "511252",
"date": "2020-09-23"
}, {
"value": "511253",
"date": "2020-09-24"
}, {
"value": "511253",
"date": "2020-09-25"
}, {
"value": "511253",
"date": "2020-09-26"
}, {
"value": "511363",
"date": "2020-09-27"
}, {
"value": "511467",
"date": "2020-09-28"
}, {
"value": "511467",
"date": "2020-09-29"
}, {
"value": "512016",
"date": "2020-09-30"
}, {
"value": "512016",
"date": "2020-10-01"
}, {
"value": "512016",
"date": "2020-10-02"
}, {
"value": "512016",
"date": "2020-10-03"
}, {
"value": "512610",
"date": "2020-10-04"
}, {
"value": "512889",
"date": "2020-10-05"
}, {
"value": "513552",
"date": "2020-10-06"
}, {
"value": "513836",
"date": "2020-10-07"
}];
parseDate = d3.timeParse("%Y-%m-%d");
line = d3.line()
.defined(d => !isNaN(d.value))
.x(d => x(parseDate(d.date)))
.y(d => y(parseInt(d.value)));
margin = ({
top: 20,
right: 30,
bottom: 30,
left: 40
});
height = 500;
width = 500;
x = d3.scaleUtc()
.domain(d3.extent(data, d => parseDate(d.date)))
.range([margin.left, width - margin.right]);
y = d3.scaleLinear()
.domain([0, d3.max(data, d => parseInt(d.value))]).nice()
.range([height - margin.bottom, margin.top]);
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0));
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y));
svg = d3.select("body").append("svg")
//.attr("viewBox", [0, 0, width, height]);
.attr("width", "100%")
.attr("height", "100%");
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
<script src="https://d3js.org/d3.v6.min.js"></script>
I am trying to understand how to create a multi ring pie chart using d3js . Here is what I have tried and got so far. I am pretty sure its not reading the data correctly here. Any example on how should I be creating such a chart. What I am trying to do is to get each ring based on each group like pop quarter and so on.
var dataset = [
{
"name": "Population Quater",
"code": "POP_QUATER",
"parent": "POP_BY_QUAT",
"children": [
{
"name": "POP_CYQ1",
"code": "POP_CYQ1",
"parent": "POP_QUATER",
"value": "6772",
"label": "CYQ1",
"children": []
},
{
"name": "POP_CYQ2",
"code": "POP_CYQ2",
"parent": "POP_QUATER",
"value": "6716",
"label": "CYQ2",
"children": []
},
{
"name": "POP_CYQ3",
"code": "POP_CYQ3",
"parent": "POP_QUATER",
"value": "6714",
"label": "CYQ3",
"children": []
},
{
"name": "POP_CYQ4",
"code": "POP_CYQ4",
"parent": "POP_QUATER",
"value": "6703",
"label": "CYQ4",
"children": []
},
{
"name": "POP_LYQ1",
"code": "POP_LYQ1",
"parent": "POP_QUATER",
"value": "6721",
"label": "LYQ1",
"children": []
},
{
"name": "POP_LYQ2",
"code": "POP_LYQ2",
"parent": "POP_QUATER",
"value": "6671",
"label": "LYQ2",
"children": []
},
{
"name": "POP_LYQ3",
"code": "POP_LYQ3",
"parent": "POP_QUATER",
"value": "6708",
"label": "LYQ3",
"children": []
},
{
"name": "POP_LYQ4",
"code": "POP_LYQ4",
"parent": "POP_QUATER",
"value": "6734",
"label": "LYQ4",
"children": []
}
]
},
{
"name": "Transient Pop",
"code": "TRANSIENT_POP",
"parent": "POP_BY_QUAT",
"label": "Transient Pop",
"children": [
{
"name": "TRANSIENT_LYQ1",
"code": "TRANSIENT_LYQ1",
"parent": "TRANSIENT_POP",
"value": "54",
"label": "LYQ1",
"children": []
},
{
"name": "TRANSIENT_LYQ2",
"code": "TRANSIENT_LYQ2",
"parent": "TRANSIENT_POP",
"value": "86",
"label": "LYQ2",
"children": []
},
{
"name": "TRANSIENT_LYQ3",
"code": "TRANSIENT_LYQ3",
"parent": "TRANSIENT_POP",
"value": "219",
"label": "LYQ3",
"children": []
},
{
"name": "TRANSIENT_LYQ4",
"code": "TRANSIENT_LYQ4",
"parent": "TRANSIENT_POP",
"value": "191",
"label": "LYQ4",
"children": []
},
{
"name": "TRANSIENT_CYQ1",
"code": "TRANSIENT_CYQ1",
"parent": "TRANSIENT_POP",
"value": "52",
"label": "CYQ1",
"children": []
},
{
"name": "TRANSIENT_CYQ2",
"code": "TRANSIENT_CYQ2",
"parent": "TRANSIENT_POP",
"value": "91",
"label": "CYQ2",
"children": []
},
{
"name": "TRANSIENT_CYQ3",
"code": "TRANSIENT_CYQ3",
"parent": "TRANSIENT_POP",
"value": "222",
"label": "CYQ3",
"children": []
},
{
"name": "TRANSIENT_CYQ4",
"code": "TRANSIENT_CYQ4",
"parent": "TRANSIENT_POP",
"value": "186",
"label": "CYQ4",
"children": []
}
]
},
{
"name": "Seasonal Pop",
"code": "SEASONAL_POP",
"parent": "POP_BY_QUAT",
"label": "Seasonal Pop",
"children": [
{
"name": "SEASONAL_LYQ1",
"code": "SEASONAL_LYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "LYQ1",
"children": []
},
{
"name": "SEASONAL_LYQ2",
"code": "SEASONAL_LYQ2",
"parent": "SEASONAL_POP",
"value": "24",
"label": "LYQ2",
"children": []
},
{
"name": "SEASONAL_LYQ3",
"code": "SEASONAL_LYQ3",
"parent": "SEASONAL_POP",
"value": "152",
"label": "LYQ3",
"children": []
},
{
"name": "SEASONAL_LYQ4",
"code": "SEASONAL_LYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "LYQ4",
"children": []
},
{
"name": "SEASONAL_CYQ1",
"code": "SEASONAL_CYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "CYQ1",
"children": []
},
{
"name": "SEASONAL_CYQ2",
"code": "SEASONAL_CYQ2",
"parent": "SEASONAL_POP",
"value": "22",
"label": "CYQ2",
"children": []
},
{
"name": "SEASONAL_CYQ3",
"code": "SEASONAL_CYQ3",
"parent": "SEASONAL_POP",
"value": "161",
"label": "CYQ3",
"children": []
},
{
"name": "SEASONAL_CYQ4",
"code": "SEASONAL_CYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "CYQ4",
"children": []
}
]
}
];
var width = 460,
height = 300,
cwidth = 25;
var color = d3.scale.category20();
var pie = d3.layout.pie()
.sort(null).value(function (d) {
return d.value;//since score is the parameter for the pie
});
var arc = d3.svg.arc();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var gs = svg.selectAll("g").data(d3.values(dataset)).enter().append("g");
var path = gs.selectAll("path")
.data(function(d) { return pie(d.value); })
.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", function(d, i, j) { return arc.innerRadius(10+cwidth*j).outerRadius(cwidth*(j+1))(d); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.13/d3.min.js"></script>
You are not binding the data array (dataset) to your groups. It should be:
var gs = svg.selectAll("g")
.data(dataset)
.enter()
.append("g");
And then, for the inner selections, use children:
var path = gs.selectAll("path")
.data(function(d) {
return pie(d.children);
})
//etc...
Here is your code with those changes:
var dataset = [{
"name": "Population Quater",
"code": "POP_QUATER",
"parent": "POP_BY_QUAT",
"children": [{
"name": "POP_CYQ1",
"code": "POP_CYQ1",
"parent": "POP_QUATER",
"value": "6772",
"label": "CYQ1",
"children": []
}, {
"name": "POP_CYQ2",
"code": "POP_CYQ2",
"parent": "POP_QUATER",
"value": "6716",
"label": "CYQ2",
"children": []
}, {
"name": "POP_CYQ3",
"code": "POP_CYQ3",
"parent": "POP_QUATER",
"value": "6714",
"label": "CYQ3",
"children": []
}, {
"name": "POP_CYQ4",
"code": "POP_CYQ4",
"parent": "POP_QUATER",
"value": "6703",
"label": "CYQ4",
"children": []
}, {
"name": "POP_LYQ1",
"code": "POP_LYQ1",
"parent": "POP_QUATER",
"value": "6721",
"label": "LYQ1",
"children": []
}, {
"name": "POP_LYQ2",
"code": "POP_LYQ2",
"parent": "POP_QUATER",
"value": "6671",
"label": "LYQ2",
"children": []
}, {
"name": "POP_LYQ3",
"code": "POP_LYQ3",
"parent": "POP_QUATER",
"value": "6708",
"label": "LYQ3",
"children": []
}, {
"name": "POP_LYQ4",
"code": "POP_LYQ4",
"parent": "POP_QUATER",
"value": "6734",
"label": "LYQ4",
"children": []
}]
}, {
"name": "Transient Pop",
"code": "TRANSIENT_POP",
"parent": "POP_BY_QUAT",
"label": "Transient Pop",
"children": [{
"name": "TRANSIENT_LYQ1",
"code": "TRANSIENT_LYQ1",
"parent": "TRANSIENT_POP",
"value": "54",
"label": "LYQ1",
"children": []
}, {
"name": "TRANSIENT_LYQ2",
"code": "TRANSIENT_LYQ2",
"parent": "TRANSIENT_POP",
"value": "86",
"label": "LYQ2",
"children": []
}, {
"name": "TRANSIENT_LYQ3",
"code": "TRANSIENT_LYQ3",
"parent": "TRANSIENT_POP",
"value": "219",
"label": "LYQ3",
"children": []
}, {
"name": "TRANSIENT_LYQ4",
"code": "TRANSIENT_LYQ4",
"parent": "TRANSIENT_POP",
"value": "191",
"label": "LYQ4",
"children": []
}, {
"name": "TRANSIENT_CYQ1",
"code": "TRANSIENT_CYQ1",
"parent": "TRANSIENT_POP",
"value": "52",
"label": "CYQ1",
"children": []
}, {
"name": "TRANSIENT_CYQ2",
"code": "TRANSIENT_CYQ2",
"parent": "TRANSIENT_POP",
"value": "91",
"label": "CYQ2",
"children": []
}, {
"name": "TRANSIENT_CYQ3",
"code": "TRANSIENT_CYQ3",
"parent": "TRANSIENT_POP",
"value": "222",
"label": "CYQ3",
"children": []
}, {
"name": "TRANSIENT_CYQ4",
"code": "TRANSIENT_CYQ4",
"parent": "TRANSIENT_POP",
"value": "186",
"label": "CYQ4",
"children": []
}]
}, {
"name": "Seasonal Pop",
"code": "SEASONAL_POP",
"parent": "POP_BY_QUAT",
"label": "Seasonal Pop",
"children": [{
"name": "SEASONAL_LYQ1",
"code": "SEASONAL_LYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "LYQ1",
"children": []
}, {
"name": "SEASONAL_LYQ2",
"code": "SEASONAL_LYQ2",
"parent": "SEASONAL_POP",
"value": "24",
"label": "LYQ2",
"children": []
}, {
"name": "SEASONAL_LYQ3",
"code": "SEASONAL_LYQ3",
"parent": "SEASONAL_POP",
"value": "152",
"label": "LYQ3",
"children": []
}, {
"name": "SEASONAL_LYQ4",
"code": "SEASONAL_LYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "LYQ4",
"children": []
}, {
"name": "SEASONAL_CYQ1",
"code": "SEASONAL_CYQ1",
"parent": "SEASONAL_POP",
"value": "2",
"label": "CYQ1",
"children": []
}, {
"name": "SEASONAL_CYQ2",
"code": "SEASONAL_CYQ2",
"parent": "SEASONAL_POP",
"value": "22",
"label": "CYQ2",
"children": []
}, {
"name": "SEASONAL_CYQ3",
"code": "SEASONAL_CYQ3",
"parent": "SEASONAL_POP",
"value": "161",
"label": "CYQ3",
"children": []
}, {
"name": "SEASONAL_CYQ4",
"code": "SEASONAL_CYQ4",
"parent": "SEASONAL_POP",
"value": "55",
"label": "CYQ4",
"children": []
}]
}];
var width = 460,
height = 300,
cwidth = 25;
var color = d3.scale.category20();
var pie = d3.layout.pie()
.sort(null).value(function(d) {
return d.value; //since score is the parameter for the pie
});
var arc = d3.svg.arc();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var gs = svg.selectAll("g")
.data(dataset)
.enter()
.append("g");
var path = gs.selectAll("path")
.data(function(d) {
return pie(d.children);
})
.enter().append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", function(d, i, j) {
return arc.innerRadius(10 + cwidth * j).outerRadius(cwidth * (j + 1))(d);
});
<script src="https://d3js.org/d3.v3.min.js"></script>
I have the following Force layout graph in my D3 code...
// Code goes here
var width = w = 300,
height = h = 300,
link,
node,
svg,
force,
drag,
currentSelected;
function tick() {
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("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
}
var graph = {
"nodes":
[
{
"code": "1130",
"name": "Interior",
"title": "Interior",
"id": 137,
"label": "ATAChild"
},
{
"code": "50",
"name": "Loose Equipment",
"title": "Loose Equipment",
"id": 146,
"label": "ATAChild"
},
{
"code": "2781 ",
"name": "LE FLAP POSITION IND SYSTEM",
"title": "LE FLAP POSITION IND SYSTEM",
"id": 218,
"label": "ATAChild"
},
{
"code": "2830 ",
"name": "FUEL DUMP",
"title": "FUEL DUMP",
"id": 227,
"label": "ATAChild"
},
{
"code": "1130",
"name": "Interior",
"title": "Interior",
"id": 138,
"label": "ATAChild"
},
{
"code": "50",
"name": "Loose Equipment",
"title": "Loose Equipment",
"id": 147,
"label": "ATAChild"
},
{
"code": "2781 ",
"name": "LE FLAP POSITION IND SYSTEM",
"title": "LE FLAP POSITION IND SYSTEM",
"id": 219,
"label": "ATAChild"
},
{
"code": "2830 ",
"name": "FUEL DUMP",
"title": "FUEL DUMP",
"id": 228,
"label": "ATAChild"
},
{
"code": "1130",
"name": "Interior",
"title": "Interior",
"id": 139,
"label": "ATAChild"
},
{
"code": "50",
"name": "Loose Equipment",
"title": "Loose Equipment",
"id": 148,
"label": "ATAChild"
},
{
"code": "2781 ",
"name": "LE FLAP POSITION IND SYSTEM",
"title": "LE FLAP POSITION IND SYSTEM",
"id": 220,
"label": "ATAChild"
},
{
"code": "2830 ",
"name": "FUEL DUMP",
"title": "FUEL DUMP",
"id": 229,
"label": "ATAChild"
}
],
"links":
[
]
}
function render() {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
link = link.data(graph.links)
.enter().append("line")
.attr("class", "link");
node = node.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 12)
.call(drag);
}
function init() {
force = d3.layout.force()
.size([width, height])
.charge(-400)
.linkDistance(40)
.on("tick", tick);
svg = d3.select("#graph").append("svg")
.attr("width", width)
.attr("height", height);
drag = force.drag();
link = svg.selectAll(".link");
node = svg.selectAll(".node");
}
window.onload = function(){
init();
render();
}
<div id="graph" class="full"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Now I am fairly new to D3 but as you can see my issue is that you cannot see all the nodes. I am not sure what is happening here but I would like to have it zoom out so that even if it is painfully small I can still see all nodes. I have seen some answers related to zooming but I have had no luck getting it to work with the force layout. Im you need to play around I have a plnkr as well.