-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeste2.jl
More file actions
228 lines (185 loc) · 7.04 KB
/
Copy pathTeste2.jl
File metadata and controls
228 lines (185 loc) · 7.04 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
216
217
218
219
220
221
222
223
224
225
226
227
228
using Stipple
using StippleUI
using StipplePlotly
using CSV, DataFrames, Dates
# configuration
const data_opts = DataTableOptions(columns = [Column("Good_Rating"), Column("Amount", align = :right),
Column("Age", align = :right), Column("Duration", align = :right)])
const plot_colors = ["#72C8A9", "#BD5631"]
# reading data from CSV file and contrucing data frame
cd(@__DIR__)
data = CSV.File("Geo/german_credit.csv") |> DataFrame
# Defining a Stipple ReactiveModel of type observable
@reactive mutable struct Dashboard1 <: ReactiveModel
credit_data::R{DataTable} = DataTable()
credit_data_pagination::DataTablePagination = DataTablePagination(rows_per_page=100)
credit_data_loading::R{Bool} = false
range_data::R{RangeData{Int}} = RangeData(18:80)
big_numbers_count_good_credits::R{Int} = 0
big_numbers_count_bad_credits::R{Int} = 0
big_numbers_amount_good_credits::R{Int} = 0
big_numbers_amount_bad_credits::R{Int} = 0
age_slots::R{Vector{String}} = ["20-30", "30-40", "40-50", "50-60", "60-70", "70-80"]
bar_plot_data::R{Vector{PlotData}} = PlotData[]
bar_layout::R{PlotLayout} = PlotLayout(barmode= "group")
bubble_plot_data::R{Vector{PlotData}} = PlotData[]
bubble_layout::R{PlotLayout} = PlotLayout(showlegend = false)
end
# functions
function creditdata(data::DataFrame, model::M) where {M<:Stipple.ReactiveModel}
model.credit_data[] = DataTable(data, data_opts) # credit_data propertly of type StippleUI.DataTable is assigned to data from CSV
# data_opts data_opts = DataTableOptions(columns = [Column("Good_Rating"), Column("Amount", align = :right), Column("Age", align = :right), Column("Duration", align = :right)])
end
function bignumbers(data::DataFrame, model::M) where {M<:ReactiveModel}
model.big_numbers_count_good_credits[] = data[data.Good_Rating .== true, [:Good_Rating]] |> nrow # Good_Rating from CSV
model.big_numbers_count_bad_credits[] = data[data.Good_Rating .== false, [:Good_Rating]] |> nrow
model.big_numbers_amount_good_credits[] = data[data.Good_Rating .== true, [:Amount]] |> Array |> sum # Amount field from CSV
model.big_numbers_amount_bad_credits[] = data[data.Good_Rating .== false, [:Amount]] |> Array |> sum
end
function barstats(data::DataFrame, model::M) where {M<:Stipple.ReactiveModel}
age_stats = Dict{Symbol,Vector{Int}}(:good_credit => Int[], :bad_credit => Int[])
for x in 20:10:70
push!(age_stats[:good_credit],
data[(data.Age .∈ [x:x+10]) .& (data.Good_Rating .== true), [:Good_Rating]] |> nrow)
push!(age_stats[:bad_credit],
data[(data.Age .∈ [x:x+10]) .& (data.Good_Rating .== false), [:Good_Rating]] |> nrow)
end
model.bar_plot_data[] =
[PlotData(x = model.age_slots[],
y = age_stats[:good_credit],
name = "Good credit",
plot = StipplePlotly.Charts.PLOT_TYPE_BAR,
marker = PlotDataMarker(color = plot_colors[1])),
PlotData(x = model.age_slots[],
y = age_stats[:bad_credit],
name = "Bad credit",
plot = StipplePlotly.Charts.PLOT_TYPE_BAR,
marker = PlotDataMarker(color = plot_colors[2]))]
end
function bubblestats(data::DataFrame, model::M) where {M<:ReactiveModel}
selected_columns = [:Age, :Amount, :Duration]
credit_stats = Dict{Symbol,DataFrame}()
credit_stats[:good_credit] = data[data.Good_Rating .== true, selected_columns]
credit_stats[:bad_credit] = data[data.Good_Rating .== false, selected_columns]
model.bubble_plot_data[] =
[PlotData(x = credit_stats[:good_credit].Age,
y = credit_stats[:good_credit].Amount,
name = "Good Credit",
mode = "markers",
marker = PlotDataMarker(size=18, opacity= 0.4, color = plot_colors[1], symbol="circle")),
PlotData(x = credit_stats[:bad_credit].Age,
y = credit_stats[:bad_credit].Amount,
name = "Bad Credit",
mode = "markers",
marker = PlotDataMarker(size=18, color = plot_colors[2], symbol="cross"))]
end
function setmodel(data::DataFrame, model::M)::M where {M<:ReactiveModel}
creditdata(data, model)
bignumbers(data, model)
barstats(data, model)
bubblestats(data, model)
model
end
### setting up vuejs and stipple connection with ReactiveModel
# Stipple.register_components(Dashboard1, StippleCharts.COMPONENTS)
# Instantiating Reactive Model isntantace
gc_model = setmodel(data, Dashboard1()) |> init
function filterdata(model::Dashboard1)
model.credit_data_loading[] = true
model = setmodel(data[(model.range_data[].range.start .<= data[!, :Age] .<= model.range_data[].range.stop), :], model)
model.credit_data_loading[] = false
nothing
end
# handlers
on(gc_model.range_data) do _
filterdata(gc_model)
end
function ui(model)
(
page(model,
title="German Credits",
head_content = Genie.Assets.favicon_support(),
partial = false,
prepend = style(
"""
.modebar {
display: none!important;
}
"""
),
[
heading("German Credits by Age")
row([
cell(class="st-module", [
row([
cell(class="st-br", [
bignumber("Bad credits",
:big_numbers_count_bad_credits,
icon="format_list_numbered",
color="negative")
])
cell(class="st-br", [
bignumber("Good credits",
:big_numbers_count_good_credits,
icon="format_list_numbered",
color="positive")
])
cell(class="st-br", [
bignumber("Bad credits total amount",
R"big_numbers_amount_bad_credits | numberformat",
icon="euro_symbol",
color="negative")
])
cell(class="st-br", [
bignumber("Good credits total amount",
R"big_numbers_amount_good_credits | numberformat",
icon="euro_symbol",
color="positive")
])
])
])
])
row([
cell([
h4("Age interval filter")
range(18:1:90,
:range_data;
label=true,
labelalways=true,
labelvalueleft=Symbol("'Min age: ' + range_data.min"),
labelvalueright=Symbol("'Max age: ' + range_data.max"))
])
])
row([
cell(class="st-module", [
h4("Credits data")
table(:credit_data;
style="height: 400px;",
pagination=:credit_data_pagination,
loading=:credit_data_loading
)
])
cell(class="st-module", [
h4("Credits by age")
plot(:bar_plot_data; layout=:bar_layout, config = "{ displayLogo:false }")
])
])
row([
cell(class="st-module", [
h4("Credits by age, amount and duration")
plot(:bubble_plot_data, layout=:bubble_layout, config = "{ displayLogo:false }")
])
])
footer(class="st-footer q-pa-md", [
cell([
span("Stipple © $(year(now()))")
])
])
])
)
end
# serving on localhost
route("/") do
ui(gc_model) |> html
end
up()