electricityMixViz = {
const mixData = energyMining.filter(d =>
+d.Year >= 2015 &&
d["average_value_Electricity production from coal sources (% of total)"] !== "" &&
d["average_value_Electricity production from natural gas sources (% of total)"] !== "" &&
d["average_value_Electricity production from oil sources (% of total)"] !== "" &&
d["average_value_Electricity production from nuclear sources (% of total)"] !== "" &&
d["average_value_Electricity production from hydroelectric sources (% of total)"] !== "" &&
d["average_value_Electricity production from renewable sources, excluding hydroelectric (% of total)"] !== ""
)
const withRegion = mixData.map(d => ({
"Country Code": d["Country Code"],
"Country Name": d["Country Name"],
Year: +d.Year,
Region: countryInfo[d["Country Code"]]?.Region || "Unknown",
Coal: +d["average_value_Electricity production from coal sources (% of total)"],
NaturalGas: +d["average_value_Electricity production from natural gas sources (% of total)"],
Oil: +d["average_value_Electricity production from oil sources (% of total)"],
Nuclear: +d["average_value_Electricity production from nuclear sources (% of total)"],
Hydro: +d["average_value_Electricity production from hydroelectric sources (% of total)"],
OtherRenewable: +d["average_value_Electricity production from renewable sources, excluding hydroelectric (% of total)"]
})).filter(d => d.Region !== "Unknown")
const countryLatestYear = {}
for (const d of withRegion) {
const key = d["Country Code"]
if (!countryLatestYear[key] || +d.Year > +countryLatestYear[key].Year) {
countryLatestYear[key] = d
}
}
const byRegion = {}
for (const d of Object.values(countryLatestYear)) {
if (!byRegion[d.Region]) {
byRegion[d.Region] = {
Region: d.Region,
coal: [],
gas: [],
oil: [],
nuclear: [],
hydro: [],
renewable: []
}
}
if (!isNaN(d.Coal)) byRegion[d.Region].coal.push(d.Coal)
if (!isNaN(d.NaturalGas)) byRegion[d.Region].gas.push(d.NaturalGas)
if (!isNaN(d.Oil)) byRegion[d.Region].oil.push(d.Oil)
if (!isNaN(d.Nuclear)) byRegion[d.Region].nuclear.push(d.Nuclear)
if (!isNaN(d.Hydro)) byRegion[d.Region].hydro.push(d.Hydro)
if (!isNaN(d.OtherRenewable)) byRegion[d.Region].renewable.push(d.OtherRenewable)
}
// Calculate regional averages
const regionAverages = []
for (const region in byRegion) {
const r = byRegion[region]
const avg = {
Region: r.Region,
Coal: r.coal.length ? r.coal.reduce((sum, val) => sum + val, 0) / r.coal.length : 0,
NaturalGas: r.gas.length ? r.gas.reduce((sum, val) => sum + val, 0) / r.gas.length : 0,
Oil: r.oil.length ? r.oil.reduce((sum, val) => sum + val, 0) / r.oil.length : 0,
Nuclear: r.nuclear.length ? r.nuclear.reduce((sum, val) => sum + val, 0) / r.nuclear.length : 0,
Hydro: r.hydro.length ? r.hydro.reduce((sum, val) => sum + val, 0) / r.hydro.length : 0,
OtherRenewable: r.renewable.length ? r.renewable.reduce((sum, val) => sum + val, 0) / r.renewable.length : 0
}
regionAverages.push(avg)
}
// Convert to long form
const longFormData = []
for (const r of regionAverages) {
longFormData.push({Region: r.Region, Source: "Coal", Value: r.Coal / 100})
longFormData.push({Region: r.Region, Source: "Natural Gas", Value: r.NaturalGas / 100})
longFormData.push({Region: r.Region, Source: "Oil", Value: r.Oil / 100})
longFormData.push({Region: r.Region, Source: "Nuclear", Value: r.Nuclear / 100})
longFormData.push({Region: r.Region, Source: "Hydroelectric", Value: r.Hydro / 100})
longFormData.push({Region: r.Region, Source: "Other Renewable", Value: r.OtherRenewable / 100})
}
// Create direct JSON specification
const spec = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"title": "Regional Electricity Production Mix (Latest Available Data)",
"width": 600,
"height": 400,
"data": {
"values": longFormData
},
"mark": "bar",
"encoding": {
"x": {
"field": "Region",
"type": "nominal",
"title": null
},
"y": {
"field": "Value",
"type": "quantitative",
"title": "Electricity Production (%)",
"stack": "normalize",
"axis": {"format": "%"}
},
"color": {
"field": "Source",
"type": "nominal",
"scale": {
"domain": ["Coal", "Natural Gas", "Oil", "Nuclear", "Hydroelectric", "Other Renewable"],
"range": ["#333333", "#f28e2c", "#d08c60", "#b392ac", "#4e79a7", "#7bccc4"]
}
},
"tooltip": [
{"field": "Region", "type": "nominal"},
{"field": "Source", "type": "nominal"},
{"field": "Value", "type": "quantitative", "format": ".1%"}
]
}
}
return vegalite(spec)
}