-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.js
More file actions
193 lines (138 loc) · 4.21 KB
/
chart.js
File metadata and controls
193 lines (138 loc) · 4.21 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
function Graph(id,width,height)
{
this.margin={top:5,bottom:30+5,left:20,right:20};
this.width=width-this.margin.left+this.margin.right;
this.height=height-this.margin.top+this.margin.bottom;
this.values=[];
var color = "steelblue";
var svg = this.svg;
var initNbBins = 10;
svg = d3.select("#"+id).append("svg")
.attr("class","graph")
.attr("width",this.width+this.margin.left+this.margin.right)
.attr("height",this.height+this.margin.top+this.margin.bottom)
.append("g")
.attr("transform","translate("+this.margin.left+","+this.margin.top+")");
d3.select("body").append("p")
.text("Slider histogramme")
.style("color", color);
d3.select("body").append("input")
.attr("type", "range")
.attr("id", "sliderHisto")
.attr("min", "1")
.attr("max", "20")
.attr("value", initNbBins);
d3.select("body").append("p")
.text("Slider regression")
.style("color", color);
d3.select("body").append("input")
.attr("type", "range")
.attr("id", "sliderReg")
.attr("min", "1")
.attr("max", "20")
.attr("value", initNbBins);
var max = d3.max(values);
var min = d3.min(values);
// Axe x
var x = d3.scaleLinear()
.domain([min, max])
.range([0, width]);
// Axe y
var y = d3.scaleLinear()
.range([height, 0]);
var init = d3.histogram()
.domain(x.domain())
.thresholds(max);
var bins = init(values);
// Création du graphe
function makeHistogram(nbBins) {
svg.selectAll("rect")
.data(bins)
.enter().append("rect")
.attr("class", "bar")
.attr("x", 1)
.attr("height", "0")
updateHistogram(nbBins);
// affichage de l'axe x
svg.append("g")
.attr("id", "xAxis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// affichage de l'axe y
svg.append("g")
.attr("id", "yAxis")
.call(d3.axisLeft(y));
}
// mise à jour de l'histogramme en fonction du nombre de bins du slider
function updateHistogram(nbBins) {
var histogram = d3.histogram()
.domain(x.domain())
.thresholds(nbBins);
var bins = histogram(values);
y.domain([0, d3.max(bins, function(d) { return d.length; })]);
svg.selectAll("rect")
.attr("height", "0");
svg.selectAll("rect")
.data(bins)
.attr("transform", function(d) {
return "translate(" + x(d.x0) + "," + y(d.length) + ")"; })
.attr("width", function(d) { return x(d.x1) - x(d.x0)- 5; })
.attr("height", function(d) { return height - y(d.length); })
.attr("fill", color);
svg.select("#yAxis")
.call(d3.axisLeft(y));
}
makeHistogram(initNbBins);
d3.select("#sliderHisto").on("change", function(d){
updateHistogram(this.value)
});
// appel des calculs de densité
var kde = kernelDensityEstimator(kernelGauss(), x.ticks(initNbBins))
var density = kde( values.map(function(d){ return d; }) )
// Affichage de la courbe de densité
var curve = svg
.append('g')
.append("path")
.attr("class", "mypath")
.datum(density)
.attr("fill", "none")
.attr("opacity", ".8")
.attr("stroke", "#000")
.attr("stroke-width", 1)
.attr("stroke-linejoin", "round")
.attr("d", d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]*initNbBins); })
);
// Mise à jour de la courbe en fonction du nombre de bins du slider
function updateChart(binNumber) {
kde = kernelDensityEstimator(kernelGauss(), x.ticks(binNumber))
density = kde( values.map(function(d){ return d; }) )
curve
.datum(density)
.attr("d", d3.line()
.curve(d3.curveBasis)
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]*binNumber); })
);
}
d3.select("#sliderReg").on("change", function(d){
updateChart(this.value)
});
// Calcul de la densité
function kernelDensityEstimator(kernel, X) {
return function(V) {
return X.map(function(x) {
return [x, d3.mean(V, function(v) { return kernel(x - v); })];
});
};
}
// Calcul du coefficient de Gauss
function kernelGauss() {
return function(v) {
return (Math.sqrt(2*3.14)*Math.exp(-0.5*(v*v)));
};
}
this.setValues=function(values){this.values=values;return(this);};
}