javascript - 为什么我的 React material-ui Grid 没有占据整个屏幕高度?
问题描述
我为导航构建了一个 material-ui 抽屉组件,并使用 material-ui 网格将我的 chart.js 图形放置在其中。然而,即使我在 Grid.js 中指定了 100% 的高度,图形/网格也不会占据整个高度,底部仍然有空白。
我希望带有图表的网格占据屏幕的整个高度并且不留空白。
下图:
代码:
网格.js
import React from 'react';
import { makeStyles } from "@material-ui/core/styles";
import { Grid, Paper } from "@material-ui/core";
import Chart3 from '../Components/Chart/Chart3';
import ChartStream from '../Components/Chart/ChartStream';
import Table from '../Components/Table/Table';
const useStyles = makeStyles((theme) => ({
grid: {
height: '100%',
width: '100%',
margin: '0px'
},
paper: {
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.seconday,
background: theme.palette.success.light,
}
}));
function Grido() {
const classes = useStyles();
return (
<Grid container spacing={2} className={classes.grid} >
<Grid item xs={12} md={12}>
<Chart3 />
</Grid>
</Grid>
);
}
export default Grido;
App2.js
import React from 'react';
import { makeStyles } from "@material-ui/core/styles"
import './App.css'
import {
BrowserRouter as Router,
Switch, Route, Link
} from "react-router-dom";
import {
Drawer, List, ListItem,
ListItemIcon, ListItemText,
Container, Typography,
} from "@material-ui/core";
import HomeIcon from "@material-ui/icons/Home";
import InfoIcon from '@material-ui/icons/Info';
import Grido from './Grid/Grid';
import Footer from './Components/Footer/Footer';
const useStyles = makeStyles((theme) => ({
drawerPaper: { width: 'inherit', height: '100%', background: '#F7F5FB' },
link: {
textDecoration: 'none',
color: theme.palette.text.primary
}
}))
const styles = {
paper: {
background: "blue"
}
}
function App2() {
const classes = useStyles();
return (
<Router>
<div style={{ display: 'flex' }}>
<Drawer
style={{ width: '220px' }}
classes={{ paper: classes.paper }}
variant="persistent"
anchor="left"
open={true}
classes={{ paper: classes.drawerPaper }}
>
<List>
<Link to="/" className={classes.link}>
<ListItem button>
<ListItemIcon>
<HomeIcon />
</ListItemIcon>
<ListItemText primary={"Home"} />
</ListItem>
</Link>
<Link to="/about" className={classes.link}>
<ListItem button>
<ListItemIcon>
<InfoIcon />
</ListItemIcon>
<ListItemText primary={"About"} />
</ListItem>
</Link>
</List>
</Drawer>
<Switch>
<Route exact path="/">
<Container>
<Grido />
</Container>
</Route>
<Route exact path="/about">
<Container>
<Typography variant="h3" gutterBottom>
About
</Typography>
<Typography variant="body1" gutterBottom>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
</Typography>
</Container>
</Route>
</Switch>
</div>
</Router>
);
}
export default App2;
chart3.js
import React, { useState, useEffect } from "react";
import { Line } from "react-chartjs-2";
import axios from "axios";
import './Chart.css';
const Chart3 = () => {
//Setting states for later use
const [chartData, setChartData] = useState({});
const [Forecast, setForecast] = useState([]);
const [Actual, setActual] = useState([]);
const chart = () => {
//Setting up variables to store data
let Fore = [];
let Act = [];
//Using Axios to get the data from api
axios
.get('https://api.carbonintensity.org.uk/intensity/2020-09-01T15:30Z/2020-09-10T17:00Z')
.then(res => {
console.log(res);
for (const dataObj of res.data.data) {
Fore.push(parseInt(dataObj.intensity.forecast));
Act.push(parseInt(dataObj.intensity.actual));
}
//Now setting chart data now we have the data from API and parsed it
setChartData({
labels: Fore,
datasets: [
{
label: "Carbon Intensity Levels",
data: Act,
backgroundColor: "#F58A07",
borderWidth: 4
}
]
});
})
//Catching an error for when recieving API data
.catch(err => {
console.log(err);
});
console.log(Fore, Act);
};
useEffect(() => {
chart();
}, []);
return (
<div className="App">
<h1></h1>
<div className="chart">
<Line
data={chartData}
options={{
responsive: true,
title: { text: "2020-09-01T15:30Z - 2020-09-10T17:00Z", display: true },
scales: {
yAxes: [
{
ticks: {
autoSkip: true,
maxTicksLimit: 100,
beginAtZero: true
},
gridLines: {
display: false
},
scaleLabel: {
display: true,
labelString: "Actual"
}
}
],
xAxes: [
{
gridLines: {
display: false
},
scaleLabel: {
display: true,
labelString: "Forecast"
}
}
]
}
}}
/>
</div>
</div>
);
};
export default Chart3;
解决方案
推荐阅读
- vega - 使用 vl2png 时如何应用 vega 主题
- r - ggplot missing plot with x-axis factor
- java - 我想要这个..地图缩放我的多个标记,在一帧中。现在我的地图缩放显示国家的中心位置
- html - 如何在同一行上水平居中 2 个或多个 div
- retina-display - 硬件问题:MacBook 使用的 4k 外接显示器上的小字体?
- python - 如何拒绝 http 请求(抛出一些代码错误)
- c++ - 在函数中分配内存的问题
- r - 尝试更改我的 ggplot 中的中断会删除 y 轴
- python - 进行算术运算时如何保持两个python字典之间的匹配顺序
- css - 背景图像路径 css