-
Notifications
You must be signed in to change notification settings - Fork 1
/
ice-cream-d3.html
171 lines (144 loc) Β· 4.76 KB
/
ice-cream-d3.html
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
<html>
<head>
<title>Ice Cream Picker</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
<py-config type="json">
{
"packages": ["pandas"]
}
</py-config>
</head>
<body>
<div id="input" style="margin: 20px;">
Select your π¨ flavour: <br/>
<input type="radio" id="all" name="flavour" value="ALL">
<label for="all"> All π§</label>
<input type="radio" id="chocolate" name="flavour" value="COCOA">
<label for="chocolate"> Chocolate π«</label>
<input type="radio" id="cherrie" name="flavour" value="CHERRIES">
<label for="cherrie"> Cherries π</label>
<input type="radio" id="berries" name="flavour" value="BERRY">
<label for="berries"> Berries π</label>
<input type="radio" id="cheese" name="flavour" value="CHEESE">
<label for="cheese"> Cheese π§</label>
<input type="radio" id="peanut" name="flavour" value="PEANUT">
<label for="peanut"> Peanut π₯</label>
</div>
<div id="viz"></div>
<script type="importmap">
{
"imports": {
"d3": "https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/optimized/d3.js"
}
}
</script>
<script type="module">
import * as d3 from "https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/optimized/d3.js";
</script>
<py-script output="viz">
import pandas as pd
import d3
from pyodide.http import open_url
from pyodide.ffi import create_proxy, to_js
url = (
"https://raw.githubusercontent.com/Cheukting/pyscript-ice-cream/main/bj-products.csv"
)
ice_data = pd.read_csv(open_url(url)).sort_values(by=["rating"], ascending=False)
current_selected = []
flavour_elements = document.getElementsByName("flavour")
viz = d3.select("#viz")
viz.select(".loading").remove()
margin = {"top": 30, "right": 30, "bottom": 70, "left": 260}
width = 1000 - margin["left"] - margin["right"]
max_height = 1200 - margin["top"] - margin["bottom"]
svg = (viz
.append("svg")
.attr("width", width + margin["left"] + margin["right"])
.attr("height", max_height + margin["top"] + margin["bottom"])
.append("g")
.attr("transform",
f"translate({margin['left']},{margin['top']})")
)
x = d3.scaleLinear().domain([0, 5]).range([0, width])
xAxis = svg.append("g").call(d3.axisBottom(x))
y = d3.scaleBand()
yAxis = svg.append("g").call(d3.axisLeft(y))
def plot(data):
data_js = to_js(data.to_dict(orient="records"))
height = data["name"].count() * 100 - margin["top"] - margin["bottom"]
if height > max_height:
height = max_height
(
svg.transition().duration(1000)
.attr("width", width + margin["left"] + margin["right"])
.attr("height", height + margin["top"] + margin["bottom"])
)
# x-axis
xAxis.transition().duration(1000).attr("transform", f"translate(0,{height})").call(d3.axisBottom(x))
# y-axis
y.domain(list(data["name"])).range([0, height]).padding(.1)
yAxis.transition().duration(1000).call(d3.axisLeft(y))
# bars
y_fn = create_proxy(lambda record, *_:y(record["name"]))
x_fn = create_proxy(lambda record, *_:x(record["rating"]))
u = svg.selectAll("rect").data(data_js)
(
u.enter()
.append("rect")
.merge(u)
.transition()
.duration(1000)
.attr("x", x(0) )
.attr("y", y_fn)
.attr("width", x_fn)
.attr("height", y.bandwidth() )
.attr("fill", "#69b3a2")
)
u.exit().remove()
# label
label_fn = create_proxy(lambda record, *_:record["rating"])
labels = svg.selectAll(".label").remove()
l = svg.selectAll(".text").data(data_js)
(
l.enter()
.append("text")
.merge(l)
.transition()
.duration(1000)
.attr("class","label")
.attr("x", x_fn)
.attr("y", y_fn)
.attr("dy", "1.5em")
.text(label_fn)
)
l.exit().remove()
(
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin["top"] / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("β Ice cream flavour ratings β")
)
def select_flavour(event):
for ele in flavour_elements:
if ele.checked:
current_selected = ele.value
break
if current_selected == "ALL":
plot(ice_data)
else:
filter = ice_data.apply(lambda x: ele.value in x["ingredients"], axis=1)
plot(ice_data[filter])
ele_proxy = create_proxy(select_flavour)
for ele in flavour_elements:
if ele.value == "ALL":
ele.checked = True
current_selected = ele.value
ele.addEventListener("change", ele_proxy)
plot(ice_data)
</py-script>
</body>
</html>