Skip to content

Commit dde59db

Browse files
committed
fix excessive tooltip generation
1 parent c875379 commit dde59db

File tree

2 files changed

+49
-59
lines changed

2 files changed

+49
-59
lines changed

src/EsnetMatrix.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ interface Props extends PanelProps<MatrixOptions> {
1414
}
1515

1616
export const EsnetMatrix: React.FC<Props> = ({ options, data, width, height, id }) => {
17-
// console.log(data);
1817
const theme = useTheme2();
19-
// console.log(options);
18+
console.log(options);
2019
const parsedData = parseData(data, options, theme);
2120
try {
2221
if (parsedData.data === 'too many inputs') {
@@ -31,15 +30,13 @@ export const EsnetMatrix: React.FC<Props> = ({ options, data, width, height, id
3130
if (parsedData.data == null) {
3231
return <div>No Data</div>;
3332
}
34-
// const divStyle = {
35-
// width: 'auto',
36-
// };
3733

3834
const ref = Matrix.matrix(parsedData.rows, parsedData.columns, parsedData.data, id, height, options, parsedData.legend);
35+
const thisPanelClass = `matrix-panel-${id}`;
3936

4037
return (
4138
<CustomScrollbar autoHeightMin="100%">
42-
<div ref={ref}></div>
39+
<div ref={ref} id={thisPanelClass}></div>
4340
</CustomScrollbar>
4441
);
4542
};

src/matrix.js

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function createViz(elem, id, height, rowNames, colNames, matrix, options, theme,
3030

3131
// do a bit of work to setup the visual layout of the wiget --------
3232
if (elem === null) {
33-
console.log('bailing after failing to find parent element');
33+
console.error('bailing after failing to find parent element');
3434
return;
3535
}
3636
while (elem !== undefined && elem.firstChild) {
@@ -62,25 +62,6 @@ function createViz(elem, id, height, rowNames, colNames, matrix, options, theme,
6262
width = colNames.length * cellSize,
6363
height = rowNames.length * cellSize;
6464

65-
//we'll use this div as our tooltip.
66-
//the div will be invisible except when in use
67-
//the div will start at page coordinates 0,0
68-
//and be moved into place on hover
69-
//on mouse out the div will move back to 0,0 so
70-
//as not to be covering other boxes we want to hover
71-
var tooltip = d3
72-
.select('body')
73-
.append('div')
74-
.attr('class', 'matrix-tooltip')
75-
.style('background-color', theme.colors.background.primary)
76-
.style('font-family', theme.typography.fontFamily.sansSerif)
77-
.style('font-color', theme.colors.text.primary)
78-
.style('box-shadow', '3px 3px 4px lightgray')
79-
.style('padding', '5px')
80-
.style('z-index', '500')
81-
.style('position', 'absolute')
82-
.style('width', 'fit-content')
83-
.style('opacity', 0);
8465

8566
// append the svg object to the body of the page
8667
var svgClass = `svg-${id}`;
@@ -113,23 +94,30 @@ function createViz(elem, id, height, rowNames, colNames, matrix, options, theme,
11394
.attr('fill', theme.colors.text.primary)
11495
.call(truncateLabel, maxTxtLength)
11596
.on('mouseover', function (event, d) {
116-
tooltip.html(d);
117-
118-
//to center the tooltip appropriately we need to find the rendered width of both the
119-
//the box they hovered and of the tooltip with the text in it.
120-
// var rect = event.target.getBoundingClientRect();
121-
var divSize = tooltip.node().getBoundingClientRect();
122-
123-
// tooltip for label
124-
tooltip
125-
.style('left', event.pageX - divSize.width + 'px')
97+
// var divSize = tooltip.node().getBoundingClientRect();
98+
let tooltip = d3
99+
.select('body')
100+
.append('div')
101+
.attr('class', `matrix-tooltip-${id}`)
102+
.html(d)
103+
.style('background-color', theme.colors.background.primary)
104+
.style('font-family', theme.typography.fontFamily.sansSerif)
105+
.style('font-color', theme.colors.text.primary)
106+
.style('box-shadow', '3px 3px 4px lightgray')
107+
.style('padding', '5px')
108+
.style('z-index', '500')
109+
.style('position', 'absolute')
110+
.style('width', 'fit-content')
111+
.style('left', event.pageX + 'px')
112+
// .style('left', event.pageX - divSize.width + 'px')
126113
//place the tooltip 5 pixels above the box they hovered
127-
.style('top', event.pageY - divSize.height - 5 + 'px')
128-
.style('opacity', 1);
114+
.style('top', event.pageY - 5 + 'px')
115+
// .style('top', event.pageY - divSize.height - 5 + 'px')
116+
.style('opacity', 1);
129117
})
130118
.on('mouseout', function (d, i) {
131119
d3.select(this).attr('opacity', '1');
132-
tooltip.style('opacity', 0).style('left', '0px').style('top', '0px');
120+
d3.selectAll(`.matrix-tooltip-${id}`).remove();
133121
});
134122

135123
//build the matrix /////////////////////////////////////////
@@ -201,25 +189,31 @@ function createViz(elem, id, height, rowNames, colNames, matrix, options, theme,
201189
.attr('height', y.bandwidth() + 5)
202190
.attr('transform', 'translate(-1, -1)');
203191

204-
//like the mouseover above go ahead and render the text so we can calculate its size
205-
//and position correctly.
206-
tooltip.html(() => {
207-
var thisDisplay = d.display;
208-
var text = `<p><b>${srcText}:</b> ${d.row}
209-
<br>
210-
<b>${targetText}:</b> ${d.col}
211-
<br>
212-
<b>${valText}:</b> ${thisDisplay.text} ${thisDisplay.suffix ? thisDisplay.suffix : ''}
213-
</p>`;
214-
return text;
215-
});
216-
217-
tooltip
218-
// .style('left', rect.left + rect.width - divSize.width / 2 + 'px')
219-
// .style('top', rect.top - divSize.height - 5 + 'px')
192+
let tooltip = d3
193+
.select('body')
194+
.append('div')
195+
.attr('class', `matrix-tooltip-${id}`)
196+
.html(() => {
197+
var thisDisplay = d.display;
198+
var text = `<p><b>${srcText}:</b> ${d.row}
199+
<br>
200+
<b>${targetText}:</b> ${d.col}
201+
<br>
202+
<b>${valText}:</b> ${thisDisplay.text} ${thisDisplay.suffix ? thisDisplay.suffix : ''}
203+
</p>`;
204+
return text;
205+
})
206+
.style('background-color', theme.colors.background.primary)
207+
.style('font-family', theme.typography.fontFamily.sansSerif)
208+
.style('font-color', theme.colors.text.primary)
209+
.style('box-shadow', '3px 3px 4px lightgray')
210+
.style('padding', '5px')
211+
.style('z-index', '500')
212+
.style('position', 'absolute')
213+
.style('width', 'fit-content')
220214
.style('left', event.pageX + 5 + 'px')
221215
.style('top', event.pageY + 5 + 'px')
222-
.style('opacity', 1);
216+
.style('opacity', 1);
223217
}
224218
})
225219
.on('mouseout', function (d, i) {
@@ -229,12 +223,11 @@ function createViz(elem, id, height, rowNames, colNames, matrix, options, theme,
229223
.attr('transform', 'translate(0, 0)')
230224
.attr('width', x.bandwidth())
231225
.attr('height', y.bandwidth());
232-
tooltip.style('opacity', 0).style('left', '0px').style('top', '0px');
226+
d3.selectAll(`.matrix-tooltip-${id}`).transition(50).remove();
233227
})
234228
.on('click', function (d) {
235229
if(linkURL) {
236-
// d3.select(this).remove();
237-
tooltip.remove();
230+
d3.selectAll(`.matrix-tooltip-${id}`).remove();
238231
}
239232
});
240233

0 commit comments

Comments
 (0)