score:3
Data will only be matched by index unless you provide a key function as the second argument to selection.data([data[, key]])
.
If a key function is not specified, then the first datum in data is assigned to the first selected element, the second datum to the second selected element, and so on. A key function may be specified to control which datum is assigned to which element, replacing the default join-by-index.
If you do provide a key function, however, it is worth noting, that this function will first be called for every element in the selection and will afterwards be evaluated for every new datum in your data array. Or, as the docs have it:
This key function is evaluated for each selected element, in order, being passed the current datum (d), the current index (i), and the current group (nodes), with this as the current DOM element (nodes[i]). The key function is then also evaluated for each new datum in data, being passed the current datum (d), the current index (i), and the group’s new data, with this as the group’s parent DOM element. The datum for a given key is assigned to the element with the matching key.
To match your new data to your existing texts by id you can make use of this by specifying a key function as follows:
d3.selectAll("text")
.data(data, function(d) { return this.id || d.id; })
This will evaluate to the text elements' ids in the first run, i.e. this.id
, and to the data ids in the second run, whereby matching elements to new data.
Have a look at the following snippet for a working demo:
var data = [{"id": "id1", "val":1}, {"id": "id2", "val":2}, {"id": "id3", "val":3}];
d3.selectAll("text")
.data(data, function(d) { return this.id || d.id; })
.text(d => d.val);
<script src="https://d3js.org/d3.v4.js"></script>
<svg>
<text id="id1" y="10">text1</text>
<text id="id2" y="25">text2</text>
<text id="id3" y="40">text3</text>
<text id="abc1" y="55">textABC1</text>
<text id="abc2" y="70">textABC2</text>
<text id="abc3" y="85">textABC3</text>
</svg>
score:-1
Try to make loop for data
...
var data = [{"id": "id1", "val":1}, {"id": "id2", "val":2},...];
$(data).each(function(i,d){
d3.select("#"+d.id).text(d.val);
});
and without jQuery
for(var i=0, ii=data.length; i<ii; i++){
d3.select("#"+data[i].id).text(data[i].val);
}
Source: stackoverflow.com
Related Query
- Match SVG text element id with data id using D3
- Using Angular ng-class with d3.js on svg element
- How to match svg element ids with json keys
- D3JS: help to create a SVG text element inside a circle using D3js
- How do I do a line break with SVG text element ?
- Select content of text element with tspan specifier in SVG with D3js
- Automatically place text from two data variables into an svg with d3
- D3 - Finding an SVG Element with the same data
- Text not wrapping in d3 SVG element even when using Bostock's wrap function
- Create a multiple line chart with Text data in x-axis using d3.js
- using array like object (Node List) with in SVG element - d3 Java Script
- d3 SVG text element background-color with getBBox() wrong order
- Draw text on svg path element with d3.js
- populating existing SVG elements with JSON data using D3
- Angular + d3.js: Data binding with SVG text attr?
- How can I toggle the value of an svg text element with D3.js when user clicks the text?
- Limiting Width on a Text element in SVG using D3.js
- how to create circles with text over svg file using d3 js?
- Updating SVG Element Z-Index With D3
- How do I get the width of an svg element using d3js?
- Append SVG canvas to element other than body using D3
- Changing number displayed as svg text gradually, with D3 transition
- Get width of d3.js SVG text element after it's created
- Appending multiple svg text with d3
- StopPropagation() with SVG element and G
- how to assign unique id to SVG text element in d3 javascript
- Fit Text into SVG Element (Using D3/JS)
- Fill SVG element with a repeating linear gradient color
- nvd3.js : unable to bind onClick event with the data points in the svg
- Using D3 transition method with data for scatter plot
More Query from same tag
- How to flatten d3.nest using javascript map() method?
- Magnifier in d3
- Heatmap does not show the first rect
- DC.js X-axis hour labels appear as thousandths
- D3 arc - not visible
- d3Js - how to arrange my values as around the 360d and automate data in a circle?
- Firefox event.clientX not functioning
- dc charts change legend order
- Circle clip and projection with D3 orthographic
- Json file for Canada's province
- Why doesn't d3.json send cookies with the request?
- Enter/update/exit on two-levels, without nesting
- Loop only through nested elements of the array item from D3 selectAll creation from data()
- Creating dictionary with map & filter?
- Mouseover in a scatterplot of a second svg in D3
- Donut chart: how to flip upside down numbers?
- rotate and center globe on click
- Slice and bounce effect not working properly in pie chart
- D3 map - filter on specific properties
- Retrieve value from password field
- D3 force layout - Links exceed the boundaries of the container, while dragging the nodes
- How to change sizing of D3 axis?
- Creating a real time changing rectangle in d3.js with a bitcoin WebSocket
- d3.js month.range interval issue
- Displaying D3.js Chart with Django Backend
- Align text element to middle in treemap
- d3.js v7 - scaleOrdinal - Color child nodes same as parent
- Load SVG shapes over multiple lines in D3
- cant change the opacity of a specific path in D3js sunburst chart
- Multiple paths with d3.js and google maps API