Public
Edited
Nov 21, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
result = calculateEmissions()
Insert cell
function calculateEmissions() {
const CPImultiplier = .8414
const adjustedIncome = +user.income * CPImultiplier
const calc = new Calculator(adjustedIncome, user.state)

const categories = calc.getTotalGoodsEmissions()
const fuelEmissions = calc.getFuelEmissions()
const electricEmissions = calc.getElectricEmissions()
const total = d3.sum(categories, d => d.emissions) + d3.sum(fuelEmissions, d => d.emissions) + electricEmissions.emissions

const result = [...categories, ...fuelEmissions, electricEmissions]
result.total = total
result.percentile = Math.ceil(percentile(households.map(d => d.Total_CO2), total))
return result
}
Insert cell
class Calculator {
constructor(income, state) {
this.income = income
this.state = state
}
getElectricEmissions = () => {
const x = electricMultipliers.get(this.state) * this.income * (electricity / 100)
return {category: "electricity", emissions: x}
}

getFuelEmissions = () => {
return Object.keys(userFuel).map(key => {
const dollarsSpent = this.income * (userFuel[key] / 100)
const f = directMultipliers[key]
const emissions = (f.supplyChain * dollarsSpent) + (f.value * dollarsSpent)
return {category: key, emissions: emissions}
})
}

getTotalGoodsEmissions = () => Object.keys(form)
.map(key => ( {category: key, emissions: (form[key] / 100) * this.income * this.getMultiplier(key) } ) )

getMultiplier(key) {
return goodsMultipliers.has(key) ? goodsMultipliers.get(key) : 0
}


}
Insert cell
Insert cell
Insert cell
Insert cell
defaults = [.0172, .0461, .0303, .1957, .0646, .0213, 0.2553, .0142, .0395, .0273, .0083, 0.1276, .05]
Insert cell
directMultipliers = {return {"Fuel Oil": {supplyChain:0.003735, value: 0.003273}, "Gasoline": {supplyChain: 0.003832, value: 0.003343}, "Natural gas": {supplyChain: 0.005367, value: 0.005078}, "Other fuels": {supplyChain: 0.003497, value: 0.002983}}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{const data = await FileAttachment("state_electric_multipliers.csv").csv()
return data.map(d => ({state: d.state, multiplier: +d["multiplier (t CO2e / $)"] }))

}
Insert cell
await FileAttachment("consumption_multipliers.csv").csv()
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more