使用recharts设置响应图表的高度和宽度(条形图)

Shr*_* BK 8 javascript css reactjs material-ui recharts

我正在尝试使用recharts来实现BarChart.但是width={600}并且height={300}导致条形图是绝对的,没有响应.如何使条形图成为响应式的?我尝试使用百分比,width={'50%"} height={"40%"}但没有工作.请在这件事上给予我帮助 !!谢谢 !!

import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend } from 'recharts';


<BarChart className="barChart" width={600} height={300} data={data}
          margin={{top: 5, right: 30, left: 20, bottom: 5}} label="heaf">
      <CartesianGrid strokeDasharray="3 3"/>
      <XAxis dataKey="name"/>
      <YAxis/>
      <Tooltip/>
      <Legend />
      <Bar dataKey="occupied" barSize={10} fill="#05386b" />
      <Bar dataKey="available" barSize={10} fill="#fdaa00" />
      <Bar dataKey="cleaning" barSize={10} fill="#379583" />
      <Bar dataKey="reserved" barSize={10} fill="#c60505" />
</BarChart>
Run Code Online (Sandbox Code Playgroud)

Mur*_*ati 7

你可以使用ResponsiveContainer提供的recharts.

文件ResponsiveContainer说:

一个容器组件,用于使图表适应父容器的大小.其中一个道具宽度和高度应为百分比字符串.

这是来自jsfiddle的代码.尝试调整输出窗口宽度的大小:

import React from "react";
import ReactDOM from "react-dom";
import {
  BarChart,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend,
  ResponsiveContainer
} from "recharts";

import "./styles.css";

const data = [
  { name: "Page A", uv: 4000, pv: 2400, amt: 2400 },
  { name: "Page B", uv: 3000, pv: 1398, amt: 2210 },
  { name: "Page C", uv: 2000, pv: 9800, amt: 2290 },
  { name: "Page D", uv: 2780, pv: 3908, amt: 2000 },
  { name: "Page E", uv: 1890, pv: 4800, amt: 2181 },
  { name: "Page F", uv: 2390, pv: 3800, amt: 2500 },
  { name: "Page G", uv: 3490, pv: 4300, amt: 2100 }
];

const SimpleAreaChart = () => {
  return (
    <ResponsiveContainer>
      <BarChart data={data} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}>
        <CartesianGrid strokeDasharray="3 3" />
        <XAxis dataKey="name" />
        <YAxis />
        <Tooltip />
        <Legend />
        <Bar dataKey="pv" fill="#8884d8" />
        <Bar dataKey="uv" fill="#82ca9d" />
      </BarChart>
    </ResponsiveContainer>
  );
};

const rootElement = document.getElementById("container");
ReactDOM.render(<SimpleAreaChart />, rootElement);
Run Code Online (Sandbox Code Playgroud)

这是css container:

#container {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 10px;
  width: 100%;
  height: 400px;
  background-color: #fff;
}
Run Code Online (Sandbox Code Playgroud)


Mr *_*nce 6

如果您希望两个维度都缩放,只需使用aspect属性

<ResponsiveContainer aspect={1.2}>
   // chart with width = 1.2 height
</ResponsiveContainer>

<ResponsiveContainer aspect={0.8}>
   // chart with width = 0.8 height
</ResponsiveContainer>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,高度/宽度将为父容器的 100%。

  • 谢谢——我一直在寻找这种方法!我见过的每个示例都需要显式设置高度和/或宽度(如果不是“&lt;ResponsiveContainer&gt;”,则为父级 div),但“aspect”让我只需将图表调整到其父级的大小,无论无论发生什么其他事情。 (2认同)

Sau*_*oab 5

您需要使用ResponsiveContainer它才能工作。另外,请使用不带方括号的百分比值。

<ResponsiveContainer width="95%" height={400}>
   // your chart
</ResponsiveContainer>
Run Code Online (Sandbox Code Playgroud)

我用了,效果很好!:)

来源:响应式容器文档

  • 我试图将高度作为百分比,但它不起作用。你也一样吗? (3认同)

vij*_*yst 0

用 div 包裹条形图。

<div style={{ position: 'relative', width: '50%', paddingTop: '40%' }}>
  <div style={{ position: 'absolute', height: '100%' }}>
    <BarChart />
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

对于外部 div:50% 的宽度是相对于容器宽度的。paddingTop 是容器宽度的 40%。

对于内部 div:高度为 100% 将占据其整个容器,其高度仅为其 paddingTop。

  • 我在我的反应应用程序中尝试了几个变体,但无法渲染条形图。 (2认同)