Created
February 16, 2019 05:09
-
-
Save dustincjensen/41b011b657fd0c5c178611c9fe8223f1 to your computer and use it in GitHub Desktop.
Academind - #22 Creating Charts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { Bar as BarChart } from "react-chartjs"; | |
const BOOKINGS_BUCKETS = { | |
Cheap: { | |
min: 0, | |
max: 100 | |
}, | |
Normal: { | |
min: 100, | |
max: 200 | |
}, | |
Expensive: { | |
min: 200, | |
max: 100000000 | |
} | |
}; | |
const bookingsChart = props => { | |
const chartData = { | |
labels: Object.keys(BOOKINGS_BUCKETS), | |
datasets: [ | |
{ | |
fillColor: "rgba(220,220,220,0.5)", | |
strokeColor: "rgba(220,220,220,0.8)", | |
highlightFill: "rgba(220,220,220,0.75)", | |
highlightStroke: "rgba(220,220,220,1)", | |
data: [] | |
} | |
] | |
}; | |
for (const bucket in BOOKINGS_BUCKETS) { | |
const filteredBookingsCount = props.bookings.reduce((prev, current) => { | |
if ( | |
current.event.price >= BOOKINGS_BUCKETS[bucket].min && | |
current.event.price < BOOKINGS_BUCKETS[bucket].max | |
) { | |
return prev + 1; | |
} else { | |
return prev; | |
} | |
}, 0); | |
chartData.datasets[0].data.push(filteredBookingsCount); | |
} | |
console.log(chartData); | |
return ( | |
<div style={{ textAlign: "center" }}> | |
<BarChart data={chartData} /> | |
</div> | |
); | |
}; | |
export default bookingsChart; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment