question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scale axis with zoom

See original GitHub issue

When using the zoom component I would like to apply the transform matrix to my axis so that they represent the new visible domain. How would I go about this using vx?

  // Scales aren't necessarily linear and time
  const xScale = scaleTime({
    range: [0, xMax],
    domain: extent(data, x)
  });

  const yScale = scaleLinear({
    range: [yMax, 0],
    domain: [0, max(data, y)]
  });

  <Zoom
      width={width}
      height={height}
      scaleXMin={1}
      scaleXMax={4}
      scaleYMin={1}
      scaleYMax={4}
      transformMatrix={initialTransform}
    >
      {zoom =>
        <svg width={width} height={height}>
          <Group>
            <Group transform={zoom.toString()}>
               {renderLine()} // Code rendering actual graph that can be zoomed
            </Group>
            // Axis live outside group with zoom transform
            <AxisLeft
              scale={???} // What goes here?
              top={0}
              left={0}
            />
            <AxisBottom
              scale={???} // What goes here?
              top={yMax}      
             />
         </Group>
       }
    </Zoom>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

2reactions
sseiracommented, Sep 24, 2019

Yup, here is a method I’m using to scale my Axis based on the zoom. Its all about updating the domain to what is now visible due to the zoom area.

const rescaleYAxis = (scale, zoom) => {
  let newDomain = scale.range().map((r) => {
    return scale.invert((r - zoom.transformMatrix.translateY)/zoom.transformMatrix.scaleY)
  })
  return scale.copy().domain(newDomain)
}
2reactions
neb42commented, May 21, 2019

Correction, this is working for me for both linear and time. The trick was just to convert everything to pixel values, apply the transforms, then convert back to the domain.

const xScaleTransformed = scaleTime({
  range: [0, xMax],
  domain: [
    xScale.invert((xScale(extent(data, x)[0]) - zoom.transformMatrix.translateX) / zoom.transformMatrix.scaleX),
    xScale.invert((xScale(extent(data, x)[1]) - zoom.transformMatrix.translateX) / zoom.transformMatrix.scaleX),
  ],
});

const yScaleTransformed = scaleLinear({
  range: [yMax, 0],
  domain: [
    yScale.invert((yScale(0) - zoom.transformMatrix.translateY) / zoom.transformMatrix.scaleY),
    yScale.invert((yScale(max(data, y)) - zoom.transformMatrix.translateY) / zoom.transformMatrix.scaleY),
  ],
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scale axis with zoom #1138 - airbnb/visx - GitHub
Yup, here is a method I'm using to scale my Axis based on the zoom. Its all about updating the domain to what...
Read more >
Pan & Zoom Axes / D3 - Observable
Pan & Zoom Axes. This example demonstrates using d3-zoom to drive changes to scales' domains via transform.rescaleX and transform.rescaleY. The ...
Read more >
Zooming/Rescaling the Graphs Work Area
To rescale only one axis, hold down Shift and drag a tic mark on the axis. ... From the Window / Zoom menu,...
Read more >
AxisScaleView.Zoom Method - Microsoft Learn
Sets a new axis data view and/or position based on the specified start position, view size, unit of measurement and save state.
Read more >
Zooming and Scrolling - KST Plot
Y-Zoom Maximum, Shift+M, Sets the y axis scale such that the y values for all data points are between the minimum and maximum...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found