Comments (4)
Hi @hueypeard thanks for checking out vx and the helpful example code!
Here's an example of working tooltips on the centroid on mouseEnter/Leave that avoids the ref on the <Arc />
and the groupArgs
prop: https://www.webpackbin.com/bins/-KojHA8GeGMHfwdcwAIk
Things to note:
- You can wrap your
<Arc />
in a<g ref={g => this.mainGroup = g}>
- event handlers have a thunk style signature on vx components
onMouseEnter={data => event => { console.log(data) }}
- I'm using the
withTooltip
higher-order component to pass in the common tooltip pattern of updating the tooltip position and data. This HOC passes inhideTooltip()
,showTooltip()
,tooltipLeft
,tooltipTop
, andtooltipData
. I'm using the<Tooltip />
from@vx/tooltip
but you can use your own from your example - as you pointed out you don't get the
centroid
in the event handler. I agree this should get added to<Arc />
and will do that in the next release.
Let me know if you have questions about implementation or other ideas for improvement. I want to make vx a comfortable api to work with for developers and your feedback will only help improve vx for everyone as we go!
from visx.
Added centroid
to the event handler data here: #101
from visx.
Hi! :D
Can you give an example of where you would need the ref to get the translated position of an element?
from visx.
Sorry for the elongated example, but I figured the context is necessary. The centroid argument on
Arc's onMouseEnter is something I am submitting an issue about soon. groupArgs on Arc is another thing I am also submitting an issue about, and it simply allows the developer to supply additional arguments to supply to the Arc's containing group.
tl;dr onMouseEnter triggers setActiveData with the screen coordinates of the centroid, and leaving the graph triggers setActiveData with null disabling the tooltip.
class MyChart extends React.Component {
render() {
const {
width,
height,
setActiveData,
activeData,
data,
events = false
} = this.props;
const radius = Math.min(width, height) / 2;
return (
<div>
<svg
width={width}
height={height}
ref={ref => {
this.svg = ref;
}}
>
<Arc
data={_.map(data, (data, dataIdx) => ({
value: data.length,
data: data,
key: dataIdx
}))}
top={radius}
left={radius}
pieValue={d => d.value}
outerRadius={radius}
stroke="#FFFFFF"
strokeWidth={2}
fill="#FF00FF"
onMouseEnter={(e, arc, centroid) => {
// get screen offset of centroid
const point = this.svg.createSVGPoint();
point.x = centroid[0];
point.y = centroid[1];
// transform centroid according to main group transforms
const mainGroupNode = ReactDOM.findDOMNode(this.mainGroup);
const newPoint = point.matrixTransform(mainGroupNode.getScreenCTM());
// adjust screen offset to be document position
const left = window.pageXOffset ||
document.documentElement.scrollLeft;
const top = window.pageYOffset ||
document.documentElement.scrollTop;
const documentX = newPoint.x + left;
const documentY = newPoint.y + top;
setActiveData({
key: arc.data.key,
x: documentX,
y: documentY
});
}}
style={{
pointerEvents: "all"
}}
groupArgs={{
onMouseLeave: () => setActiveData(null)
}}
ref={ref => {
this.mainGroup = ref;
}}
/>
</svg>
{activeData !== null &&
<div
style={{
position: "absolute",
top: activeData.y,
left: activeData.x,
transform: "translateY(-50%)",
pointerEvents: "none"
}}
>
<div
style={{ position: "relative" }}
className="tooltip-wrapper__placement-right"
>
<Tooltip
style={{
pointerEvents: "none"
}}
>
Tooltip!
</Tooltip>
</div>
</div>}
</div>
);
}
}
`
from visx.
Related Issues (20)
- computeStats throws exception when all statistics align
- XYChart has a zero width or height, bailing - console.log HOT 5
- Tooltip in XYChart - a way to make it compatible for mobile/touch devices
- scroll appears when drawing xychart using LineSeries but doesn't when using AreaStack
- Brush onBrushEnd does not seem to report the end x position HOT 1
- <Axis /> labelProps is documented as Partial, but overwrites values HOT 1
- Adding node version into .npmrc files HOT 1
- Adding inertia option to visx-drag [enhancement] HOT 3
- XYChart forces me to have the yScale include 0 HOT 2
- Zoom: Enable customisation of event handlers HOT 2
- Docs: README.md broken slack link HOT 1
- TooltipInPortal used inside a microfrontend obtains an unstyled (and unstylable) tooltip content HOT 4
- Brush jumps to wrong position when changing selection with a handle
- Nearest datum bug in XYChart HOT 3
- tickFormat function is not working with Typescript HOT 1
- Tooltip with an arrow HOT 7
- Help needed - XY scatter chart with line & area series HOT 5
- Package Responsive: `useParentSize` is not updating the width and height
- Poor performance - XYChart (w/captureEvents) + AnimatedBarSeries + NextJS
- onBrushEnd infinite render loop
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from visx.