Published unlisted
Edited
Feb 22, 2021
Insert cell
Insert cell
margin = ({top: 50, bottom: 50, left: 75, right: 50})
Insert cell
height = 640
Insert cell
data = FileAttachment("nitrate_leaching_fixs.csv").csv()//{typed: true})
Insert cell
group = d3.group(data, d => d.geography_name, d => d.year, d=> d.animal)

/*
geography_type
geography_name
animal
year
no3_kg_per_yr
*/
Insert cell
group.get("Auckland").get("1990")// .get('Sheep') //[0].no3_kg_per_yr
Insert cell
hierarchy = d3.hierarchy(group)
Insert cell
hierarchy.height
Insert cell
hierarchy.data
Insert cell
hierarchy.children[0].data // region
Insert cell
hierarchy.children[0].children[0].data // year
Insert cell
hierarchy.children[0].children[0].children[0].data // Animal
Insert cell
hierarchy.children[0].children[0].children[0].children[0].data // the complete data at the bottom level
Insert cell
hierarchy.children[0].children[0].children[0].children[0].data.no3_kg_per_yr
Insert cell
{
const ff = []
hierarchy.descendants().map(
d => ff.push(`${"> ".repeat(d.depth)} ${d.data[0] || d.data["name"] || d.data.no3_kg_per_yr}`)
)
return html`<pre style="max-height: 15em; overflow-y: scroll;">${ff.join(
"\n"
)}`;
}
Insert cell
hierarchy.count().value // number of entries in the dataset
Insert cell
sums = hierarchy.copy().sum(d => {
if (!(d.geography_type=='Island'||d.geography_type=='New Zealand')) return d.no3_kg_per_yr}) // sum only the regions
Insert cell
sums.value
Insert cell
sums.find(d => d.data[0] === "Auckland").value // 1990 auckland total
Insert cell
sums.find(d => d.data[0] === "Sheep").value // 1990 auckland sheep
Insert cell
sums.find(d => d.data[0] === "Deer").value
Insert cell
sums.find(d => d.data[0] === '1990').value // auckland total for 1990
Insert cell
sums.find(d => d.data[0] === '2017').value // auckland total
Insert cell
hierarchy.leaves().map(d => d.data)//[111]
Insert cell
hierarchy.copy().sort((a, b) => d3.ascending(a.data[0], b.data[0])).children.map(d => d.data[0])
Insert cell
rollup = d3.rollup( // makes totals for each region each year.
data,
v => d3.sum(v, d => d.no3_kg_per_yr),
d => d.year,
d => d.geography_name
// d=>d.animal // this would return the same as the orgional data

)
Insert cell
R = d3.hierarchy(rollup)
Insert cell
R.leaves().map(d => [d.parent.data[0], ">", ...d.data].join(" "))
Insert cell
{
const names = [];
R.copy().sum(d => d[1]).eachBefore((d, index) =>
names.push(" ".repeat(d.depth) + `${d.depth}: ${d.data[0] || "All"}, ${d.value} (${index})`)
);
return html`<pre style="max-height: 15em; overflow-y: scroll;">${names.join(
"\n"
)}`;
}
Insert cell
{
const names = [];
R.copy().sum(d => d[1]).eachAfter((d, i) =>
names.push(" ".repeat(d.depth) + `${d.depth}: ${d.data[0] || "All"}, ${d.value} (${i})`)
);
return html`<pre style="max-height: 15em; overflow-y: scroll;">${names.join(
"\n"
)}`;
}
Insert cell
d3.extent(hierarchy, d => d.data.no3_kg_per_yr)
Insert cell
Insert cell
no3_rollup = d3.rollup(no3, v => v.length,d => d.year, d => d.geography_name, d => d.animal, d=>d.no3_kg_per_yr)
Insert cell
data_table = Table(no3_hierarchy.map(d => ({"Region": d.geography_name, "Nitrates (kg per year)": d.no3_kg_per_yr,"Stock type": d.animal,'year':d.year })),
{columns: ["Region", "Stock type",'year','Nitrates (kg per year)',' '],
layout:'auto',
align: { 'animal':'center','year':'center','no3_kg_per_yr':'right'},
width: { 'geography_name':'150px','animal':'120px','year':'90px','no3_kg_per_yr':'120px' },
})
Insert cell
Insert cell
no3_hierarchy = {

// if(nitrateData[nitrateData.length-1].children!='root') { // this added a root node
// nitrateData.push({parent: "",
// geography_type: "New Zealand",
// geography_name: "Aotearoa" ,
// children: "root",
// })
// }
let no3h = nitrateData.filter( d => {
// d.year=new Date(Date.UTC(d.year, 0, 1)) // can't use "Date" as I am using the year sting in the name hierarchy
d.year=''+d.year // fix the auto format make the year a string
if (d.no3_kg_per_yr==-1) d.no3_kg_per_yr =0 // fix up the NA -1 data to read zero as some regions used 0 and others NA
switch(d.geography_name){
case 'New Zealand':
d.children=d.geography_name+d.animal+d.year
d['parent']='root'
break;

case 'North Island':
case 'South Island':
d.children=d.geography_name+d.animal+d.year
d['parent']='New Zealand'+d.animal+d.year
break;

case "Auckland":
case "Bay of Plenty":
case "Gisborne":
case "Hawke's Bay":
case "Manawatu-Whanganui":
case "Waikato":
case "Wellington":
case "Northland":
case "Taranaki":
d.children = d.geography_name+d.animal+d.year
d.parent = 'North Island'+d.animal+d.year
break;

case "Otago":
case "Southland":
case "Marlborough":
case "Tasman":
case "West Coast":
case "Canterbury":
d.children = d.geography_name+d.animal+d.year
d.parent = 'South Island'+d.animal+d.year
break;
}

if (!(d.geography_type == 'New Zealand'||d.geography_type == 'Island')) return d // remover New Zealand, North and South Island totals from the dataset.
})
return no3h
}
Insert cell
Insert cell
Insert cell
viewof res = Table(search,
{columns: ["geography_name", "animal",'year','no3_kg_per_yr','geography_type',' '],
layout:'auto',
align: { 'animal':'center','year':'center','no3_kg_per_yr':'right'},
width: { 'geography_name':'150px','animal':'120px','year':'90px','no3_kg_per_yr':'120px' },
})
Insert cell
Insert cell
// filter by one region
no3_Canterbury = no3.filter( d => {
if (d.geography_name=='Canterbury') return d
})
Insert cell
no3 = nitrateData.filter( d => {
d.year=''+d.year // fix the auto format make the year a string
if (d.no3_kg_per_yr==-1) d.no3_kg_per_yr =0 // fix up the NA -1 data to read zero as some regions used 0 and others NA
return d
})
Insert cell
Insert cell
viewof canSearch = Search(no3_Canterbury, {label: "NO3 levels in Canterbury NZ."})
Insert cell
canNO3 = Table(canSearch,
{columns: ["year", "geography_name", "animal",'no3_kg_per_yr',' ','some padding to move the data left',''],
// layout:'auto',
align: { 'animal':'left','year':'center','geography_name':'left','no3_kg_per_yr':'right'},
}
)
Insert cell
nitrateData = FileAttachment("nitrate_leaching_fixs.csv").csv({typed: true})
Insert cell
import {Table, Search} from "@observablehq/inputs"
Insert cell
d3 = require('d3')
Insert cell
_ = require('lodash')
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