<jupyter_start><jupyter_text>Read in files<jupyter_code>b = pd.read_csv("building_inventory.csv",
           na_values = {'Year Acquired': 0,
                       'Year Constructed': 0}) 
# b<jupyter_output><empty_output><jupyter_text>Making a pivot table to summarize the data for 'Congress Dist' and 'Agency Name'<jupyter_code>stats = pd.pivot_table(b,index = 'Congress Dist',columns='Agency Name',values = 'Square Footage',aggfunc =np.sum)
# sums = pd.pivot_table(b,index = ['Congress Dist'],values = ['Square Footage'],aggfunc =np.sum)
# sums.values
# type(stats.columns)
# stats.loc[0, 'Appellate Court / Fifth District']
# stats.index
stats
# (I) CREATE LABEL
mySelectedLabel = ipywidgets.Label() # start with our label
# mySelectedLabel<jupyter_output><empty_output><jupyter_text>In the head map, the data is calculated by log10, as the original data span is too large. Two axes are Congress Dist and Agency Name.<jupyter_code># (II) HEAT MAP
# (1) Scales: x/y, colors
col_sc = bqplot.ColorScale(scheme = "Reds")
x_sc = bqplot.OrdinalScale()
y_sc = bqplot.OrdinalScale()

# (2) Axis: x/y, colors
c_ax = bqplot.ColorAxis(scale = col_sc, 
                        orientation = 'vertical', 
                        side = 'right')
x_ax = bqplot.Axis(scale = x_sc)
y_ax = bqplot.Axis(scale = y_sc, 
                   orientation = 'vertical')

# (3) Marks: heatmap
heat_map = bqplot.GridHeatMap(color = np.log10(stats.values),
                              row = stats.index,
                              column = stats.columns,
                              scales = {'color': col_sc,
                                        'row': y_sc,
                                        'column': x_sc},
                              interactions = {'click': 'select'}, # make interactive on click of each box
                              anchor_style = {'fill':'blue'}, # to make our selection blue
                              selected_style = {'opacity': 1.0}, # make 100% opaque if box is selected
                              unselected_style = {'opacity': 0.8}) # make a little see-through if not<jupyter_output><empty_output><jupyter_text>Hard code the default data. [0,0] was put as default selection.<jupyter_code># (III) LINE PLOT
# (1) scales
x_scl = bqplot.LinearScale()
y_scl = bqplot.LinearScale()

# (2) Axis
ax_xcl = bqplot.Axis(label='Year Acquired', scale=x_scl)
ax_ycl = bqplot.Axis(label='Total Square Footage', scale=y_scl, 
                    orientation='vertical', side='left')

# (3) Marks
i, j = 0, 0
temp = b[(b['Congress Dist'] == i) & (b['Agency Name'] == stats.columns[j])]
g = temp.groupby(['Year Acquired'])['Square Footage'].sum()
# x = np.array(g.index)
# y = np.array(g)
footage_line = bqplot.Lines(x = np.array(g.index), y = np.array(g), scales={'x':x_scl, 'y':y_scl})<jupyter_output><empty_output><jupyter_text>Define the on_select function. When selected, the line axis values are modified to the newly selected ones.<jupyter_code># (IV) LINKING TOGETHER DASHBOARD WITH INTERACTIVITY
def on_selected(change):
    if len(change['owner'].selected) == 1: 
        i, j = change['owner'].selected[0]
        v = stats.loc[i][j]
        mySelectedLabel.value = 'Data Value = ' + str(v)
        
        temp = b[(b['Congress Dist'] == i) & (b['Agency Name'] == stats.columns[j])]
        g = temp.groupby(['Year Acquired'])['Square Footage'].sum()
        footage_line.x = np.array(g.index)
        footage_line.y = np.array(g)
        
heat_map.observe(on_selected, 'selected')<jupyter_output><empty_output><jupyter_text>Put all figures and lables together.<jupyter_code># (V) CREATE FIGURES
fig_heatmap = bqplot.Figure(marks = [heat_map], axes = [c_ax, y_ax, x_ax])
fig_sq = bqplot.Figure(marks = [footage_line], axes = [ax_xcl, ax_ycl])

fig_heatmap.layout.min_width='500px'
fig_sq.layout.min_width='500px'
myDashboard = ipywidgets.VBox([mySelectedLabel, ipywidgets.HBox([fig_heatmap,fig_sq])])
myDashboard<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup