Skip to content

Commit 273d234

Browse files
committed
d3 tool tip, restyling
1 parent e7d76e5 commit 273d234

File tree

2 files changed

+136
-29
lines changed

2 files changed

+136
-29
lines changed

src/app/components/Chart.jsx

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,35 @@ import '../styles/components/_d3Tree.scss';
33
import * as d3 from 'd3';
44

55
var root={};
6+
let duration = 750;
7+
8+
// var test={
9+
// name: 'root',
10+
// state: {
11+
// "kids": 0,
12+
// "kids1": 0,
13+
// "kids2": 0,
14+
// "kids3": 0,
15+
// "kids4": 0,
16+
// "kids5": {
17+
// subkid: 3,
18+
// subkid3: 5,
19+
// sukid5:{
20+
// kid:1,
21+
// kid:3
22+
// }
23+
// },
24+
// "kids6": 0,
25+
// "kids7": 0,
26+
// "kids8": 0,
27+
// "kids9": 0,
28+
// "kids10": 0,
29+
// "kids11": 0,
30+
// "kids12": 0,
31+
// "kids13": 0,
32+
// },
33+
// children: [{name: 'hello',state:{name: "codesmith"}},{name: 'bye'}]
34+
// }
635

736
class Chart extends Component {
837
constructor(props){
@@ -31,15 +60,17 @@ class Chart extends Component {
3160
maked3Tree(){
3261
this.removed3Tree();
3362

63+
duration=0;
64+
// root = test;
65+
3466
var margin = {top: 20, right: 120, bottom: 20, left: 120},
3567
width = 960 - margin.right - margin.left,
3668
height = 800 - margin.top - margin.bottom;
3769

38-
var i = 0,
39-
duration = 750;
70+
var i = 0;
4071

4172
var tree = d3.layout.tree()
42-
.size([height, width]);
73+
.size([400, 400]);
4374

4475
var diagonal = d3.svg.diagonal()
4576
.projection(function(d) { return [d.y, d.x]; });
@@ -50,6 +81,14 @@ class Chart extends Component {
5081
.append("g")
5182
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
5283

84+
// Add tooltip div
85+
var div = d3.select("body").append("div")
86+
.attr("class", "tooltip")
87+
.style("opacity", 1e-6)
88+
.on("mouseover", tipMouseover)
89+
.on("mouseout", tipMouseout)
90+
91+
5392

5493
root.x0 = height / 2;
5594
root.y0 = 0;
@@ -74,7 +113,9 @@ class Chart extends Component {
74113
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
75114
.on("click", click)
76115
.on("mouseover", mouseover)
77-
.on("mouseout", mouseout);
116+
.on("mouseout", mouseout)
117+
.on("mousemove", function(d){mousemove(d);});
118+
78119

79120
nodeEnter.append("circle")
80121
.attr("r", 1e-6)
@@ -85,6 +126,7 @@ class Chart extends Component {
85126
.attr("dy", ".35em")
86127
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
87128
.text(function(d) { return d.name; })
129+
.style('fill', '#6FB3D2')
88130
.style("fill-opacity", 1e-6);
89131

90132
// Transition nodes to their new position.
@@ -93,8 +135,8 @@ class Chart extends Component {
93135
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
94136

95137
nodeUpdate.select("circle")
96-
.attr("r", 4.5)
97-
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
138+
.attr("r", 7)
139+
.style("fill", function(d) { return d._children ? "#A1C658" : "#D381C3"; });
98140

99141
nodeUpdate.select("text")
100142
.style("fill-opacity", 1);
@@ -158,17 +200,49 @@ class Chart extends Component {
158200

159201
// Show state on mouse over
160202
function mouseover(d) {
161-
d3.select(this).append("text")
162-
.attr("class", "hover")
163-
.attr('transform', function(d){
164-
return 'translate(5, -10)';
165-
})
166-
.text(d.state === undefined ? '' : JSON.stringify(d.state));
203+
// d3.select(this).append("text")
204+
// .attr("class", "hover")
205+
// .attr('transform', function(d){
206+
// return 'translate(5, -10)';
207+
// })
208+
// .text(d.state === undefined ? '' : JSON.stringify(d.state));
209+
210+
div.transition()
211+
.duration(300)
212+
.style("display", "block")
213+
.style("opacity", 1)
167214
}
168215

169-
// Toggle children on click.
170216
function mouseout(d) {
171-
d3.select(this).select("text.hover").remove();
217+
// d3.select(this).select("text.hover").remove();
218+
219+
div.transition()
220+
.duration(3000)
221+
.style("opacity", 1e-6)
222+
.style("display", "none");
223+
}
224+
225+
function tipMouseover(d){
226+
div.transition()
227+
.duration(300)
228+
.style("opacity", 1);
229+
}
230+
231+
function tipMouseout(d) {
232+
// d3.select(this).select("text.hover").remove();
233+
234+
div.transition()
235+
.duration(3000)
236+
.style("opacity", 1e-6)
237+
.style("display", "none");
238+
}
239+
240+
241+
function mousemove(d){
242+
div
243+
.text(d.state == undefined ? 'No state found' : JSON.stringify(d.state,null,4))
244+
.style("left", (d3.event.pageX ) + "px")
245+
.style("top", (d3.event.pageY) + "px");
172246
}
173247

174248
function collapse(d) {
@@ -181,16 +255,14 @@ class Chart extends Component {
181255

182256
// root.children.forEach(collapse);
183257
update(root);
258+
duration = 750;
184259

185260
// d3.select(self.frameElement).style("height", "800px");
186261

187262
}
188263

189264
render(){
190-
return (
191-
<div ref="anchor" className={'d3Container'} width={'100%'}>
192-
</div>
193-
)
265+
return <div ref="anchor" className={'d3Container'} width={'100%'}></div>
194266
}
195267
}
196268

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
1+
.d3Container{
2+
display: flex;
3+
align-items: center;
4+
justify-content: center;
5+
}
6+
17
.node {
28
cursor: pointer;
3-
}
9+
}
410

5-
.node circle {
11+
.node circle {
612
fill: #fff;
7-
stroke: steelblue;
8-
stroke-width: 1.5px;
9-
}
13+
stroke: black;
14+
stroke-width: 2px;
15+
16+
}
1017

11-
.node text {
12-
font: 10px sans-serif;
13-
}
18+
.node text {
19+
font: 15px sans-serif;
20+
color: white;
21+
}
1422

15-
.link {
23+
.link {
1624
fill: none;
17-
stroke: #ccc;
25+
stroke: black;
1826
stroke-width: 1.5px;
19-
}
27+
}
28+
29+
div.tooltip {
30+
white-space: pre;
31+
position: absolute;
32+
// display: flex;
33+
// flex-direction: column;
34+
// flex-wrap: nowrap;
35+
// justify-content: center;
36+
overflow-y: scroll;
37+
overscroll-behavior: contain;
38+
// align-items: center;
39+
flex: auto;
40+
// min-height: 10vh;
41+
// min-width: 10vh;
42+
// max-height: 100px;
43+
// max-width: 50vh;
44+
// text-align: center;
45+
// width: 300px;
46+
// height: 100px;
47+
padding: 8px;
48+
font: 15px sans-serif;
49+
color: black;
50+
background: #ffff;
51+
border: solid 1px #aaa;
52+
border-radius: 8px;
53+
// pointer-events: none;
54+
}

0 commit comments

Comments
 (0)