Rechart 响应式容器不呈现

Pab*_*ada 12 javascript css reactjs

我正在尝试使用 reChart 渲染一个条形图以使其容器 100% 适合:

http://recharts.org/#/en-US/api/ResponsiveContainer

 <div className="question">
    <div className="question-container">
        <ResponsiveContainer width="100%" height="100%">
            <BarChart   layout="vertical" data={rows}
                      margin={{top: 5, right: 30, left: 20, bottom: 5}}>

                <YAxis axisLine={false} tickLine={false} width={400} dataKey="name" type="category">
                </YAxis>
                <Bar dataKey="valuePre" fill="#00a0dc" label={<Text scaleToFit={true} verticalAnchor="middle" />}/>
                <Bar dataKey="valuePost" fill="#c7c7c7"  label={<Text verticalAnchor="middle" />}/>

            </BarChart>
        </ResponsiveContainer>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

当我添加 ResponsiveContainer 组件时,数据停止呈现。而且只要我取出 ResponsiveContainer 并设置 BarChart 组件的宽度和高度,我就可以再次看到条形图。

谁能帮我找出我做错了什么?

这是一个解决问题的方法:

http://jsfiddle.net/eyh80eeo/

Nou*_*non 13

我无法为容器设置高度,因此我使用了aspect,它设置了 ResponsiveContainer 的高度:

 <ResponsiveContainer width={"100%"} aspect={1}>
Run Code Online (Sandbox Code Playgroud)


Nan*_*ndi 11

由于ResponsiveContainer依赖于父级的维度,因此您需要确保父级具有正确的widthheight.

设置<Barchart />工作的原因是因为它设置了自己的widthheight

看看这个小提琴RESPONSIVECONTAINER

我在你拥有的父类中添加了 CSS

.question {
  width: 500px;
  height: 500px;
}

.question-container {
  width: 100%;
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)


Ral*_*oss 9

在 CSS 网格中使用ResponsiveContainer组件时,我遇到了类似的问题。

我只是添加width={'99%'}ResponsiveContainer并正确调整了大小。

<ResponsiveContainer width={'99%'} height={300}>

参考:Recharts 响应式图表在 CSS 网格内不响应

  • 出色的!正是我需要的:) 谢谢! (2认同)

Gih*_*ana 6

为响应式容器提供固定高度

<ResponsiveContainer width="100%" height={400}>
        <BarChart   layout="vertical" data={rows}
                  margin={{top: 5, right: 30, left: 20, bottom: 5}}>

            <YAxis axisLine={false} tickLine={false} width={400} dataKey="name" type="category">
            </YAxis>
            <Bar dataKey="valuePre" fill="#00a0dc" label={<Text scaleToFit={true} verticalAnchor="middle" />}/>
            <Bar dataKey="valuePost" fill="#c7c7c7"  label={<Text verticalAnchor="middle" />}/>

        </BarChart>
    </ResponsiveContainer>
Run Code Online (Sandbox Code Playgroud)