Skip to content

Commit fb9c533

Browse files
committed
added tooltip functionality
1 parent ba63f6d commit fb9c533

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/app/components/Chart.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,36 @@ class Chart extends Component {
9090
node.append("circle")
9191
.attr("r", 15)
9292

93+
// creating a d3.tip method where the html has a function that returns the data we passed into tip.show from line 120
94+
let tip = d3.tip()
95+
.attr("class", "d3-tip")
96+
.html(function (d) { return "State Snapshot: " + d; })
97+
// invoking tooltip for nodes
98+
node.call(tip)
99+
93100
node
94101
.append("text")
95102
// adjusts the y coordinates for the node text
96103
.attr("dy", "0.276em")
97104
.attr("x", function (d) {
98105
// this positions how far the text is from leaf nodes (ones without children)
99106
// negative number before the colon moves the text of rightside nodes, positive number moves the text for the leftside nodes
100-
return d.x < Math.PI === !d.children ? -6 : 7;
107+
return d.x < Math.PI === !d.children ? -5 : 9;
101108
})
102109
.attr("text-anchor", function (d) { return d.x < Math.PI === !d.children ? "start" : "end"; })
103110
// this arranges the angle of the text
104111
.attr("transform", function (d) { return "rotate(" + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 1 / Math.PI + ")"; })
105112
.text(function (d) {
106113
return d.data.index;
107114
});
115+
116+
//applying tooltip on mouseover and removes it when mouse cursor moves away
117+
node
118+
.on('mouseover', function (d) {
119+
// without JSON.stringify, data will display as object Object
120+
tip.show(JSON.stringify(d.data.stateSnaphot))
121+
})
122+
.on('mouseout', tip.hide)
108123

109124
function reinfeldTidierAlgo(x, y) {
110125
return [(y = +y) * Math.cos(x -= Math.PI / 2), y * Math.sin(x)];

0 commit comments

Comments
 (0)