score:1
You shouldn't feel dumb — I think everyone who uses d3 does this at some point (me repeatedly).
What's happening is that enter()
is returning the selection made when you add data. Everything you chain after enter()
will be called on the appended circle created with the enter()
selection. That's why your first example works.
When you break chain however, you are now calling attire()
on the original selection, which won't work. It's equivalent to doing this:
const circles = svg.selectAll('circle').data(data)
.attr("cx", function (d) { return ( xScale(d.x) )})
.attr("cy", function (d) { return d.y});
To separate them you need to make a new selection:
d3.selectAll('circle')
.attr("cx", function (d) { return ( xScale(d.x) )})
.attr("cy", function (d) { return d.y});
This is a nice way to separate things that will need to be updated.
p.s. I think the reason the youtube video works is because he's using the old version of D3. In the new version, selections are immutable. See the first section here: https://github.com/d3/d3-selection/releases/tag/v1.0.0
Source: stackoverflow.com
Related Query
- Method chaining seems to break this d3 code. Why?
- Why does this D3 code add the <p> element outside the body, instead of inside it?
- Why does this code not reset d3 zoom state?
- Why does this code work for counting one item in a list but not the others?
- Why does this D3 code not produce any output in Jupyter Notebook?
- Why is there a multi-second delay in this d3 code running?
- Why is this D3 exit() code not working as expected?
- Why does my D3 code break when I set the script type to module?
- D3/SVG: Why is this code slower when selection is changed?
- I'm working on d3.js. Why this code doesn't work correctly?
- Why does d3.js v3 break my force graph when implementing zooming when v2 doesn't?
- Why can't I get the Bounding Box of this d3.js text?
- Why is this animation so slow in FireFox?
- Why doesn't this simple attrTween example work?
- D3: Is there a name for this type of chart and code example?
- Why does this SVG graphic bog down Webkit only when it's large?
- Why does this syntax work over the other one with d3.json requests?
- Why are my images not showing up in the nodes of this D3 chart?
- Why am I getting different results with same code in D3js?
- This Search Collapsible Tree code doesn't work when I run it on my PC?
- Why is my method not recognizing my object member?
- Why does this title graphic not update with World
- Why can't I draw a line on this D3.js using angular code?
- Should this code override the default d3 version in use by Juypyter notebooks?
- Why does this d3 chart render with crosses, then get rid of them upon moving a node?
- Why does this vanilla js function return different results in d3v3 and d3v4
- Why the line in this D3 chart isn't properly displayed when updated?
- Looking for a way to refactor D3.js-style method chaining pattern
- How do I re-write this Javascript code to avoid JSHint warning?
- Why does separating a d3 method chain on enter alter outcome?
More Query from same tag
- How to filter an array of objects by property?
- xAxis ordinal() does not start in left corner
- D3 filling dataset from csv failing
- How to pass a value to line generation function in D3
- d3.js - What is the purpose of selectAll that returns non-existing element
- Interactive Legend onclick or mouseover - D3js
- d3 Stacked Bar chart - assign olors
- Panning/Zooming of Newly added points to d3.js Chart
- what's the absolute shortest d3 area example?
- Reading from Json file to create graph javascript
- Axes not showing in D3 diagram
- Increasing d3 SVG container size
- Django JsonResponse with safe=False to D3
- Zooming in a d3js force simulation on a canvas
- d3 (multi-series) line graph being filled in
- Change diagram to bottom up instead of left to right
- How to make a scatter plot from a CSV file using d3.js?
- D3.zoom jumps when using mouse wheel after programmatically zoom
- d3 sankey svg text tspan - on click not working
- d3 draw once on mouseover
- d3.js get JSON from url
- d3.js Force Directed Graph - Using Images instead of Circles for the Nodes
- Is it possible to use non-mouse, non-touch events to interact with a D3.js graph? If so, what is the most efficient way to go about it?
- How to specify timeout in d3 xhr send request
- d3 javascript naming or referring to groups
- d3.js Tree Layout - Cross Links
- Plotting a line using d3 and an JSON data object passed from rails
- D3.js Plotting Multiple Data Sets from Separate Files
- d3js onClick function can't access variable from superordinate function
- How to get data from d3js polyline?