Comments (18)
@hshoff any way you could update the links to those examples? They no longer seem to work: https://www.webpackbin.com/bins/-KpGjCM5yQ4nkND4c-P-
from visx.
Yup! This is coming.
from visx.
@thienanle it does but it's a bit more involved. I'm working on making this easier for folks, but haven't settled on an api I'm happy with just yet.
Here's an example of how it works: https://www.webpackbin.com/bins/-KpGjCM5yQ4nkND4c-P-
Things to note:
- use a stateful react component (
class
) so you can grab aref
to the svg<svg ref={s => this.svg = s}>
, you'll use this in your event handlers - i'm using a transparent
<Bar />
overlay on top of the chart to handle the mouse events onMouseMove
// convert event coords to local svg coords
const { x } = localPoint(this.svg, event);
// use xScale.invert to get x value
const x0 = xScale.invert(x);
// use d3-array bisector to find the data value
const index = bisectDate(data, x0, 1);
const d0 = data[index - 1];
const d1 = data[index];
const d = x0 - xStock(d0.date) > xStock(d1.date) - x0 ? d1 : d0;
// use `withTooltip`s `showTooltip()` to update the position and data
showTooltip({
tooltipData: d,
tooltipLeft: x,
tooltipTop: yScale(d.close),
});
I'm hoping to make this easier in the future as I'll be needing to use it for @vx/brush @vx/drag interactions . But for now this should get you up and going with position based on event positions.
Happy to answer any questions if things come up.
from visx.
any progress on this one?
from visx.
🎉🎉🎉
from visx.
psyched to see this! 🎉 🏆
from visx.
So 1337!
from visx.
@hshoff does this work with LinePath
/AreaClosed
? Behavior seems inconsistent with BarStack
in the demos.
from visx.
@hshoff thank you for the example, super helpful!
Yes it would be great to not have to use a transparent <Bar />
and have the mouse events on the <AreaClosed />
instead. Thanks for your work on this!
I was also thinking, in the case where a chart has more than 1 series (multiple <LinePath />
components for example), would it make sense to put the mouse events on each? Perhaps on a wrapping component (something like <Group />
) would make more sense?
from visx.
@thienanle yup! you can put the events on the <AreaClosed />
as of @vx/[email protected]
. I just used the transparent <Bar />
above to give a bit target area on mouse move for the demo, but it's not necessary.
Example of events on <AreaClosed />
: https://www.webpackbin.com/bins/-KpN6KNn40htehDwd80C
For multiple <LinePath />
s it depends what you're trying to do, you could put them on each or you could put it on a <Group />
like so: https://www.webpackbin.com/bins/-KpN9sBHl_pBgbCcLFCt
from visx.
@hshoff thanks for your example above, it solved my problem!
Have you tested the mouse events on <Arc />
? I love the feature of getting the hovered arc section data onMouseMove
, but I'm getting an error
Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
from visx.
@thienanle could you post your onMouseMove
function? happy to take a look.
from visx.
onMouseMove={data => event => {
const { x, y } = localPoint(this._svg, event);
showTooltip({
tooltipData: data,
tooltipLeft: x,
tooltipTop: y,
});
}}
@hshoff calling showTooltip
is what seems to cause the warning.
from visx.
I solved my issue by calling showTooltip
on onMouseMove
on <Group />
and setting tooltipData
to my component's state (so that I can get the data for the hovered pie).
<Group
onMouseLeave={hideTooltip}
onMouseMove={event => {
const { x, y } = localPoint(this._svg, event);
showTooltip({
tooltipLeft: x,
tooltipTop: y,
});
}}
>
<Arc
onMouseMove={data => event => {
this.setState({
tooltipData: data,
});
}}
/>
</Group>
But it would be nice to be able to do everything on <Arc />
!
from visx.
@hshoff second
from visx.
@hshoff third
from visx.
The demos all now link out to code sandboxes, this is the Areas
sandbox.
As harry said above, there is an invisible Bar
on top of the Areas which triggers the tooltip anywhere you mouse over, but you could change this behavior by moving the onMouse/TouchMove
handler to the Area
, or whatever is relevant for your series shape.
from visx.
Please show example with normal tooltip for multiline chart (hover on marker circle should show value of series item).
from visx.
Related Issues (20)
- 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
- Not able to import bisector from @visx/vendor/d3-array
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.