首页 > 解决方案 > 为什么我的 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;

标签: javascriptreactjsgridmaterial-uichart.js

解决方案


推荐阅读