Skip to content

Commit 2cc1c22

Browse files
committed
trying a new tooltip functionality to hopefully please eslint
1 parent 54530fa commit 2cc1c22

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
},
2525
"ecmaVersion": 2018,
2626
"sourceType": "module"
27-
},
27+
}
2828
}

src/app/components/Chart.jsx

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import React, { Component } from 'react';
1313
// import PropTypes from 'prop-types';
1414
import * as d3 from 'd3';
15-
import d3Tip from 'd3-tip';
15+
// import d3Tip from 'd3-tip';
1616

1717
let root = {};
1818
class Chart extends Component {
@@ -106,14 +106,14 @@ class Chart extends Component {
106106
node.append('circle')
107107
.attr('r', 10);
108108

109-
// creating a d3.tip method where the html has a function that returns
110-
// the data we passed into tip.show from line 120
111-
const tip = d3Tip()
112-
.attr('class', 'd3-tip')
113-
.html(function (d) { return 'State: ' + d; });
109+
// // creating a d3.tip method where the html has a function that returns
110+
// // the data we passed into tip.show from line 120
111+
// const tip = d3Tip()
112+
// .attr('class', 'd3-tip')
113+
// .html(function (d) { return 'State: ' + d; });
114114

115-
// invoking tooltip for nodes
116-
node.call(tip);
115+
// // invoking tooltip for nodes
116+
// node.call(tip);
117117

118118
node
119119
.append('text')
@@ -160,14 +160,33 @@ class Chart extends Component {
160160
g.attr('transform', d3.event.transform);
161161
}
162162

163+
// define the div for the tooltip
164+
const tooltipDiv = d3.select('body').append('div')
165+
.attr('class', 'tooltip')
166+
.style('opacity', 0);
167+
163168
// applying tooltip on mouseover and removes it when mouse cursor moves away
164169
node
165170
.on('mouseover', function (d) {
166-
// without JSON.stringify, data will display as object Object
167-
// console.log('d.data --> ', JSON.stringify(d.data))
168-
tip.show(JSON.stringify(d.data.stateSnapshot.children[0].state), this);
171+
tooltipDiv.transition()
172+
.duration(200)
173+
.style('opacity', 0.9);
174+
tooltipDiv.html(JSON.stringify(d.data.stateSnapshot.children[0].state), this)
175+
.style('left', (d3.event.pageX) + 'px')
176+
.style('top', (d3.event.pageY - 28) + 'px');
169177
})
170-
.on('mouseout', tip.hide);
178+
// eslint-disable-next-line no-unused-vars
179+
.on('mouseout', function (d) {
180+
tooltipDiv.transition()
181+
.duration(500)
182+
.style('opacity', 0);
183+
});
184+
// .on('mouseover', function (d) {
185+
// // without JSON.stringify, data will display as object Object
186+
// // console.log('d.data --> ', JSON.stringify(d.data))
187+
// tip.show(JSON.stringify(d.data.stateSnapshot.children[0].state), this);
188+
// })
189+
// .on('mouseout', tip.hide);
171190

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

src/app/styles/components/d3graph.css

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,22 @@ body {
4343
stroke-width: 5px;
4444
}
4545

46-
.d3-tip {
46+
div.tooltip {
47+
position: absolute;
48+
text-align: center;
49+
display: inline;
50+
max-width: 400px;
51+
overflow-wrap: break-word;
52+
padding: 6px;
53+
color: #320a5c;
54+
font-size: 15px;
55+
font-family: "Overpass Mono", monospace;
56+
background: #9cf4df;
57+
border-radius: 8px;
58+
pointer-events: none;
59+
}
60+
61+
/* .d3-tip {
4762
line-height: 1;
4863
padding: 6px;
4964
background: #9cf4df;
@@ -53,7 +68,7 @@ body {
5368
max-width: 400px;
5469
overflow-wrap: break-word;
5570
font-family: "Overpass Mono", monospace;
56-
}
71+
} */
5772

5873
/* Creates a small triangle extender for the tooltip
5974
.d3-tip:after {
@@ -68,8 +83,8 @@ body {
6883
} */
6984

7085
/* Style northward tooltips specifically */
71-
.d3-tip.n:after {
86+
/* .d3-tip.n:after {
7287
margin: -2px 0 0 0;
7388
top: 100%;
7489
left: 0;
75-
}
90+
} */

0 commit comments

Comments
 (0)