I'm using almost same code which has been used in http://techanjs.org/. I was trying to add zooming feature in the chart as the developer has rendered the chart in here. But somehow I'm not able to zoom it. I'm getting error selection is not defined in console. I'm using following code to render the chart. https://paste.fedoraproject.org/paste/7gJq9wKL4h4s862EDuxnQQ. I'm sure I'm doing something wrong in here, I would really appreciate if I get any assistance regarding it as I'm still learning D3.
Asked
Active
Viewed 305 times
3
-
1Really hard to say without a Minimal, Complete, and Verifiable example - https://stackoverflow.com/help/mcve - for example what is throwing the error, which call? Without more information it is pretty hard to help. – Fraser Dec 12 '17 at 20:35
-
It's really difficult to minimize it because I really want the other features along with the zoom and not sure if I try to minify it will produce the same error or not. – DAKSH Dec 13 '17 at 11:02
-
Then it is a vote to close once the bounty clears - Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. The simple error is because you are trying to use "selection" in functions where it isn't defined. – Fraser Dec 13 '17 at 11:49
1 Answers
2
I'd guess the problem is that you are referencing the variable selection
but it isn't defined.
Hence -
selection is not defined
selection
is the parameter passed into your bigchart
function.
But you then use it in your reset
, zoomed
functions where it is not defined.
//Zoom Begins
function reset() {
zoom.scale(1);
zoom.translate([0,0]);
selection.call(draw); // this will throw an error
}
function zoomed() {
x.zoomable().domain(d3.event.transform.rescaleX(zoomableInit).domain());
y.domain(d3.event.transform.rescaleY(yInit).domain());
yPercent.domain(d3.event.transform.rescaleY(yPercentInit).domain());
selection.call(draw); // this will throw an error
}
//Zoom Ends
To fix, either pass the selection to the functions as a parameter (like the other functions) - or else make the selection
a property of BigChart
so that it can be referenced by the reset
and zoomed
functions.
i.e.
// pass selection to the functions as a parameter
function reset(selection) {
zoom.scale(1);
zoom.translate([0,0]);
selection.call(draw);
}
function zoomed(selection) {
x.zoomable().domain(d3.event.transform.rescaleX(zoomableInit).domain());
y.domain(d3.event.transform.rescaleY(yInit).domain());
yPercent.domain(d3.event.transform.rescaleY(yPercentInit).domain());
selection.call(draw);
}
Then when you call the functions obviously you pass in the correct object...e.g.
zoom = selection.call(d3.zoom().on("zoom", zoomed));
rather than
zoom = d3.zoom().on("zoom", zoomed),

Fraser
- 15,275
- 8
- 53
- 104
-
I just tried the code, if I use zoom = selection.call(d3.zoom().on("zoom", zoomed)); I'm getting selection is not defined on that very line and the chart is not rendering. However, if I use zoom = d3.zoom().on("zoom", zoomed), chart is rendering fine but I'm getting the error while trying to zoom. Any suggestion? – DAKSH Dec 13 '17 at 10:59
-
Yes, post a Minimal, Complete, and Verifiable example - stackoverflow.com/help/mcve - the example zoom = selection.call(d3.zoom().on("zoom", zoomed)); was to show how you might pass the selection - it wasn't for you to copy and paste. Obviously "selection" is undefined before you use it - that is the root of your problem! – Fraser Dec 13 '17 at 11:46
-
I found another set of codes in http://bl.ocks.org/andredumas/edf630690c10b89be390 is it possible to resize it as per browser width? Because in here the zoom works perfectly fine. – DAKSH Dec 13 '17 at 12:09