Skip to content

Instantly share code, notes, and snippets.

@roveo
Created December 26, 2021 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roveo/a8b6f1920339475e537d4ca03a0c896c to your computer and use it in GitHub Desktop.
Save roveo/a8b6f1920339475e537d4ca03a0c896c to your computer and use it in GitHub Desktop.
Vega spec for SO answer about stratify node totals
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An example of treemap layout for hierarchical data.",
"width": 860,
"height": 600,
"padding": 2.5,
"autosize": "none",
"data": [
{
"name": "tree",
"values": [
{
"id":0,
"name":""
},
{
"id":1,
"name":"INDUSTRY",
"parent":0
},
{"id":2,
"name":"iron & steel",
"percentage":7.2,
"parent":1,
"detail":"manufacturing "
},
{"id":3,
"name":"Chemical & petrochemical",
"percentage":5.8,
"parent":1,
"detail":"greenhouse gases can be produced <br/>as a byproduct of chemical processes like the manufacturing of fertilizers, pharmaceuticals, refrigerants, alongside oil and gas extraction, etc. "
},
{"id":4,
"name":"Food & tobacco",
"percentage":1,
"parent":1,
"detail":"manufacturing of tobacco products and food processing (the conversion of raw agricultural products into their final products, such as the conversion of wheat into bread)."
},
{"id":5,
"name":"non-ferrous metals",
"percentage":0.7,
"parent":1,
"detail":"Non-ferrous metals are metals which contain very little iron: this includes manufacturing of aluminium, copper, lead, nickel, tin, titanium and zinc, and alloys such as brass. The manufacturing of these metals requires energy which results in emissions."
},
{"id":6,
"name":"paper, pulp & printing",
"percentage":0.6,
"parent":1,
"detail":"conversion of wood into paper and pulp."
},
{"id":7,
"name":"machinery",
"percentage":0.5,
"parent":1,
"detail":"production of machinery."
},
{"id":8,
"name":"cement",
"percentage":3,
"parent":1,
"detail":"carbon dioxide is produced as a byproduct of a chemical conversion process used in the production of clinker, a component of cement"
},
{"id":9,
"name":"other industries",
"percentage":10.6,
"parent":1,
"detail":"manufacturing in other industries including mining and quarrying, construction, textiles, wood products, and transport equipment (such as car manufacturing)."
},
{"id":10,
"name":"digital tech",
"percentage":3.7,
"parent":1,
"detail":"servers, networks, computers, screens, devices (manufacture & use)"
},
{"id":11,
"name":"TRANSPORT",
"parent":0
},
{"id":12,
"name":"cars and trucks",
"percentage":11.9,
"parent":11,
"detail":"burning of petrol and diesel from all forms of road transport which includes cars, trucks, lorries, motorcycles and buses - if we could electrify the whole road transport sector, and transition to a fully decarbonized electricity mix, we could feasibly reduce global emissions by 11.9%."
},
{"id":13,
"name":"cars motorcycles buses",
"percentage":7.1,
"parent":11
},
{"id":14,
"name":"lorries and trucks",
"percentage":4.8,
"parent":11
},
{
"id":15,
"name":"aviation",
"percentage":1.9,
"detail":"passenger travel and freight, and domestic and international aviation.",
"parent":11
},
{
"id":16,
"name":"passenger travel",
"percentage":1.5,
"parent":11
},
{
"id":17,
"name":"international",
"percentage":0.9,
"parent":11
},
{
"id":18,
"name":"domestic",
"percentage":0.6,
"parent":11
},
{
"id":19,
"name":"freight",
"percentage":0.4,
"parent":11
},
{
"id":20,
"name":"ships",
"percentage":1.7,
"detail":"burning of petrol or diesel on boats (passenger & feight)",
"parent":11
},
{
"id":20,
"name":"rail",
"percentage":0.4,
"detail":"passenger and freight rail travel.",
"parent":11
},
{
"id":20,
"name":"pipelines",
"percentage":0.3,
"detail":"fuels and commodities (e.g. oil, gas, water or steam) often need to be transported (either within or between countries) via pipelines. This requires energy inputs, which results in emissions.",
"parent":11
},
{
"id":21,
"name":"ENERGY",
"parent":0
},
{
"id":22,
"name":"oil natural gas leaks and losses",
"percentage":3.9,
"detail":"fugitive emissions are the often-accidental leakage of methane to the atmosphere during oil and gas extraction and transportation, from damaged or poorly maintained pipes. This also includes flaring – the intentional burning of gas at oil facilities. Oil wells can release gases, including methane, during extraction – producers often don’t have an existing network of pipelines to transport it, or it wouldn’t make economic sense to provide the infrastructure needed to effectively capture and transport it. But under environmental regulations they need to deal with it somehow: intentionally burning it is often a cheap way to do so.",
"parent":21
},
{
"id":23,
"name":"coal mining leaks and losses",
"percentage":1.9,
"detail":"accidental leakage of methane during coal mining.",
"parent":21
},
{
"id":24,
"name":"other fuels",
"percentage":7.8,
"detail":"electricity and heat from biomass; on-site heat sources; combined heat and power (CHP); nuclear industry; and pumped hydroelectric storage.",
"parent":21
},
{
"id":25,
"name":"BUILDINGS",
"parent":0
},
{
"id":26,
"name":"residencial",
"percentage":10.9,
"detail":"generation of electricity for lighting, appliances, cooking etc. and heating at home..",
"parent":25
},
{
"id":26,
"name":"commercial",
"percentage":6.6,
"detail":"generation of electricity for lighting, appliances, etc. and heating in commercial buildings such as offices, restaurants, and shops.",
"parent":25
},
{
"id":27,
"name":"WASTE",
"parent":0
},
{
"id":28,
"name":"wastewater and sewage",
"percentage":1.3,
"detail":"organic matter and residues from animals, plants, humans and their waste products can collect in wastewater systems. When this organic matter decomposes it produces methane and nitrous oxide..",
"parent":27
},
{
"id":29,
"name":"landfills",
"percentage":1.9,
"detail":"landfills are often low-oxygen environments. In these environments, organic matter is converted to methane when it decomposes.",
"parent":27
},
{
"id":30,
"name":"LAND-USE",
"parent":0
},
{
"id":31,
"name":"cropland",
"percentage":1.4,
"detail":"CO2 can be emitted when croplands are degraded; or sequestered when they are restored.",
"parent":30
},
{
"id":32,
"name":"lost grasslands",
"percentage":0.1,
"detail":"when grassland becomes degraded, these soils can lose carbon, converting to carbon dioxide in the process. Conversely, when grassland is restored (for example, from cropland), carbon can be sequestered. Emissions here therefore refer to the net balance of these carbon losses and gains from grassland biomass and soils.",
"parent":30
},
{
"id":33,
"name":"deforestation",
"percentage":2.2,
"detail":"changes in forestry cover. This means reforestation is counted as ‘negative emissions’ and deforestation as ‘positive emissions’. Net forestry change is therefore the difference between forestry loss and gain. Emissions are based on lost carbon stores from forests and changes in carbon stores in forest soils.",
"parent":30
},
{
"id":34,
"name":"crop burning",
"percentage":3.5,
"detail":"burning of agricultural residues – leftover vegetation from crops such as rice, wheat, sugar cane, and other crops – releases carbon dioxide, nitrous oxide and methane. Farmers often burn crop residues after harvest to prepare land for the resowing of crops.",
"parent":30
},
{
"id":35,
"name":"rice growing",
"percentage":1.3,
"detail":"landfills are often low-oxygen environments. In these environments, organic matter is converted to methane when it decomposes.",
"parent":30
},
{
"id":36,
"name":"fertilizers",
"percentage":4.1,
"detail":"flooded paddy fields produce methane through a process called ‘anaerobic digestion’. Organic matter in the soil is converted to methane due to the low-oxygen environment of water-logged rice fields. 1.3% seems substantial, but it’s important to put this into context: rice accounts for around one-fifth of the world’s supply of calories, and is a staple crop for billions of people globally.",
"parent":30
},
{
"id":37,
"name":"agricultureal machinery",
"percentage":1.7,
"detail":"Nitrous oxide – a strong greenhouse gas – is produced when synthetic nitrogen fertilizers are applied to soils.",
"parent":30
},
{
"id":38,
"name":"livestock manure",
"percentage":5.8,
"detail":"hey",
"parent":30
}
],
"transform": [
{
"type": "stratify",
"key": "id",
"parentKey": "parent"
},
{
"type": "treemap",
"field": "percentage",
"sort": {"field": "value", "order":"descending"},
"round": true,
"method": "resquarify",
"ratio": 1,
"size": [{"signal": "width"}, {"signal": "height"}],
"paddingOuter": 2,
"paddingInner":2,
"as": [
"x0",
"y0",
"x1",
"y1",
"depth",
"children"
]
}
]
},
{
"name": "leaves",
"source": "tree",
"transform": [{ "type": "filter", "expr": "!datum.children" },
{"type": "filter", "expr": "datum.percentage > 0.3"}]
},
{
"name": "totals",
"source": "leaves",
"transform": [{"type": "aggregate", "groupby": ["parent"], "fields": ["percentage"], "as": ["total"], "ops": ["sum"]}]
},
{
"name": "nodes",
"source": "tree",
"transform": [
{ "type": "filter", "expr": "datum.children" },
{ "type": "lookup", "from": "totals", "key": "parent", "fields": ["id"], "values": ["total"], "as": ["total"]}
]
},
{
"name": "leavesByParent",
"source": "leaves",
"transform": [{ "type": "aggregate", "groupby":["parent"]}]
}
],
"scales": [
{
"name": "colorByNodes",
"type": "ordinal",
"domain": {"data": "nodes", "field": "name"},
"range": [
"transparent", "#dd96ba", "#dea84e", "#c83836", "#dfde9b",
"#5eafb9", "#adc35d"]
},
{
"name": "colorByNodesByLeaves",
"type": "ordinal",
"domain": {"data": "leavesByParent", "field": "parent"},
"range": [
"#dd96ba", "#dea84e", "#c83836", "#dfde9b",
"#5eafb9", "#adc35d"]
},
{
"name": "size",
"type": "ordinal",
"domain": [0, 1, 2, 3],
"range": [256, 28, 7, 14]
},
{
"name": "sizeP",
"type": "ordinal",
"domain": [0, 1, 2, 3],
"range": [256, 28, 15, 14]
}
],
"marks": [
{
"type": "rect",
"from": {"data": "nodes"},
"interactive": false,
"encode": {
"enter": {
"fill": {"value":"#333238"},
"stroke": {"scale": "colorByNodes", "field": "name"},
"strokeWidth":{"value": 5}
},
"update": {
"x": {"field": "x0"},
"y": {"field": "y0"},
"x2": {"field": "x1"},
"y2": {"field": "y1"}
}
}
},
{
"type": "rect",
"from": {"data": "leaves"},
"encode": {
"enter": {
"stroke": {"value": "#fff"}
},
"update": {
"x": {"field": "x0"},
"y": {"field": "y0"},
"x2": {"field": "x1"},
"y2": {"field": "y1"},
"fill": {"value": "transparent"}
},
"hover": {
"fill": {"value": "red"}
}
}
},
{
"type": "text",
"from": {"data": "nodes"},
"interactive": false,
"encode": {
"enter": {
"font": {"value": "Helvetica Neue, Arial"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"fill": {"scale": "colorByNodes", "field": "name"},
"text": {"field": "name"},
"fontSize": {"scale": "size", "field": "depth"}
},
"update": {
"x": {"signal": "0.5 * (datum.x0 + datum.x1)"},
"y": {"signal": "0.5 * (datum.y0 + datum.y1)"}
}
}
},
{
"type": "text",
"from": {"data": "leaves"},
"interactive": false,
"encode": {
"enter": {
"font": {"value": "Helvetica Neue, Arial"},
"align": {"value": "left"},
"baseline": {"value": "alphabetic"},
"fill": {"scale": "colorByNodesByLeaves", "field": "parent"},
"text": {"field": "name"},
"fontSize": {"scale": "size", "field": "depth"}
},
"update": {
"x": {"signal": "datum.x0 +10"},
"y": {"signal": "datum.y0 +10"}
}
}
},
{
"type": "text",
"from": {"data": "leaves"},
"interactive": false,
"encode": {
"enter": {
"font": {"value": "Helvetica Neue, Arial"},
"align": {"value": "left"},
"baseline": {"value": "middle"},
"fill": {"value":"#6b686f"},
"text": {"field": "percentage"},
"fontSize": {"scale": "sizeP", "field": "depth"}
},
"update": {
"x": {"signal": "datum.x0 +17"},
"y": {"signal": "datum.y0 +20"}
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment