github.com/tuotoo/go-ethereum@v1.7.4-0.20171121184211-049797d40a24/dashboard/assets/components/Home.jsx (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import React, {Component} from 'react';
    18  import PropTypes from 'prop-types';
    19  import Grid from 'material-ui/Grid';
    20  import {LineChart, AreaChart, Area, YAxis, CartesianGrid, Line, ResponsiveContainer} from 'recharts';
    21  import {withTheme} from 'material-ui/styles';
    22  
    23  import {isNullOrUndefined, DATA_KEYS} from "./Common.jsx";
    24  
    25  // ChartGrid renders a grid container for responsive charts.
    26  // The children are Recharts components extended with the Material-UI's xs property.
    27  class ChartGrid extends Component {
    28      render() {
    29          return (
    30              <Grid container spacing={this.props.spacing}>
    31                  {
    32                      React.Children.map(this.props.children, child => (
    33                          <Grid item xs={child.props.xs}>
    34                              <ResponsiveContainer width="100%" height={child.props.height}>
    35                                  {React.cloneElement(child, {data: child.props.values.map(value => ({value: value}))})}
    36                              </ResponsiveContainer>
    37                          </Grid>
    38                      ))
    39                  }
    40              </Grid>
    41          );
    42      }
    43  }
    44  
    45  ChartGrid.propTypes = {
    46      spacing: PropTypes.number.isRequired,
    47  };
    48  
    49  // Home renders the home component.
    50  class Home extends Component {
    51      shouldComponentUpdate(nextProps) {
    52          return !isNullOrUndefined(nextProps.shouldUpdate[DATA_KEYS.memory]) ||
    53              !isNullOrUndefined(nextProps.shouldUpdate[DATA_KEYS.traffic]);
    54      }
    55  
    56      render() {
    57          const {theme} = this.props;
    58          const memoryColor = theme.palette.primary[300];
    59          const trafficColor = theme.palette.secondary[300];
    60  
    61          return (
    62              <ChartGrid spacing={24}>
    63                  <AreaChart xs={6} height={300} values={this.props.memory}>
    64                      <YAxis />
    65                      <Area type="monotone" dataKey="value" stroke={memoryColor} fill={memoryColor} />
    66                  </AreaChart>
    67                  <LineChart xs={6} height={300} values={this.props.traffic}>
    68                      <Line type="monotone" dataKey="value" stroke={trafficColor} dot={false} />
    69                  </LineChart>
    70                  <LineChart xs={6} height={300} values={this.props.memory}>
    71                      <YAxis />
    72                      <CartesianGrid stroke="#eee" strokeDasharray="5 5" />
    73                      <Line type="monotone" dataKey="value" stroke={memoryColor} dot={false} />
    74                  </LineChart>
    75                  <AreaChart xs={6} height={300} values={this.props.traffic}>
    76                      <CartesianGrid stroke="#eee" strokeDasharray="5 5" vertical={false} />
    77                      <Area type="monotone" dataKey="value" stroke={trafficColor} fill={trafficColor} />
    78                  </AreaChart>
    79              </ChartGrid>
    80          );
    81      }
    82  }
    83  
    84  Home.propTypes = {
    85      theme:        PropTypes.object.isRequired,
    86      shouldUpdate: PropTypes.object.isRequired,
    87  };
    88  
    89  export default withTheme()(Home);