Recharts响应式容器在flexbox中无法正确调整大小

14
我正在尝试为我的数据可视化应用程序创建一个自定义折叠图例。它使用React和Recharts。该组件第一次呈现得很好。但是,当我折叠传说并重新打开它时,响应式容器不会收缩以适应。如果我知道像素的父容器大小,那么这将更容易处理,但是在渲染时我没有那些信息。这是Recharts或Flexbox的错误还是我的做法不对?
以下是代码: https://codesandbox.io/s/8krz9qjk52 澄清一下:问题在于当我关闭并重新打开图例时,图例组件被推出了视觉区域,而图表也没有缩回到原来较小的尺寸。
6个回答

42

2
这对我有用。我不知道为什么它有效。如果有人能解决这个问题,我会非常感激。 - Ryxle
怎么做?因为这个是有效的 :D - rufatZZ
TypeScript说它需要一个数字。 - A.com
它还说方面不存在。 - A.com

10
我知道这是一个相当老的问题,但我在这里发帖,以防万一有人通过Google找到这里。
我不知道为什么这样可以解决问题,但是在 .recharts-wrapper 上设置 position: absolute 将完全解决此问题。到目前为止,我尚未发现任何缺点,但您可能会有所不同。

我遇到了这个问题,而且无法通过任何谷歌搜索的方法来解决它,包括本文。还有其他人有解决方案吗? - dsldsl

6

试一下这个,它可以解决宽度和高度的响应性(调整大小)问题。

<div style={{position: 'relative', width: '100%', paddingBottom: '250px'}}>
  <div
    style={{
      position: 'absolute',
      left: 0,
      right: 0,
      bottom: 0,
      top: 0,
    }}
  >
    <ResponsiveContainer>
      <YourChartGoesHere />
    </ResponsiveContainer>
  </div>
</div>


请您再次检查一下,是否能够正常运行而不出现任何错误信息。错误信息如下:{ "message": "Uncaught SyntaxError: Unexpected token '<'", "filename": "https://stacksnippets.net/js", "lineno": 12, "colno": 9 } - Sumathi J

2
最好的方法是将其宽度设置为100%,然后调整纵横比,直到满意为止。我这样做是为了适应不同的屏幕尺寸。
const theme = useTheme(); //this is from mui
const isMobile = useMediaQuery(theme.breakpoints.down("sm"));

<ResponsiveContainer width="100%" aspect={isMobile?1.5:1.8}>

非常好用


1

由于我在我的项目中遇到了类似的问题,所以我决定坚持这个问题。最终我成功找到了解决方案!

codesandbox

我将所做的更改从大到小列出:

我将状态从CustomLegend提升到CustomChart

  • Now, CustomChart is in charge of what happens. It passes visibility information to CustomLegend as a prop. How does CustomChart know when to change state?

  • CustomChart has a member function called handleButtonClick that sets its state. handleButtonClick is sent to CustomLegend as a prop. So CustomChart renders CustomLegend like this:

    <CustomLegend
        items={legendData}
        onClick={this.handleButtonClick}
        legendVisible={this.state.legendVisible}
    />
    

    Now in CustomLegend, we can include this in the button definition: onClick={this.props.onClick} Note that this pattern only works if the original function is bound to the parent since it alters the parent's state.

"chart-container" 现在是一个 CSS Grid 容器。保留HTML,不做解释。
  • It renders an inline style handling the column width based on state.

    <div
        className="chart-container"
        style={{
          gridTemplateColumns: this.state.legendVisible
            ? "80% 1fr"
            : "1fr min-content"
        }}
    >
    

    Note that 1fr is a fractional unit which, in this case, serves to fill the remaining space.

  • In styles.css, grid-auto-flow is set to column which allows things to be placed in the grid as columns. The things we place into the grid are SampleChart and CustomLegend, so SampleChart is the left column and CustomLegend is the right column.
  • gridTemplateColumns: 80% 1fr: When the legend is visible, we want the left column to take up 80% of the width and the right column to fill the rest.
  • gridTemplateColumns: 1fr min-content: When the legend is invisible, we want the right column to take up the minimum amount of space which its elements fill and the left column to fill the remaining space.

CustomLegend 只有一个 render 方法。

  • 与其使用 renderVisiblerenderHidden,现在只有一个方法,根据 props(依赖于 CustomChart 的状态)有条件地渲染 legend-list
  • legend-container 现在始终渲染(它是一个网格项)。

其他小改动:

  • SampleChart中:<ResponsiveContainer width="100%" height="100%" className={props.className}>已更改,因为<SampleChart className="chart-area" data={data} />实际上会将className作为一个属性发送。请注意!您需要直接在SampleChart返回的层次结构上(在这种情况下是ResponsiveContainer)设置className。尝试返回原始codesandbox并更改styles.csschart-areabackground-color(什么也不会发生!)。
  • styles.css中所有实例的overflow:scroll都已删除,因为我们不再需要它。
你会注意到,如果你尝试使用仅 fr 单位或在 chart-container 上使用任何 flex 布局来进行网格布局,则图表不会正确地调整大小。不幸的是,Recharts 的 ResponsiveContainer 与 Grid 或 Flexbox 不兼容(该项目的主要贡献者正在逐渐减少支持 - 截至目前为止已经3个月没有提交了)。

0
请将以下内容添加到您的styles.css文件中:
html, body, #root {
  width: 100%;
  height: 100%;
}

否则,即使将其宽度和高度设置为100%,.chart-container也不会占据整个视口。然后在SampleChart.jsx中更改此内容:
<ResponsiveContainer width="100%" height={400}>

转换为:

<ResponsiveContainer width="100%" height="100%">

让你的ResponsiveContainer的高度...响应式。


这并没有帮助。当我关闭然后重新打开图例时,图例组件会被推出视图区域,而图表不会缩回到原始较小的大小。 - Lucas D
抱歉,我误解了问题。请查看我的第二个答案! - Parabolord

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接