Who's responsible?

Archives New Zealand's Archway database divides government activities up into 303 functions. Over time, different agencies have been made responsible for these functions, and it can be interesting to track how these responsibilities have shifted.

This notebook uses data about functions harvested from Archway to create a a simple visualisation of the agencies responsible for a selected function.

In [2]:
import ipywidgets as widgets
from IPython.display import display, HTML
import json
import altair as alt
import pandas as pd

In [8]:
# Load the harvested functions data from a JSON file
with open('data/functions.json', 'r') as json_file:
        data = json.load(json_file)
In [9]:
# Create a dictionary to use in the dropdown
options = {f['term']: f for f in data}
In [12]:
def make_chart(change):
    # Clear current output
    # Get the currently selected term from the dropdown
    term = change['new']
    # Get the agencies responsible for the selected function
    agencies = term['agencies_responsible']
    if agencies:
        # Convert to a dataframe
        df = pd.DataFrame(agencies)
        # Set some defualts for missing dates
        df['start_date'] = df['start_date'].replace('', '1853')
        df['end_date'] = df['end_date'].replace('', '2019')
        # Create a Gannt style chart
        chart = alt.Chart(df).mark_bar(size=20).encode(
            x=alt.X('start_date:T', axis=alt.Axis(format='%Y', title='Dates agency was responsible for function'), scale=alt.Scale(nice=True)),
            y=alt.Y('entity', scale=alt.Scale(rangeStep=30), title='Agency'),
            color=alt.Color('entity', legend=None),
            tooltip=[alt.Tooltip('entity', title='Agency'), alt.Tooltip('start_date', title='From', timeUnit='year'), alt.Tooltip('end_date', title='To', timeUnit='year')]
        with out:
            display(HTML('<h3>Agencies responsible for &lsquo;{}&rsquo;</h3>'.format(term['term'])))
        with out:
            display(HTML('<p>No agencies responsible for &lsquo;{}&rsquo;</p>'.format(term['term'])))

# This is where the chart will be displayed
out = widgets.Output()

# Create the dropdown
term = widgets.Dropdown(

# Making a selection from the dropdown will automatically run 'make_chart'
term.observe(make_chart, names='value')

display(widgets.HBox([widgets.Label('Select a function:'), term]))
In [ ]:
In [ ]: