Generate a SPA for the analytics dashboard with hardcoded values The page should be…
- User
Generate a SPA for the analytics dashboard with hardcoded values The page should be aesthetically very attractive and professional and comprehensive
- Assistant
Generate a SPA for the analytics dashboard with hardcoded values The page should be aesthetically very attractive and professional and comprehensive
Creating a Single Page Application (SPA) for an analytics dashboard involves using a modern JavaScript framework such as React, along with UI libraries for styling and charting libraries for data visualization. Below, I'll guide you through building a professional and aesthetically pleasing analytics dashboard using React, Material-UI for UI components, and Recharts for charts. We'll use hardcoded data to populate the dashboard.
Create a new React application using create-react-app:
npx create-react-app analytics-dashboard
cd analytics-dashboard
We'll use Material-UI for UI components and Recharts for data visualization.
npm install @mui/material @emotion/react @emotion/styled @mui/icons-material recharts
Organize your project as follows:
analytics-dashboard/
├── public/
├── src/
│ ├── components/
│ │ ├── Header.js
│ │ ├── Sidebar.js
│ │ ├── Dashboard.js
│ │ ├── AnalyticsCard.js
│ │ └── Charts/
│ │ ├── LineChartComponent.js
│ │ ├── PieChartComponent.js
│ │ └── BarChartComponent.js
│ ├── App.js
│ ├── App.css
│ └── index.js
├── package.json
└── README.md
src/components/Header.js
import React from 'react';
import { AppBar, Toolbar, Typography, IconButton, Avatar } from '@mui/material';
import NotificationsIcon from '@mui/icons-material/Notifications';
import SettingsIcon from '@mui/icons-material/Settings';
const Header = () => {
return (
<AppBar position="static" color="primary" elevation={1}>
<Toolbar>
<Typography variant="h6" style={{ flexGrow: 1 }}>
Analytics Dashboard
</Typography>
<IconButton color="inherit">
<NotificationsIcon />
</IconButton>
<IconButton color="inherit">
<SettingsIcon />
</IconButton>
<Avatar alt="User" src="https://i.pravatar.cc/300" style={{ marginLeft: '10px' }} />
</Toolbar>
</AppBar>
);
};
export default Header;
src/components/Sidebar.js
import React from 'react';
import { Drawer, List, ListItem, ListItemIcon, ListItemText, Toolbar } from '@mui/material';
import DashboardIcon from '@mui/icons-material/Dashboard';
import AssessmentIcon from '@mui/icons-material/Assessment';
import PeopleIcon from '@mui/icons-material/People';
import SettingsApplicationsIcon from '@mui/icons-material/SettingsApplications';
const drawerWidth = 240;
const Sidebar = () => {
const menuItems = [
{ text: 'Dashboard', icon: <DashboardIcon /> },
{ text: 'Reports', icon: <AssessmentIcon /> },
{ text: 'Users', icon: <PeopleIcon /> },
{ text: 'Settings', icon: <SettingsApplicationsIcon /> },
];
return (
<Drawer
variant="permanent"
sx={{
width: drawerWidth,
flexShrink: 0,
'& .MuiDrawer-paper': { width: drawerWidth, boxSizing: 'border-box' },
}}
>
<Toolbar />
<List>
{menuItems.map((item, index) => (
<ListItem button key={index}>
<ListItemIcon>{item.icon}</ListItemIcon>
<ListItemText primary={item.text} />
</ListItem>
))}
</List>
</Drawer>
);
};
export default Sidebar;
src/components/AnalyticsCard.js
import React from 'react';
import { Card, CardContent, Typography } from '@mui/material';
import { Box } from '@mui/system';
import Trend from 'react-trend';
const AnalyticsCard = ({ title, value, trend, trendUp }) => {
return (
<Card variant="outlined" sx={{ minWidth: 200 }}>
<CardContent>
<Typography variant="h6" color="text.secondary" gutterBottom>
{title}
</Typography>
<Typography variant="h4" component="div">
{value}
</Typography>
<Box display="flex" alignItems="center">
<Trend
data={trend}
smooth
autoDraw
autoDrawDuration={3000}
autoDrawEasing="ease-in"
stroke={trendUp ? '#4caf50' : '#f44336'}
strokeWidth={3}
strokeLinecap={'round'}
/>
<Typography variant="body2" color={trendUp ? 'green' : 'red'} sx={{ ml: 1 }}>
{trendUp ? '↑' : '↓'} {Math.abs(trend[trend.length - 1] - trend[0]).toFixed(2)}%
</Typography>
</Box>
</CardContent>
</Card>
);
};
export default AnalyticsCard;
Note: You'll need to install react-trend for the trend lines:
npm install react-trend
a. LineChartComponent
src/components/Charts/LineChartComponent.js
import React from 'react';
import { LineChart, Line, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid } from 'recharts';
import { Card, CardContent, Typography } from '@mui/material';
const data = [
{ name: 'Jan', Sales: 4000 },
{ name: 'Feb', Sales: 3000 },
{ name: 'Mar', Sales: 5000 },
{ name: 'Apr', Sales: 4000 },
{ name: 'May', Sales: 6000 },
{ name: 'Jun', Sales: 7000 },
];
const LineChartComponent = () => {
return (
<Card variant="outlined" sx={{ mt: 2 }}>
<CardContent>
<Typography variant="h6" gutterBottom>
Monthly Sales
</Typography>
<ResponsiveContainer width="100%" height={300}>
<LineChart data={data}>
<XAxis dataKey="name" />
<YAxis />
<Tooltip />
<CartesianGrid stroke="#eee" strokeDasharray="5 5" />
<Line type="monotone" dataKey="Sales" stroke="#3f51b5" />
</LineChart>
</ResponsiveContainer>
</CardContent>
</Card>
);
};
export default LineChartComponent;
b. PieChartComponent
src/components/Charts/PieChartComponent.js
import React from 'react';
import { PieChart, Pie, Cell, Tooltip, ResponsiveContainer, Legend } from 'recharts';
import { Card, CardContent, Typography } from '@mui/material';
const data = [
{ name: 'Direct', value: 400 },
{ name: 'Referral', value: 300 },
{ name: 'Organic', value: 300 },
];
const COLORS = ['#0088FE', '#00C49F', '#FFBB28'];
const PieChartComponent = () => {
return (
<Card variant="outlined" sx={{ mt: 2 }}>
<CardContent>
<Typography variant="h6" gutterBottom>
Traffic Sources
</Typography>
<ResponsiveContainer width="100%" height={300}>
<PieChart>
<Pie
data={data}
dataKey="value"
nameKey="name"
cx="50%"
cy="50%"
outerRadius={100}
fill="#8884d8"
label
>
{data.map((entry, index) => (
<Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
))}
</Pie>
<Tooltip />
<Legend verticalAlign="bottom" height={36}/>
</PieChart>
</ResponsiveContainer>
</CardContent>
</Card>
);
};
export default PieChartComponent;
c. BarChartComponent
src/components/Charts/BarChartComponent.js
import React from 'react';
import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid } from 'recharts';
import { Card, CardContent, Typography } from '@mui/material';
const data = [
{ name: 'Q1', Revenue: 4000 },
{ name: 'Q2', Revenue: 3000 },
{ name: 'Q3', Revenue: 5000 },
{ name: 'Q4', Revenue: 7000 },
];
const BarChartComponent = () => {
return (
<Card variant="outlined" sx={{ mt: 2 }}>
<CardContent>
<Typography variant="h6" gutterBottom>
Quarterly Revenue
</Typography>
<ResponsiveContainer width="100%" height={300}>
<BarChart data={data}>
<XAxis dataKey="name" />
<YAxis />
<Tooltip />
<CartesianGrid stroke="#eee" strokeDasharray="5 5" />
<Bar dataKey="Revenue" fill="#82ca9d" />
</BarChart>
</ResponsiveContainer>
</CardContent>
</Card>
);
};
export default BarChartComponent;
src/components/Dashboard.js
import React from 'react';
import { Grid, Container, Typography } from '@mui/material';
import AnalyticsCard from './AnalyticsCard';
import LineChartComponent from './Charts/LineChartComponent';
import PieChartComponent from './Charts/PieChartComponent';
import BarChartComponent from './Charts/BarChartComponent';
const Dashboard = () => {
// Hardcoded trend data
const salesTrend = [1, 2, 3, 4, 5, 6, 7, 8];
const usersTrend = [2, 3, 5, 7, 11, 13, 17, 19];
return (
<Container maxWidth="lg" sx={{ mt: 4, mb: 4 }}>
<Typography variant="h4" gutterBottom>
Overview
</Typography>
<Grid container spacing={3}>
{/* Analytics Cards */}
<Grid item xs={12} sm={6} md={3}>
<AnalyticsCard
title="Total Sales"
value="$25,000"
trend={salesTrend}
trendUp={true}
/>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<AnalyticsCard
title="New Users"
value="1,200"
trend={usersTrend}
trendUp={false}
/>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<AnalyticsCard
title="Active Sessions"
value="3,400"
trend={[1, 3, 2, 5, 4, 7, 6, 8]}
trendUp={true}
/>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<AnalyticsCard
title="Conversion Rate"
value="4.5%"
trend={[2, 2, 3, 3, 4, 4, 5, 5]}
trendUp={true}
/>
</Grid>
</Grid>
{/* Charts */}
<Grid container spacing={3} sx={{ mt: 2 }}>
<Grid item xs={12} md={6}>
<LineChartComponent />
</Grid>
<Grid item xs={12} md={6}>
<PieChartComponent />
</Grid>
<Grid item xs={12}>
<BarChartComponent />
</Grid>
</Grid>
</Container>
);
};
export default Dashboard;
You can customize the styling further in App.css, but since we're using Material-UI, much of the styling is handled through its theming and component properties. If you wish to add custom styles, you can modify App.css accordingly.
src/App.css
body {
margin: 0;
font-family: 'Roboto', sans-serif;
}
src/App.js
import React from 'react';
import { CssBaseline, Box, Toolbar } from '@mui/material';
import Header from './components/Header';
import Sidebar from './components/Sidebar';
import Dashboard from './components/Dashboard';
function App() {
return (
<Box sx={{ display: 'flex' }}>
<CssBaseline />
<Header />
<Sidebar />
<Box
component="main"
sx={{ flexGrow: 1, bgcolor: 'background.default', p: 3 }}
>
<Toolbar />
<Dashboard />
</Box>
</Box>
);
}
export default App;
After setting up all the components:
Start the development server:
npm start
View the dashboard by navigating to http://localhost:3000 in your web browser.
For convenience, here's the full code listing of all the components mentioned above. Ensure each component is placed in its respective file as per the project structure.
1. src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
2. src/App.js
(As provided above.)
3. src/App.css
(As provided above.)
4. src/components/Header.js
(As provided above.)
5. src/components/Sidebar.js
(As provided above.)
6. src/components/Dashboard.js
(As provided above.)
7. src/components/AnalyticsCard.js
(As provided above.)
8. src/components/Charts/LineChartComponent.js
(As provided above.)
9. src/components/Charts/PieChartComponent.js
(As provided above.)
10. src/components/Charts/BarChartComponent.js
(As provided above.)
To make the dashboard even more comprehensive and professional, consider the following enhancements:
Responsive Design: Ensure the dashboard looks good on all screen sizes. Material-UI's Grid system helps, but testing on various devices is essential.
Theme Customization: Customize Material-UI's theme to match your brand colors.
Interactive Elements: Add interactivity such as filters, date pickers, or hover effects on charts.
Icons and Images: Use high-quality icons and images to enhance visual appeal.
Performance Optimization: Lazy load components and optimize chart rendering for better performance.
State Management: For larger projects, integrate state management libraries like Redux or Context API.
Routing: Use react-router for navigation if you plan to expand the dashboard with multiple pages.
Real Data Integration: Replace hardcoded data with real API data endpoints for dynamic content.
This setup provides a solid foundation for an analytics dashboard SPA with hardcoded values. You can further expand upon this by integrating dynamic data sources, adding more detailed analytics, and customizing the UI/UX to fit specific requirements.
Not yet included in a published release.