简体   繁体   English

如何使用 react-zoom-pan-pinch 来允许用户查看根据用户输入更改大小的所有图表?

[英]How can I use react-zoom-pan-pinch to allow users to view all of a chart that changes size based on user input?

I've designed and built an interactive Org Chart that allows users to view business organizations in a hierarchical format.我设计并构建了一个交互式组织结构图,允许用户以分层格式查看业务组织。 By default only the first row below the root node of the chart is visible, but users can click to expand the chart further, thereby changing the size of the chart.默认情况下,只有图表根节点下方的第一行可见,但用户可以单击以进一步展开图表,从而更改图表的大小。 Users can also drag and drop nodes to simulate a reorganization of the business.用户还可以通过拖放节点来模拟业务的重组。

I'm currently using react-zoom-pan-pinch to allow users to zoom and pan the chart.我目前正在使用react-zoom-pan-pinch来允许用户缩放和平移图表。 It works very well when the org chart has not been expanded too much, but becomes problematic at larger chart scales.当组织结构图没有扩展太多时,它工作得很好,但在更大的图表比例下会出现问题。

The problem is that the organizations being represented by the chart are very broad in comparison to their depth, meaning a fully expanded chart is a horizontal rectangle, not a square.问题在于图表所代表的组织与其深度相比非常广泛,这意味着完全展开的图表是一个水平矩形,而不是正方形。 react-zoom-pan-pinch will only allow me to zoom out to the maximum vertical extent of the chart, meaning users can't view a fully expanded organization without scrolling from side to side. react-zoom-pan-pinch只允许我缩小到图表的最大垂直范围,这意味着用户无法在不左右滚动的情况下查看完全展开的组织。 This is not an acceptable behavior.这是不可接受的行为。

This is for work, so I cannot post code without violating numerous agreements.这是为了工作,所以我不能在不违反许多协议的情况下发布代码。 Instead I have linked to the react-zoom-pan-pinch documentation and will go over what I have tried changing.相反,我已链接到react-zoom-pan-pinch文档,并将回顾我尝试更改的内容。

The first place I looked was the TransformWrapper Props section of the documentation.我首先查看的是文档的 TransformWrapper Props 部分。

There I found the inititalScale , minScale , and maxScale props.在那里我找到了inititalScaleminScalemaxScale道具。

I can set the initialScale prop to a value of less than 1, and obtain something close to the result I want at first.我可以将initialScale道具设置为小于 1 的值,并获得接近我最初想要的结果的东西。 Setting it to 0.5 results in the chart being zoomed out further than normally possible, but when I zoom in to a value of 1 I am unable to zoom back out.将其设置为 0.5 会导致图表比通常情况下缩小得更远,但是当我放大到 1 值时,我无法缩小。 This was expected, as the minScale prop was still set to 1.这是意料之中的,因为minScale道具仍然设置为 1。

Having checked that the props indeed work, I went ahead and set minScale to 0.5, assuming I would be able to zoom back out to the initial view seen when initialScale is set to 0.5.检查过的道具确实工作,我继续设置minScale 0.5,假设我将能够放大出来时最先看到的视图initialScale设定为0.5。 This seemed like it should work, but it did not.这似乎应该起作用,但它没有。 Even with the minScale prop set to 0.5, I am unable to zoom back out after zooming in to a value of 1. This is very strange to me, as the acceptance of 0.5 as the initialScale prop and subsequent rendering of the chart indicates that values below 1 are acceptable.即使在minScale道具设定为0.5,我无法在缩放到1的值。这是很奇怪,我后放大了,作为接受0.5作为initialScale道具和图表的后续呈现指示值低于1是可以接受的。

I am now messing around with the rest of the props listed in the documentation, but have yet to achieve the desired result (infinite zoomout).我现在正在处理文档中列出的其余道具,但还没有达到预期的结果(无限缩小)。

I believe the root of the issue is that react-zoom-pan-pinch is meant for images, not things that change size and aspect ratio, but it is a good package and I would prefer to keep using it.我相信问题的根源在于react-zoom-pan-pinch是针对图像的,而不是改变大小和纵横比的东西,但它是一个很好的包,我更愿意继续使用它。

Is anyone familiar enough with this package to know the settings I should be using to allow infinite zoom out, and if so what are those settings?有没有人对这个包足够熟悉,知道我应该使用哪些设置来允许无限缩小,如果是的话,这些设置是什么?

I discovered the answer to my own question.我发现了我自己问题的答案。 It turns out the minScale , maxScale , and other props were not being passed to the component properly.事实证明minScalemaxScale和其他道具没有正确传递给组件。 Once they are passed properly the package works very well.一旦它们被正确传递,包就可以很好地工作。 Here's my explanation/fix这是我的解释/修复

The documentation suggests doing this:文档建议这样做:


import { TransformWrapper, TransformComponent } from "react-zoom-pan-pinch";

class Example extends Component {
  render() {
    return (
      <TransformWrapper
        initialScale={1}
        minScale={0.5}
        maxScale={7}
        initialPositionX={200}
        initialPositionY={100}
      >
        {({ zoomIn, zoomOut, resetTransform, ...rest }) => (
          <React.Fragment>
            <div className="tools">
              <button onClick={() => zoomIn()}>+</button>
              <button onClick={() => zoomOut()}>-</button>
              <button onClick={() => resetTransform()}>x</button>
            </div>
            <TransformComponent>
              <img src="image.jpg" alt="test" />
              <div>Example text</div>
            </TransformComponent>
          </React.Fragment>
        )}
      </TransformWrapper>
    );
  }
}

The above doesn't work, and the minScale and maxScale props aren't passed to the component.以上不起作用,并且minScalemaxScale道具不会传递给组件。 If you open the React dev tools in your browser and go to TransformWrapper , you'll see the default values of 1 for minScale and 8 for maxScale , not the values you entered in your code.如果你打开你的浏览器阵营开发工具,去TransformWrapper ,你会看到1的默认值minScale和8 maxScale ,不是你在你的代码中输入的值。

You can solve the problem by creating an object:你可以通过创建一个对象来解决这个问题:

  const transformOptions = {
    initialScale: 1,
    minScale: 0.5,
    maxScale: 2
  }

Then setting an options prop inside the TransformWrapper component equal to the object, like so:然后在 TransformWrapper 组件内设置一个等于对象的options道具,如下所示:


import { TransformWrapper, TransformComponent } from "react-zoom-pan-pinch";

class Example extends Component {
  render() {
    return (
      <TransformWrapper
        initialScale={1}
        options={transformOptions}
        initialPositionX={200}
        initialPositionY={100}
      >
        {({ zoomIn, zoomOut, resetTransform, ...rest }) => (
          <React.Fragment>
            <div className="tools">
              <button onClick={() => zoomIn()}>+</button>
              <button onClick={() => zoomOut()}>-</button>
              <button onClick={() => resetTransform()}>x</button>
            </div>
            <TransformComponent>
              <img src="image.jpg" alt="test" />
              <div>Example text</div>
            </TransformComponent>
          </React.Fragment>
        )}
      </TransformWrapper>
    );
  }
}

The same thing applies to pan, wheel, and zoom options.同样的事情适用于平移、滚轮和缩放选项。 They don't work if set directly in the component as suggested by the documentation, but do work if you create objects like I did above.如果按照文档的建议直接在组件中设置,它们将不起作用,但如果您像我上面那样创建对象,它们就起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM