score:2
You're probably better off passing in all the labels at once -- this is also more in line with the general d3 idea. You could then have code something like this:
svg.selectAll("text.label").data(data)
.enter()
.append("text")
.attr("transform", function(d, i) {
var currenty = y(d.value.temperature);
if(i > 0) {
var previousy = y(data[i-1].value.temperature),
if(currenty - previousy < 12) { currenty = previousy + 12; }
}
return "translate(" + x(d.value.date) + "," + currenty + ")";
})
.attr("x", 3)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
This does not account for the fact that the previous label may have been moved. You could get the position of the previous label explicitly and move the current one depending on that. The code would be almost the same except that you would need to save a reference to the current element (this
) such that it can be accessed later.
All of this will not prevent the labels from being potentially quite far apart from the lines they are labelling in the end. If you need to move every label, the last one will be pretty far away. A better course of action may be to create a legend separately where you can space labels and lines as necessary.
score:2
Consider using a D3 force layout to place the labels. See an example here: https://bl.ocks.org/wdickerson/bd654e61f536dcef3736f41e0ad87786
Assuming you have a data
array containing objects with a value
property, and a scale y
:
// Create some nodes
const labels = data.map(d => {
return {
fx: 0,
targetY: y(d.value)
};
});
// Set up the force simulation
const force = d3.forceSimulation()
.nodes(labels)
.force('collide', d3.forceCollide(10))
.force('y', d3.forceY(d => d.targetY).strength(1))
.stop();
// Execute thte simulation
for (let i = 0; i < 300; i++) force.tick();
// Assign values to the appropriate marker
labels.sort((a, b) => a.y - b.y);
data.sort((a, b) => b.value - a.value);
data.forEach((d, i) => d.y = labels[i].y);
Now your data
array will have a y
property representing its optimal position.
Example uses D3 4.0, read more here: https://github.com/d3/d3-force
Source: stackoverflow.com
Related Query
- d3 line chart labels overlap
- D3 line chart axis text labels in multi line
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- C3js - combination chart with data labels only for line
- nvd3 - force all xaxis labels to show on line chart
- Labels on a D3 line chart not working
- How to add labels for regions in c3.js line chart or detail chart?
- d3.js How to draw line chart with vertical x axis labels
- D3 Line Chart not having proper X & Y axis plotting with Labels
- How to create a interactive d3 line chart to show data plots / labels on hover
- Labels on a d3 line chart
- How to rotate the text labels for the x Axis of a nvd3.js line chart
- d3.js: The xaxis labels are showing bold for a line chart
- Place image on Y-axis labels in Line chart
- Rotate Labels nvd3 line chart
- d3 real time line chart with labels
- Can't get d3 donut chart labels to line up correctly
- Show labels conditionally on multi series line chart
- D3 exit mutli-series line chart labels on transition
- How can I position rotated x-axis labels on column chart using nvd3?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- Long tick labels getting cut off in plotly.js chart
- Hide Tick Labels in D3 on Line Graph
- nvd3 line chart with string values on x-axis
- Draggable line chart in R/Shiny
- D3.JS time-series line chart with real-time data, panning and zooming
- d3 Line Chart Filling at Arbitrary Line
- Multiseries line chart with mouseover tooltip
- How to do wordwrap for chart labels using d3.js
- D3 - Pie Chart & Force Directed Labels
More Query from same tag
- How to use D3 to append several html line of codes
- Large Force Directed Graph Dataset in D3/Angular
- How to fix the " 'd3' was used before it was defined" error
- Form validation not working when appended by D3
- React - Warning: React.createElement: type should not be null, undefined, boolean, or number
- Filter list instead of select dropdown in D3
- USA Heat Map with D3 doesn't size properly in IE
- Appended text not appearing when using D3
- Panning and zooming to an SVG element
- D3 update pattern drawing the same element twice
- d3v4 why are my line elements not appearing?
- Text Transition on Click D3.js
- Arrows are not touching to nodes in d3.js in tree layout
- Why do nodes in force layout jump from origin on update
- D3.js: Is there a way to cap a "displayed" value at an axis max/min while retaining the "actual" value for a tooltip display?
- trigger click when I click on a circle
- d3js negative and positive barplot looses its alignment when more bars added
- NVD3 Line Plus Bar Chart timeline
- How do you clear an SVG in d3.js in javascript
- Compose two rotations in D3 geo projection?
- Visualizing the pass traffic data with Chord Diagram
- JSON parsing in d3 based on date
- arrow d3 graph svg not rendering
- Formatting bar graph for integers developed using d3.js
- The axis label at x=0 does not show up
- Create circle on svg map
- How to remove existing nodes in d3.js
- How to convert from v4 to v3 in d3
- D3 bisector function not working properly
- Add external data in D3 Pie by dbuezas’s