-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathExample.re
More file actions
215 lines (202 loc) · 5.55 KB
/
Example.re
File metadata and controls
215 lines (202 loc) · 5.55 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
module BarChart = {
[@react.component]
let make = () =>
<Recharts.ResponsiveContainer height={Px(200.)} width={Px(300.)}>
<Recharts.BarChart
barCategoryGap={Px(1.)}
margin={
"top": 0,
"right": 0,
"bottom": 0,
"left": 0,
}
data=[|
{
"name": "Page A",
"uv": 4000,
"pv": 2400,
},
{
"name": "Page B",
"uv": 3000,
"pv": 1398,
},
{
"name": "Page C",
"uv": 2000,
"pv": 9800,
},
{
"name": "Page D",
"uv": 2780,
"pv": 3908,
},
{
"name": "Page E",
"uv": 1890,
"pv": 4800,
},
|]>
<Recharts.Bar
name="Some bar"
dataKey="pv"
fill="#2078b4"
stackId="a"
/>
<Recharts.Bar
name="Other bar"
dataKey="uv"
fill="#ff7f02"
stackId="a"
/>
<Recharts.Tooltip />
<Recharts.Legend wrapperStyle={"marginTop": "8px"} align=`left />
</Recharts.BarChart>
</Recharts.ResponsiveContainer>;
};
module PieChart = {
type dataCategory = (string, int, string);
type dataItem = {
name: string,
value: int,
fill: string,
percent: float,
category: dataCategory,
};
/* A custom Legend component, not Recharts.Legend */
module Legend = {
[@react.component]
let make = (~data: array(dataItem), ~onItemClick) => {
<div className="flex flex-col gap-2 mt-5">
{data
|> Array.mapi((index, item) =>
<div
key={Int.to_string(index)}
onClick={_ => onItemClick(item)}
className="flex items-center gap-2 cursor-pointer p-1 rounded transition-colors duration-200 hover:bg-gray-100">
<div
className="w-4 h-4 rounded-sm"
style={ReactDOM.Style.make(~backgroundColor=item.fill, ())}
/>
<span> {React.string(item.name)} </span>
<span className="text-gray-600">
{React.string(Printf.sprintf("%.1f%%", item.percent))}
</span>
</div>
)
|> React.array}
</div>;
};
};
[@react.component]
let make = () => {
let (selectedItem, setSelectedItem) = React.useState(() => None);
let rawData = [
("Desktop", 450, "#0088FE"),
("Mobile", 380, "#00C49F"),
("Tablet", 200, "#FFBB28"),
("Smart TV", 120, "#FF8042"),
];
let total =
rawData |> List.fold_left((acc, (_, value, _)) => acc + value, 0);
let data =
rawData
|> List.map(item => {
let (name, value, fill) = item;
{
name,
value,
fill,
percent: Int.to_float(value) /. Int.to_float(total) *. 100.0,
category: item,
};
})
|> Array.of_list;
let (innerRadius, outerRadius) = (40., 80.);
let containerSize = Int.of_float(outerRadius) * 2 + 40;
let onClick = (data, _index, _event) => {
switch (Js.Nullable.toOption(data)) {
| Some(d) =>
setSelectedItem(_ => Some(d##payload));
Js.log2("Selected:", d##payload.name);
| None => ()
};
};
let handleLegendClick = item => {
setSelectedItem(_ => Some(item));
Js.log2("Legend clicked:", item.name);
};
<div>
<Recharts.ResponsiveContainer
height={Px(Int.to_float(containerSize))}
width={Px(Int.to_float(containerSize))}>
<Recharts.PieChart
margin={
"top": 20,
"right": 20,
"bottom": 20,
"left": 20,
}>
<Recharts.Pie
data
dataKey="value"
nameKey="name"
cx={Prc(50.)}
cy={Prc(50.)}
innerRadius={Px(innerRadius)}
outerRadius={Px(outerRadius)}
startAngle=90
endAngle=(-270)
animationBegin=0
animationDuration=800
onClick>
{data
|> Array.mapi((index, item) => {
<Recharts.Cell
key={Int.to_string(index)}
fill={item.fill}
/>
})
|> React.array}
</Recharts.Pie>
<Recharts.Tooltip
formatter={(value: int, name: string, _props) =>
[|React.int(value), React.string(name)|]
}
/>
</Recharts.PieChart>
</Recharts.ResponsiveContainer>
<Legend data onItemClick=handleLegendClick />
{switch (selectedItem) {
| Some(item) =>
<div className="mt-5 p-2.5 bg-gray-100 rounded">
<strong> {React.string("Selected: ")} </strong>
{React.string(
item.name
++ " - "
++ Int.to_string(item.value)
++ " ("
++ Printf.sprintf("%.1f%%", item.percent)
++ ")",
)}
</div>
| None => React.null
}}
</div>;
};
};
module App = {
[@react.component]
let make = () => {
<main className="flex flex-col items-center h-screen gap-8 p-4">
<BarChart />
<PieChart />
</main>;
};
};
switch (ReactDOM.querySelector("#root")) {
| Some(el) =>
let root = ReactDOM.Client.createRoot(el);
ReactDOM.Client.render(root, <App />);
| None => Js.log("No root element found")
};