Skip to content

Commit 6a9af3a

Browse files
committed
Added ChartBar and Chart component
1 parent 73dc8ad commit 6a9af3a

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.chart {
2+
padding: 1rem;
3+
border-radius: 12px;
4+
background-color: #f8dfff;
5+
text-align: center;
6+
display: flex;
7+
justify-content: space-around;
8+
height: 10rem;
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ChartBar from './ChartBar'
2+
import './Chart.css'
3+
4+
const Chart = (props) => {
5+
const dataPointValues = props.dataPoints.map(datapoint => datapoint.value);
6+
const totalMaximum = Math.max(...dataPointValues);
7+
8+
return (
9+
<div className='chart'>
10+
{props.dataPoints.map((datapoint) => (
11+
<ChartBar
12+
key={datapoint.label}
13+
value={datapoint.value}
14+
maxValue={totalMaximum}
15+
label={datapoint.label}
16+
/>
17+
))}
18+
</div>
19+
)
20+
}
21+
22+
export default Chart
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.chart-bar {
2+
height: 100%;
3+
display: flex;
4+
flex-direction: column;
5+
align-items: center;
6+
}
7+
8+
.chart-bar__inner {
9+
height: 100%;
10+
width: 100%;
11+
border: 1px solid #313131;
12+
border-radius: 12px;
13+
background-color: #c3b4f3;
14+
overflow: hidden;
15+
display: flex;
16+
flex-direction: column;
17+
justify-content: flex-end;
18+
}
19+
20+
.chart-bar__fill {
21+
background-color: #4826b9;
22+
width: 100%;
23+
transition: all 0.3s ease-out;
24+
}
25+
26+
.chart-bar__label {
27+
font-weight: bold;
28+
font-size: 0.5rem;
29+
text-align: center;
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import './ChartBar.css'
2+
3+
const ChartBar = (props) => {
4+
let barFillHeight = '0%';
5+
if(props.maxValue > 0) {
6+
barFillHeight = Math.round((props.value / props.maxValue) * 100) + '%';
7+
}
8+
9+
return (
10+
<div className='chart-bar'>
11+
<div className='chart-bar__inner'>
12+
<div
13+
className='chart-bar__fill'
14+
style={{ height: barFillHeight }}
15+
></div>
16+
</div>
17+
<div className='chart-bar__label'>{props.label}</div>
18+
</div>
19+
)
20+
}
21+
22+
export default ChartBar

Sections/Section 3/01-starting-setup/src/components/Expenses/Expenses.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import './Expenses.css'
22
import ExpensesList from './ExpensesList'
33
import Card from '../UI/Card'
44
import ExpensesFilter from './ExpensesFilter'
5+
import ExpensesChart from './ExpensesChart'
56
import { useState } from 'react'
67

78
const Expenses = (props) => {
@@ -19,6 +20,7 @@ const Expenses = (props) => {
1920
<div>
2021
<Card className='expenses'>
2122
<ExpensesFilter selected={filteredYear} onChangeFilter={filterChangeHandler}/>
23+
<ExpensesChart expenses={filteredExpenses}/>
2224
<ExpensesList items = {filteredExpenses}/>
2325
</Card>
2426
</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Chart from '../Chart/Chart'
2+
const ExpensesChart = props => {
3+
const chartDataPoints = [
4+
{ label: 'Jan', value: 0 },
5+
{ label: 'Feb', value: 0 },
6+
{ label: 'Mar', value: 0 },
7+
{ label: 'Apr', value: 0 },
8+
{ label: 'May', value: 0 },
9+
{ label: 'Jun', value: 0 },
10+
{ label: 'Jul', value: 0 },
11+
{ label: 'Aug', value: 0 },
12+
{ label: 'Sep', value: 0 },
13+
{ label: 'Oct', value: 0 },
14+
{ label: 'Nov', value: 0 },
15+
{ label: 'Dec', value: 0 },
16+
];
17+
18+
for(const expense of props.expenses) {
19+
const expenseMonth = expense.date.getMonth();
20+
chartDataPoints[expenseMonth].value += expense.amount;
21+
}
22+
23+
return <Chart dataPoints={chartDataPoints}/>
24+
}
25+
26+
export default ExpensesChart

0 commit comments

Comments
 (0)