score:2
Accepted answer
Rewrite your code this way (pay attention on the comments):
layout.on("tick", function() {
tooltips // here we set new position for tooltips on every tick
.attr("x", (d, i) => { return d.x; })
.attr("y", (d, i) => { return d.y; });
d3.selectAll(".node")
.attr("cx", d => { return d.x; })
.attr("cy", d => { return d.y; });
});
...
function drawNodes(nodes) {
tooltips = d3.select("#plot").selectAll(".node")
.data(nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("id", (d, i) => { return d.name; })
.attr("cx", (d, i) => { return d.x; })
.attr("cy", (d, i) => { return d.y; })
.attr("r", 4)
.style("fill", "#EE77b4")
.select(function() { return this.parentNode }) // returns to parent node
.append('text') // append svg-text elements for tooltip
.data(nodes)
.text(function(d) { return d.name; }) // set text
.attr("x", (d, i) => { return d.x; }) // set initial x position
.attr("y", (d, i) => { return d.y; }) // set initial y position
.attr("id", function(d,i) { return "tooltip-" + i; }) // set unique id
.attr("class", "d3-tooltip");
}
Working demo:
var width = 400;
var height = 125;
var margin = 20;
var pad = margin / 2;
var tooltips = null;
var graph = { "nodes":[ { "name": "A"}, { "name": "B"}] };
drawGraph(graph);
function drawGraph(graph) {
var svg = d3.select("#force").append("svg")
.attr("width", width)
.attr("height", height);
// create an area within svg for plotting graph
var plot = svg.append("g")
.attr("id", "plot")
.attr("transform", "translate(" + pad + ", " + pad + ")");
var layout = d3.layout.force()
.size([width - margin, height - margin])
.charge(-120)
.nodes(graph.nodes)
.start();
drawNodes(graph.nodes);
// add ability to drag and update layout
d3.selectAll(".node").call(layout.drag);
layout.on("tick", function() {
tooltips
.attr("x", (d, i) => { return d.x; })
.attr("y", (d, i) => { return d.y; });
d3.selectAll(".node")
.attr("cx", d => { return d.x; })
.attr("cy", d => { return d.y; });
});
}
// Draws nodes on plot
function drawNodes(nodes) {
tooltips = d3.select("#plot").selectAll(".node")
.data(nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("id", (d, i) => { return d.name; })
.attr("cx", (d, i) => { return d.x; })
.attr("cy", (d, i) => { return d.y; })
.attr("r", 4)
.style("fill", "#EE77b4")
.select(function() { return this.parentNode })
.append('text')
.data(nodes)
.text(function(d) { return d.name; })
.attr("x", (d, i) => { return d.x; })
.attr("y", (d, i) => { return d.y; })
.attr("class", "d3-tooltip")
.attr("id", function(d,i) { return "tooltip-" + i; });
}
body {
font-family: 'Source Sans Pro', sans-serif;
font-weight: 300;
}
b {
font-weight: 900;
}
.outline {
fill: none;
stroke: #888888;
stroke-width: 1px;
}
.d3-tooltip {
font-size: 20pt;
font-family: 'Comic Sans MS';
font-weight: 900;
fill: #000000;
stroke: #ffffff;
stroke-width: 0.25px;
}
.node {
stroke: #ffffff;
stroke-weight: 1px;
}
.highlight {
stroke: red;
stroke-weight: 4px;
stroke-opacity: 1.0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div align="center" id="force"></div>
Source: stackoverflow.com
Related Query
- How to make mouseover functionality permanent
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
- How to make a bar bigger on mouseover in D3?
- How to make force layout graph in D3.js responsive to screen/browser size
- How can I make double click event on node in d3.js?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- How to make localization on months / days for D3js?
- How to make the dc.js charts responsive
- How do I make a custom axis formatter for hours & minutes in d3.js?
- How to make a d3 plugin?
- How do you make an SVG element mouse event bubble up through another element?
- How to make a dashed line legend with d3.js
- How do I tell D3 to make the dendrogram vertical (top down) instead of left to right?
- How to make data() key function to work with TypeScript?
- How to make a color scale in D3 JS to use in fill attribute?
- New to nvD3 - how can I make my unix timestamps appear as dates on the x-axis?
- D3 bubble chart / pack layout - How to make bubbles radiate out from the largest bubbles to the smallest?
- How to draw a exponential function y=ab^x using d3.js axis functionality
- How to show a tooltip with value when mouseover a svg line graph using d3.js?
- How to make graphs in dc.js scrollable within a fixed dimension div?
- How to make the scale to round to the nearest multiple of #?
- How to make curved lines to straight lines for Hierarchy Chart using d3.js
- How do I make a chloropleth based on zip codes?
- How to make an interactive tree chart with d3?
- How to as mouseover to Line Graph Interactive in D3
- how to make an image round in d3.js
- How to display a text when mouseover a node in D3 force layout
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- d3 how to make single stacked column chart
- How to make lines around piechart position evenly?
More Query from same tag
- Data enter/exit handler
- D3 force-directed graph adding new nodes causes x & y to be NaN
- Arrows are not touching to nodes in d3.js in tree layout
- D3 Millisecond tick interval issue
- D3js: How do I clear the zoom scale set by the d3.zoom event?
- d3js display arrow at the end of each axis
- (D3) Data Driven Documents -- Transition Repetition
- How to animate multiple circles on a path in d3.js?
- d3.js smooth unrolling path transition
- how to create separate bar chart with JSON array set?
- D3.js pie chart not showing all arcs
- d3.js: tickformat - adding a % sign without multiplying by 100
- d3.js not reading JSON (d3.js + dc.js + crossfilter)
- Barchart labels shouldn't go into the bars
- Failed to generating two pie charts on the same page in d3.js
- How to set the domain and scale on an yaxis on a discreteBarChart nvd3.js
- How can I create hierarchical bar chart in d3.js
- D3 V4, How can I have a tick mark at every data point, but a label at only select data points
- D3 multi series line graph missing 3rd line
- d3js Text Transition I example with v3 library
- D3.js Rotating / Plotting a Bar chart horizondally
- Create color range with radial gradients using d3.js
- Dynamically Centering Filtered Node within SVG Element in D3
- Still confused by d3's enter method
- Correct usage of stack.values([accessor]) in D3 Streamgraph?
- d3 donut chart not visible entirely when in mobile view
- How to highlight activeEntries in ngrx-charts
- D3 Javascript - these lines won't move
- Unexpected value translate parsing transform attribute
- D3 Rolling map updates d3.geo.circle, but not arcs