diff --git a/src/components/singlebar.js b/src/components/singlebar.js index 3f7ec540..42929b1f 100644 --- a/src/components/singlebar.js +++ b/src/components/singlebar.js @@ -1,80 +1,41 @@ -import React from "react"; -import { - BarChart, - Bar, - XAxis, - YAxis, - LabelList, - CartesianGrid -} from "recharts"; +import React from "react" +import { BarChart, Bar, LabelList, CartesianGrid } from "recharts" -const barColors = ["#685e4a", "#b7b7b7", "#d9d9d9", "#f3f3f3"]; +const getPct = (value, total) => { return Math.round((value/total)*100) } +const getLabel = (entry, total, field, label) => { return `${label}: ${getPct(entry[field], total)}%` } -function getTotal (payload) { - return payload[0].value + payload[1].value + payload[2].value + payload[3].value -} +export default function SingleBarChart ({emissions_data}) { + // sum all emissions fields except year + const totalEmissions = Object.entries(emissions_data[0]) + .filter(([key,_val]) => key !== 'year') + .reduce((acc, [_key,val]) => acc + val, 0) -function calcPercent (payload, index) { - return (100 * payload[index].value.toFixed(1) / getTotal(payload)).toFixed(1) -} - -// const renderLabel = (props, text) =>{ -// const { x, y, width, height, value } = props -// return ( -// {text}: {value} -// ) -// } - -const getLabel=(props, label) => {return label} - - -//Render the stacked bar. - export default function SingleBarChart({emissions_data}) { return ( - - - - getLabel(props, 'Dumps, Farms, Industrial & Other')} - position="insideTopLeft" - /> + + getLabel(entry, totalEmissions, 'dumps_farms_industrial_other', 'Dumps, Farms, Industrial & Other')} position="insideTopLeft" /> - - getLabel(props, 'Transportation')} - position="insideTopLeft" /> + + getLabel(entry, totalEmissions, 'transportation', 'Transportation')} position="insideTopLeft" /> - - getLabel(props, 'Buildings')} position="insideTopLeft" /> + + getLabel(entry, totalEmissions, 'buildings', 'Buildings')} position="insideTopLeft" /> - - getLabel(props, 'Dirty Power')} position="insideTopLeft" /> + + getLabel(entry, totalEmissions, 'dirty_power', 'Dirty Power')} position="insideTopLeft" /> - ); + ) } \ No newline at end of file diff --git a/src/pages/index.js b/src/pages/index.js index 5db2d3ef..f3f02fea 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -49,7 +49,7 @@ const IndexPage = ({data}) => { cut climate pollution by {cutPerYearPrcnt} a year.

- +

When it comes to solving the climate crisis there's one main thing @@ -57,9 +57,7 @@ const IndexPage = ({data}) => {

-
- [stacked bar graph of emissions source] -
+