File size: 4,066 Bytes
07c7fb5
718f172
fabc384
718f172
fc80367
 
 
718f172
07c7fb5
4c83c98
 
fabc384
4c83c98
fabc384
 
 
 
4c83c98
 
 
 
 
 
fc80367
07c7fb5
fc80367
 
 
07c7fb5
fc80367
 
 
 
07c7fb5
 
 
dd7931c
 
718f172
 
 
 
 
 
 
07c7fb5
 
718f172
 
3d462a4
718f172
3d462a4
718f172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc80367
 
 
 
 
 
 
 
 
c39f4d0
718f172
3d462a4
 
fc80367
3d462a4
fc80367
3d462a4
 
718f172
3d462a4
718f172
fc80367
 
 
dda3335
f2d3c9f
 
 
dda3335
2482c3f
fc80367
718f172
fc80367
 
 
 
 
 
dda3335
fc80367
 
718f172
d5d984c
 
 
 
 
 
fc80367
 
 
c93d68b
 
 
 
 
 
 
 
 
 
fc80367
718f172
fc80367
 
 
3d462a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc80367
3d462a4
 
fc80367
 
3d462a4
fc80367
3d462a4
 
 
 
fc80367
3d462a4
718f172
 
d7684ca
 
fc80367
 
 
 
 
 
 
 
 
3d462a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pandas as pd
import math
import re

from bokeh.models import HoverTool
from bokeh.plotting import figure
from bokeh.models import CustomJS


def split_created_year(created_year):

    matches = re.findall("([1-3][0-9]{3})", created_year)

    start_date = int(matches[0])

    if len(matches) > 1:
        end_date = int(matches[-1])
    else:
        end_date = start_date

    return start_date, end_date


def display_event() -> CustomJS:

    js_code = """
        console.log('Hello mum!)
    """

    return CustomJS(code=js_code)


def clean_df(columns=None, dataset="", subset=[]):

    df = pd.read_csv(dataset)

    if columns:
        df = df[columns]

    if subset:
        df = df.dropna(subset=subset)
    else:
        df = df.dropna()

    df = df.reset_index(drop=True)

    return df


def create_rgba(df, palette_col):

    rgba = f'rgb({df[f"red_{palette_col}"]}, {df[f"green_{palette_col}"]}, {df[f"blue_{palette_col}"]})'

    return rgba


def get_square_coords(df):
    start = 0
    x = 0

    length = len(df)
    sq = math.sqrt(length)
    increment = round(sq)
    end = increment

    coords = []
    while end <= length:
        arr = [(x, y) for y, item in enumerate(df.index[start:end])]
        coords.extend(arr)
        end += increment
        start += increment
        x += 1

    return coords


def get_tooltips_dict(df, columns):

    df = df[columns]

    tooltips = df.to_dict()

    return tooltips


def create_grid(df, palette_columns=["pal_1", "pal_3", "pal_5"]):

    markers = {
        "pal_1": "circle",
        "pal_2": "hex",
        "pal_3": "square_pin",
        "pal_4": "plus",
        "pal_5": "triangle",
    }

    coords = get_square_coords(df)

    # bodge the df to ensure the length matches the coords
    df = df.head(len(coords))

    df["viewer_link"] = (
        "https://viewer.slv.vic.gov.au/?entity="
        + df["IE PID"].astype(str)
        + "&mode=browse"
    )

    TOOLS = "crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset"

    data = pd.DataFrame(
        {
            "x": [coord[0] for coord in coords],
            "y": [coord[1] for coord in coords],
            "titles": df["Title (DC)"].values.tolist(),
            "created": df["Created - W 3 CDTF (DCTERMS)"].values.tolist(),
            "viewer_link": df["viewer_link"].values.tolist(),
        }
    )

    tt = """
    <div>
        <a href=@viewer_link{safe}}>@viewer_link{safe}</a>
    </div>
    """

    p = figure(sizing_mode="stretch_width", max_width=1000, tools=TOOLS)
    p.grid.grid_line_color = None
    p.axis.visible = False
    # hover = HoverTool(
    #     # tooltips=[
    #     #     ("Title", "@titles"),
    #     #     ("Date created", "@created"),
    #     #     ("SLV Viewer", "@viewer_link"),
    #     # ]
    #     tt
    # )

    hover = HoverTool(tooltips=tt)
    p.add_tools(hover)

    radius = 12
    alpha = 0.25
    hover_alpha = 0.5

    for col in palette_columns:

        df[col] = df[col].str.strip("[")
        df[col] = df[col].str.strip("]")

        df[[f"red_{col}", f"green_{col}", f"blue_{col}"]] = df[col].str.split(
            ",", expand=True
        )

        df[f"rgba_{col}"] = (
            "rgb("
            + df[f"red_{col}"]
            + ","
            + df[f"green_{col}"]
            + ","
            + df[f"blue_{col}"]
            + ")"
        )

        data["rgba"] = df[f"rgba_{col}"].values.tolist()

        p.scatter(
            x="x",
            y="y",
            size=radius,
            color="rgba",
            alpha=alpha,
            hover_alpha=hover_alpha,
            hover_line_color="white",
            marker=markers[col],
            source=data,
        )

    return p


# div = Div(width=1000)
# curdoc().on_event(events.DocumentReady, display_event(div))

# from bokeh import events

# dataset = "https://raw.githubusercontent.com/StateLibraryVictoria/public-domain-hack-2024/refs/heads/ch4-data-viz/datasets/ch3_colour_data_viz_suggestions_set_2_augmented.csv"

# p = create_grid(dataset)

# show(p)