Monthly CO2 emissions per transport are not displayed in my bar chart #720
-
Hello everyone, Many thanks in advance! PS: The three buttons do nothing yet. This is normal. Here my PR: |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
The method const totalEmissionsPerTransport = (transport, fuel, month) =>
Math.floor(
entries
.filter(
(entry) =>
entry.transport === transport &&
entry.fuel === fuel &&
new Date(entry.date).getMonth() === month
)
.reduce((acc, curr) => acc + curr.result, 0)
);
const months = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
]; |
Beta Was this translation helpful? Give feedback.
-
Thank you, Ernst, for your answer, but if I do this, I don't get any bar displayed and I get numbers instead of months in my axis. |
Beta Was this translation helpful? Give feedback.
-
Okay, I see. Forget about my first answer, we'll keep the month array as is. In total, you'll need to change several other things. First, adjust the function const totalEmissionsPerTransport = (transport, fuel, month) =>
Math.floor(
entries
.filter(
(entry) => {
const hasFuel = fuel ? true : false;
const givenTransport = entry.transport === transport;
const givenFuel = entry.fuel === fuel;
const givenMonth = new Date(entry.date).getMonth() === month;
return hasFuel ? (givenFuel && givenTransport && givenMonth) : givenTransport && givenMonth;
}
)
.reduce((acc, curr) => acc + curr.result, 0)
); Then, you will need to map over the months array and using the const carPetrol = months.map((_, monthIndex) => totalEmissionsPerTransport("car", "petrol", monthIndex));
const carDiesel = months.map((_, monthIndex) => totalEmissionsPerTransport("car", "diesel", monthIndex));
const carHybrid = months.map((_, monthIndex) => totalEmissionsPerTransport("car", "hybrid", monthIndex));
const carElectricStrommix = months.map((_, monthIndex) => totalEmissionsPerTransport("car", "electric-strommix", monthIndex));
const carElectricRenewable = months.map((_, monthIndex) => totalEmissionsPerTransport("car", "electric-renewable", monthIndex));
const train = months.map((_, monthIndex) => totalEmissionsPerTransport("train", null, monthIndex));
const plane = months.map((_, monthIndex) => totalEmissionsPerTransport("plane", null, monthIndex));
const bicycle = months.map((_, monthIndex) => totalEmissionsPerTransport("bycicle", null, monthIndex)); The const data = {
labels: months,
datasets: [
{
label: "Car petrol",
data: carPetrol,
backgroundColor: "violet",
},
{
label: "Car diesel",
data: carDiesel,
backgroundColor: "orange",
},
{
label: "Car hybrid",
data: carHybrid,
backgroundColor: "yellow",
},
{
label: "Car Electric-Strommix",
data: carElectricStrommix,
backgroundColor: "pink",
},
{
label: "Car Electric-Renewable",
data: carElectricRenewable,
backgroundColor: "turquoise",
},
{
label: "Train",
data: train,
backgroundColor: "lightgreen",
},
{
label: "Plane",
data: plane,
backgroundColor: "coral",
},
{
label: "Bicycle",
data: bicycle,
backgroundColor: "lightblue",
},
],
}; Finally, your <Bar data={data} options={config.options} /> |
Beta Was this translation helpful? Give feedback.
-
Hi Ernst, thanks a lot for your help! I have implemented your corrections and it works! |
Beta Was this translation helpful? Give feedback.
Okay, I see. Forget about my first answer, we'll keep the month array as is.
In total, you'll need to change several other things. First, adjust the function
totalEmissionsPerTransport
to consider that thefuel
might be missing as is the case for transports like the plane.