-
Notifications
You must be signed in to change notification settings - Fork 3
/
stan-distribution-zoo.js
182 lines (156 loc) · 6.12 KB
/
stan-distribution-zoo.js
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
var height;
var width;
function add_figure(x_axis_text) {
update_data()
if (continuous) {
const margin = { top: 30, right: 50, bottom: 30, left: 50 };
width = $("#figure").width() - margin.left - margin.right;
height = 500 - margin.top - margin.bottom;
svg = d3.select("#figure")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom + 20)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
x = d3.scaleLinear().range([0, width]);
xAxis = d3.axisBottom().scale(x);
svg.append("g")
.attr("transform", `translate(0, ${height})`)
.attr("class", "myXaxis")
y = d3.scaleLinear().range([height, 0]);
yAxis = d3.axisLeft().scale(y);
svg.append("g")
.attr("class", "myYaxis")
} else {
const margin = { top: 30, right: 50, bottom: 30, left: 50 };
width = $("#figure").width() - margin.left - margin.right;
height = 500 - margin.top - margin.bottom;
// append the svg object to the body of the page
svg = d3.select("#figure")
.append("svg")
.attr("width", width + margin.left + margin.right + 20)
.attr("height", height + margin.top + margin.bottom + 20)
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
// Initialize the X axis
x = d3.scaleBand()
.range([0, width])
.padding(0.2);
xAxis = svg.append("g")
.attr("transform", `translate(0,${height})`)
// Initialize the Y axis
y = d3.scaleLinear()
.range([height, 0]);
yAxis = svg.append("g")
.attr("class", "myYaxis")
}
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width/2)
.attr("y", height+30)
.text(x_axis_text);
update()
}
function setBubble(range, bubble) {
const val = range.val();
const min = range.attr("min");
const max = range.attr("max");
const newVal = Number(((val - min) * 100) / (max - min));
bubble
.html(val)
.css("left", `calc(${newVal}% + (${8 - newVal * 0.15}px))`);
}
function setup_page(settings) {
$(document).ready(function () {
$("head").append($("<title>" + settings.function_name + "</title>"));
var body = $("body");
var function_name = $('<div class="distribution-name"><h2>' + settings.function_name + '</h2></div>');
body.append(function_name);
for (i = 0; i < settings.sliders.length; i++) {
var label = $('<label for="' + settings.sliders[i].name + '_slider">' + settings.sliders[i].text + '</label>');
var inp = $('<input type="range" class="range" value="'
+ settings.sliders[i].value + '" min="' + settings.sliders[i].min
+ '" max="' + settings.sliders[i].max + '" step="' + settings.sliders[i].step
+ '" id="' + settings.sliders[i].name + '" name="' + settings.sliders[i].name + '" >');
var out = $('<output class="bubble"></output>');
inp.on("input", function () {
setBubble($(this), $(this).parent().children('.bubble'))
});
inp.on("change", function () {
$(".range").each(function () {
params[$(this).attr("name")] = $(this).val();
});
update()
})
setBubble(inp, out);
var slider = $('<div class="range-wrap"></div>');
body.append(slider.append(label).append(inp).append(out));
}
if (settings.exponentiate) {
var exp_check = $(
'<div class="range-wrap">' +
'<label class="container">Exponentiate<input type="checkbox" id="exp-check" ><span class="checkmark" id="exp"></span></label></div>');
exp_check.on("change", function () {
update()
})
body.append(exp_check);
}
var add_text = $(settings.additional_text);
body.append(add_text)
add_figure(settings.x_axis_text)
});
}
function update() {
update_data()
if (continuous) {
if (typeof x_axis_start !== 'undefined') {
x_domain_min = x_axis_start
} else {
x_domain_min = d3.min(data, function (d) { return d.ser1 })
}
if (typeof x_axis_stop !== 'undefined') {
x_domain_max = x_axis_stop
} else {
x_domain_max = d3.max(data, function (d) { return d.ser1 })
}
x.domain([x_domain_min, x_domain_max]);
svg.selectAll(".myXaxis").transition()
.duration(2000)
.call(xAxis);
y.domain([d3.min(data, function (d) { return d.ser2 }), d3.max(data, function (d) { return d.ser2 })]);
svg.selectAll(".myYaxis")
.transition()
.duration(2000)
.call(yAxis);
const u = svg.selectAll(".lineTest")
.data([data], function (d) { return d.ser1 });
u
.join("path")
.attr("class", "lineTest")
.transition()
.duration(2000)
.attr("d", d3.line()
.x(function (d) { return x(d.ser1); })
.y(function (d) { return y(d.ser2); }))
.attr("fill", "none")
.attr("stroke", "#b2001d")
.attr("stroke-width", 3.5)
} else {
x.domain(data.map(d => d.ser1))
xAxis.call(d3.axisBottom(x))
y.domain([d3.min(data, d => d.ser2), d3.max(data, d => d.ser2)]);
yAxis.transition().duration(1000).call(d3.axisLeft(y));
var u = svg.selectAll("rect")
.data(data)
u
.join("rect")
.transition()
.duration(1000)
.attr("x", d => x(d.ser1))
.attr("y", d => y(d.ser2))
.attr("width", x.bandwidth())
.attr("height", d => height - y(d.ser2))
.attr("fill", "#b2001d")
}
}