score:1
Accepted answer
It seems to me that this is an XY problem: you probably don't need any of that cumbersome each()
code, you can do what you want using a key function in your data
method.
However, since you didn't post your data or a (minimal) running version of your code, I'll address your specific question regarding the each()
method.
My suggestion is first getting the id
of this
element...
var id = d3.select(this).attr("id");
... and filtering the data
argument accordingly:
data.filter(function(d) {
return d.id === id;
})[0]
Here is a very basic demo, where the size of the circles are changed according to their IDs:
var data = [{
id: "foo",
size: 20
}, {
id: "bar",
size: 40
}, {
id: "baz",
size: 10
}];
var svg = d3.select("svg");
updateChart(data);
function updateChart(data) {
var sel = svg.selectAll(".agent")
.each(function() {
var id = d3.select(this).attr("id");
d3.select(this).transition()
.duration(1000)
.attr("r", function() {
return data.filter(function(d) {
return d.id === id;
})[0].size;
})
})
};
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg>
<circle class="agent" id="foo" cx="50" cy="50" r="5" fill="teal"></circle>
<circle class="agent" id="bar" cx="150" cy="50" r="5" fill="teal"></circle>
<circle class="agent" id="baz" cx="250" cy="50" r="5" fill="teal"></circle>
</svg>
Source: stackoverflow.com
Related Query
- d3 match element id to update existing item
- How do you update an existing svg element with the d3v5 join syntax
- D3.js Can't update existing element in group
- d3.js - only add circle element not update existing
- Update existing nodes in a d3 Force Layout
- How to update the fill color on existing svg elements with d3.js?
- Drawing an HTML table via D3 doesn't update existing data
- Appending an element to an existing element in d3.js
- D3 update circle-pack data new nodes overlap existing nodes
- d3 update selections: How does d3 decide which existing html elements to capture for its own use?
- Modify Existing d3 code - dynamic rectangle height to match text
- d3.js - update a GeoJSON element
- How to correctly update the text value of an input element in a d3.js transition
- d3 how to insert new SVG element before existing other element?
- How to add an list item to an existing list of items in D3?
- Match SVG text element id with data id using D3
- insert svg string element into an existing svg tag
- Is it possible to insert an SVG image into an existing SVG element already rendered on the page?
- How to match svg element ids with json keys
- D3 node update - how to select a specific text element among multiple ones?
- D3 General Update Pattern transitions of circles/text inside of g element
- How to manipulate the layer of a element in an existing SVG?
- Update one Wicket Panel from clicking an item in another Wicket Panel
- How should I use an existing svg element in D3?
- D3 update x coordinate element
- How to insert text sibling elements for each existing circle element of svg group?
- How to select existing chart and update it in NVD3.js
- D3 update pattern drawing the same element twice
- D3.js update dom element text on click
- d3.js v4 update element of a reusable chart
More Query from same tag
- Change bar chart to line chart in D3
- How to set unequal intervals on Y axis in D3.js?
- Getting height attributes of an element for D3 visualisation in angular 2
- How to update d3.js bar chart with new data
- D3 Object Constancy not working with key function. Enter and Update contain all elements every time
- dc.js - How do I add Totals to a dataTable?
- d3 world map with country click and zoom almost working not quite
- Adding a line on an svg with image in background in d3
- rCharts formatting axis labels: outputFormat
- Display D3 Tree Layout in Linear Format
- Chrome not running D3.js
- D3 : Loading multiple CSV files, and visualizing them using parallel coordinates
- mpld3 using d3v3 and d3v4 on the same html page
- Toggle between selected radio button using D3
- HTML button's "onclick" attribute calls function before being clicked on
- Exiting unused nodes in d3 force directed graph
- Export or Download d3.js sunburst / collapsible tree charts as pdf file
- Convert d3 map to high resolution image
- Update second chart on mouseover
- Drawing directed edges with arrow in D3
- Why copy D3 source into an Angular Service?
- D3.js v4 force layout with link forces causes strange movement when using drag behavior
- Updating word cloud with D3.js
- Count the number of rows of a CSV file, with d3.js
- How to get a data from an analysis in R in Json file format to get D3 visualisations?
- D3JS .call() Exception
- Parallel coordinates multidimensional data not visualised in D3
- Not able to create scatter and line chart together in c3.js
- live streaming plot that accumulates
- How to wire path/chart redraw after y domain changes