Coder Social home page Coder Social logo

Comments (4)

hshoff avatar hshoff commented on May 5, 2024 1

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 in hideTooltip(), showTooltip(), tooltipLeft, tooltipTop, and tooltipData. 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.

hshoff avatar hshoff commented on May 5, 2024 1

Added centroid to the event handler data here: #101

from visx.

Flaque avatar Flaque commented on May 5, 2024

Hi! :D
Can you give an example of where you would need the ref to get the translated position of an element?

from visx.

hueypeard avatar hueypeard commented on May 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.