I've seen this problem several times across multiple SO posts, while it feels wrong to post something resembling a duplicate, this error seems to present itself for lots of different reasons and as such every question is slightly different from the last. So here we go with my problem:
Here is my code;
window.onload = function () {
var width = 750,
height = 750,
counter = 0;
var force = d3.layout.force()
.size([width, height]);
d3.csv("Data/BubbleData.csv", function (error, links) {
var X = d3.scale.linear()
.domain([0, width])
.range([0, width]);
var Y = d3.scale.linear()
.domain([0, height])
.range([height, 0]);
var zoom = d3.behavior.zoom()
.x(X)
.y(Y)
.scaleExtent([1, 10])
.on("zoom", zoomed);
//handle zooming
function zoomed() {
circles.attr("transform", transform);
}
function transform(d) {
return "translate(" + X(d[0]) + "," + Y(d[1]) + ")";
};
var svg = d3.select("#GraphDiv")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.call(d3.behavior.zoom().x(X).y(Y).scaleExtent([1, 10]).on("zoom", zoom))
.on("dblclick.zoom", null);
var rect = svg.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "none")
.style("pointer-events", "all");
var color = d3.scale.category10();
var edges = [],
nodesByNames = {};
//create the nodes
links.forEach(function (link) {
link.App_No = nodeByName(link.App_No);
link.Server_No = nodeByName(link.Server_No);
edges.push({ source: link.App_No, target: link.Server_No })
});
var nodes = d3.values(nodesByNames);
//create container for all to go in
var container = svg.append("g")
.attr("class","container");
//create the links
var link = container.append("g")
.attr("class","LineGroup")
.selectAll(".link")
.data(edges)
.enter().append("line")
.attr("class", "link");
//define mouse behaviour for nodes
var drag = force.drag()
.on("dragstart", function (d) { d3.select(this).select("circle").classed("fixed", d.fixed = true); });
//create the nodes circles
var node = container.append("g")
.attr("class","circleGroup")
.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.call(drag);
var circles = node.append("circle")
.attr("r", 10)
.attr("class", "circle")
.attr("fill", function (d) { return color(d.group); })
.on("dblclick", function (d) { d3.select(this).classed("fixed", d.fixed = false); });
var labels = node.append("text")
.attr("x", 12)
.attr("y", ".35em")
.text(function (d) { return d.Name; });
force
.nodes(nodes)
.links(edges)
.linkDistance(50)
.charge(-100)
.size([width, height])
.on("tick", tick)
.start();
//draws the lines
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("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; });
}
function nodeByName(name) {
var groupNo;
switch (name.substring(0, 1)) {
case "A":
groupNo = 1;
break;
case "S":
groupNo = 2;
break;
default:
groupNo = 0;
}
return nodesByNames[name] || (nodesByNames[name] = { Name: name, group: groupNo });
}
});
};
And my data:
App_Name,App_No,Server_Name,Server_No
App_01,A1,Server_01,S1
App_01,A1,Server_02,S2
App_01,A1,Server_03,S3
App_01,A1,Server_04,S4
App_02,A2,Server_03,S3
App_02,A2,Server_04,S4
App_02,A2,Server_05,S5
App_03,A3,Server_05,S5
App_03,A3,Server_06,S6
App_03,A3,Server_07,S7
I am attempting to combine these two examples (here and here) in order to have a drag behaviour on the nodes while being able to semantically zoom. I've had to disable the zoom on dblclick behaviour(see code), but otherwise the click
, drag
, fix
and un-fix
behaviour all works perfectly, however I am getting a "cannot read property "on" of undefined"
error that occurs inside the d3
library itself (debugger in chrome shows line number it breaks on (line 1321)).
Is my code correct? Am I applying the zoom behaviour in the right place (on the svg var
)? and am I applying the transformation to the right group(in this case the circles group?)
EDIT: The error happens on line 1321 of the d3.js library. it says that "g" is null. could I just have a wrong d3 library for some reason?