score:2
Accepted answer
You can use the d.values because its part of the binded data
with your example:
.append("text")
.data(data)
.text(function(d) { return d.key; }) // so far so good
.attr("x", function(d) { return d.values[d.values.length - 1].x; })
.attr("y", function(d) { return yScale(d.values[d.values.length - 1].y); })
.attr("dy", ".35em");
const svg = d3.select('svg');
const data = [
{"key":"comp1","values":[
{"id": 0,"x":10,"y": 20},
{"id": 1,"x":20,"y": 10},
{"id": 2,"x":30,"y": 70},
{"id": 3,"x":40,"y": 80}
]},
{"key":"comp2","values":[
{"id": 0,"x":10,"y": 80},
{"id": 1,"x":20,"y": 60},
{"id": 2,"x":30,"y": 10},
{"id": 3,"x":40,"y": 40}
]}
];
const yScale = d3.scaleLinear().domain([0, 100]).range([20, 300]);
svg.selectAll('text')
.data(data)
.enter()
.append("text")
.text(function(d) { return d.key; }) // so far so good
.attr("x", function(d) { return d.values[d.values.length - 1].x; })
.attr("y", function(d) { return yScale(d.values[d.values.length - 1].y); })
.attr("dy", ".35em");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg style="background-color: #eee; width: 100%; height: 300px;"></svg>
Source: stackoverflow.com
Related Query
- Accessing values from different levels of a nested object within the same append instance using D3.js
- How do I assign ticks to be equal to a different key value from the same object from the one on which the axis is based on in D3.js?
- How to append javascript file to the body from javascript file which is in the same directory. Java server faces 2 JSF2
- d3.js - multiple charts on same page from different parts of same JSON object
- d3js: Calling different data from the same CSV - transitions
- How to access object properties in a JSON file using literals obtained from the same file
- Default values in json object based on contents of other objects in the same array
- D3 - How can I show/hide a text element when hovering a circle element created from different attributes of the same data entry?
- Two different users accessing same Wordpress page but have different views of a d3 object in that page
- Accessing d3.js element attributes from the datum?
- D3js how to append 2 children at the same level in .enter context
- Can d3.js draw two scatterplots on the same graph using data from the same source?
- Creating nested Json structure with multiple key values in Python from Json
- Accessing D3 SVG object within on click event
- Display only values from the data set into X axis ticks
- d3 setting multiple attributes from the same function?
- Return value based on max/min of another value in the same object
- How do you retrieve the key values of a nested data set in D3
- d3js pie graph from jquery ajax - correlating json keys and values on the chart
- D3 - accessing nested data for the purpose of creating a navigation tree
- d3.js create stacked bar chart from values in object
- Two bar charts from same data object using D3.js
- How can I color one Y-axis line different from the other grid-lines?
- Get X and Y axes values from the mouse position in d3.js
- Getting the max values from JSON in D3
- Rename variables at different levels of nested JSON dataset
- Display different values on axis than those found by the scale function in D3
- D3 line chart same tick on hover gives different values
- Append two elements in svg at the same level
- How to destroy multiple C3 graphs that were built from the same original function
More Query from same tag
- D3.js svg does not draw Map
- d3 realtime graph appears only after a delay
- Displaying some text on States on Map using D3.js and GeoJson
- D3js: two .append methods on the same selection can't be in the same line
- d3.js bar chart with pos & neg bars (win/loss) for each record
- D3.nest() isn't giving me the data structure I need
- Title does not appear on my D3js radar chart
- $watchCollection parameters required returns error, when passed as an array
- Nested (hierarchical) data with d3
- redrawing polygon after adding points on edges and dragging those points draws weird lines
- Holding Shift prevents mouse wheel zoom in firefox
- Pre-built directives for D3 in AngularJS
- D3 Pack text weird behavior
- Real-time D3 timeseries using Backbone -- graph moves slower than axis
- d3.zoom unexpected behavior: what am I missing?
- style text based on the context of the text in d3
- nested circles in d3
- How do I find the name of the asset to add to Bowerfile
- 'Point-along-path' d3 visualization performance issue
- Debugging Incorrectly Displaying Bars
- d3js tree.nodes() is not a function
- D3: scale data on the x-axis every 7 years
- d3.js - path drawing throws error
- Change the link color of ngx--graph
- Customize SVG text in D3 Radial Tree
- d3 synchronizing 2 separate zoom behaviors
- D3 Labels in pie chart being cut off
- D3 change the charge of a single node
- Is it possible to use a data.enter() method within a data.enter() method?
- d3.js mouseover overlapping circles