-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
345 lines (304 loc) · 11.1 KB
/
index.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<html>
<head>
<title>Data Visualization</title>
<meta http-equiv="X-UA-Compatible" content="IE=9">
<style>
.pie {
margin: 20px;
}
.legend {
float: left;
font-family: "Verdana";
font-size: .7em;
}
.pie text {
font-family: "Verdana";
fill: #000;
}
.pie .name-text{
font-size: .8em;
}
.pie .value-text{
font-size: 3em;
}
.bar{
fill: #28C587;
}
/* Lighten the axis lines */
#x-axis path,#y-axis line, #y-axis path{
stroke: #666;
}
/* Remove the tick marks on the x-axis */
#x-axis .tick line{
visibility: hidden;
}
text{
fill: #666;
font-size: 12px;
}
#x-axis text {
font-weight: 600;
}
</style>
</head>
<body>
<div class="container">
<h1>Persentase study</h1>
<div id="chart"></div>
</div>
<br/>
<div class="container">
<h1>Quiz Completion Analytics</h1>
<svg id="chart2"></svg>
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript">
var groupId = {};
var maxId = 0;
d3.csv("data/mock.csv", function(d) {
function ReadDataStudyCount(){
var groupId = [];
var maxId = 0;
for (var i = 0; i < d.length; i++){
var item = d[i];
var id_study = item.study_id;
num = id_study.replace(/^\D+/g, "");
if (!groupId[num]){
groupId[num] = [];
}
groupId[num].push({study_id: item.study_id,site_id: item.site_id,participant_name:item.participant_name, country: item.country, document: item.document, quiz_completion: item.quiz_completion});
if (maxId < num){
maxId = num;
};
}
myObj = [];
myObj2 = [];
myObj3 = [];
for (var i = 1; i <= maxId; i++){
myObj[i] = groupId[i].length;
var jumlah_data = myObj.length;
for (var j = 0; j < jumlah_data; j++){
myObj2[i] = {nama_study: groupId[i][j].study_id, jumlahambilstudy: groupId[i].length};
}
}
return myObj2;
}
function ReadDataStudy2Count(){
var studyCount = d3.nest()
.key(function(d) { return d.study_id; })
.rollup(function(a){return a.length;})
.entries(d)
.sort(function(a, b){ return d3.ascending(a.values, b.values); })
return studyCount;
}
function ReadDataQuizCompletionCount(){
var quizCount = d3.nest()
.key(function(d) { return d.quiz_completion; })
.rollup(function(a){return a.length;})
.entries(d)
.sort(function(a, b){ return d3.ascending(a.values, b.values); })
return quizCount;
}
function ReadDataDocumentCount(){
var documentCount = d3.nest()
.key(function(d) { return d.document; })
.rollup(function(a){return a.length;})
.entries(d)
.sort(function(a, b){ return d3.ascending(a.values, b.values); })
return documentCount;
}
console.log(ReadDataStudy2Count());
console.log(ReadDataStudyCount());
console.log(ReadDataQuizCompletionCount());
console.log(ReadDataDocumentCount());
function CreatePieChart(){
var data = ReadDataStudy2Count();
var text = "";
var width = 200;
var height = 200;
var thickness = 40;
var duration = 750;
var padding = 10;
var opacity = .8;
var opacityHover = 1;
var otherOpacityOnHover = .8;
var tooltipMargin = 13;
var radius = Math.min(width-padding, height-padding) / 2;
var color = d3.scaleOrdinal(d3.schemeCategory10);
var svg = d3.select("#chart")
.append('svg')
.attr('class', 'pie')
.attr('width', width)
.attr('height', height);
var g = svg.append('g')
.attr('transform', 'translate(' + (width/2) + ',' + (height/2) + ')');
var arc = d3.arc()
.innerRadius(0)
.outerRadius(radius);
var pie = d3.pie()
.value(function(d) { return d.value; })
.sort(null);
var path = g.selectAll('path')
.data(pie(data))
.enter()
.append("g")
.append('path')
.attr('d', arc)
.attr('fill', (d,i) => color(i))
.style('opacity', opacity)
.style('stroke', 'white')
.on("mouseover", function(d) {
d3.selectAll('path')
.style("opacity", otherOpacityOnHover);
d3.select(this)
.style("opacity", opacityHover);
let g = d3.select("svg")
.style("cursor", "pointer")
.append("g")
.attr("class", "tooltip")
.style("opacity", 0);
g.append("text")
.attr("class", "name-text")
.text(`${d.data.key} (${d.data.value})`)
.attr('text-anchor', 'middle');
let text = g.select("text");
let bbox = text.node().getBBox();
let padding = 2;
g.insert("rect", "text")
.attr("x", bbox.x - padding)
.attr("y", bbox.y - padding)
.attr("width", bbox.width + (padding*2))
.attr("height", bbox.height + (padding*2))
.style("fill", "white")
.style("opacity", 0.75);
})
.on("mousemove", function(d) {
let mousePosition = d3.mouse(this);
let x = mousePosition[0] + width/2;
let y = mousePosition[1] + height/2 - tooltipMargin;
let text = d3.select('.tooltip text');
let bbox = text.node().getBBox();
if(x - bbox.width/2 < 0) {
x = bbox.width/2;
}
else if(width - x - bbox.width/2 < 0) {
x = width - bbox.width/2;
}
if(y - bbox.height/2 < 0) {
y = bbox.height + tooltipMargin * 2;
}
else if(height - y - bbox.height/2 < 0) {
y = height - bbox.height/2;
}
d3.select('.tooltip')
.style("opacity", 1)
.attr('transform',`translate(${x}, ${y})`);
})
.on("mouseout", function(d) {
d3.select("svg")
.style("cursor", "none")
.select(".tooltip").remove();
d3.selectAll('path')
.style("opacity", opacity);
})
.on("touchstart", function(d) {
d3.select("svg")
.style("cursor", "none");
})
.each(function(d, i) { this._current = i; });
let legend = d3.select("#chart").append('div')
.attr('class', 'legend')
.style('margin-top', '30px');
let keys = legend.selectAll('.key')
.data(data)
.enter().append('div')
.attr('class', 'key')
.style('display', 'flex')
.style('align-items', 'center')
.style('margin-right', '20px');
keys.append('div')
.attr('class', 'symbol')
.style('height', '10px')
.style('width', '10px')
.style('margin', '5px 5px')
.style('background-color', (d, i) => color(i));
keys.append('div')
.attr('class', 'key')
.text(d => `${d.key} (${d.value})`);
keys.exit().remove();
}
function CreateBarChart(){
var data = ReadDataQuizCompletionCount();
// Set the dimensions of our chart to be displayed
var barsWidth = 500,
barsHeight = 400,
axisMargin = 100;
var chartHeight = barsHeight+axisMargin,
chartWidth = barsWidth+axisMargin;
// Select the chart element on the page so we can reference it in code
// Also set the width and height attributes of the chart SVG
var chart = d3.select('#chart2')
.attr('width', chartWidth+100)
.attr('height', chartHeight);
// Create a linear scale for our y-axis to map datapoint values to pixel heights of bars
var yScale = d3.scaleLinear()
.domain([0,d3.max(data, function(d){
// return the value property of each datapoint so the max function can compare
return d.value;
})])
.rangeRound([barsHeight, 0]);
// Create a scale that returns the bands each bar should be in along the x-axis
let xScale = d3.scaleBand()
.domain(
data.map(
function(d){
// For each datapoint in our data array
// Return the name property into our new domain array
return d.key;
}
)
)
.rangeRound([0,barsWidth])
.padding(0.1);
// Create an SVG group that we will add the individual bar elements of our chart to
var bars = chart.append('g')
.attr('id', "bars-container");
// Bind the data to our .bars svg elements
// Create a rectangle for each data point and set position and dimensions using scales
bars.selectAll('.bar')
.data(data)
.enter().append("rect")
.attr('class', "bar")
.attr('x', function(d){
return xScale(d.key);
})
.attr('y', function(d){
return yScale(d.value);
})
.attr('width', xScale.bandwidth())
.attr('height', function(d){return barsHeight-yScale(d.value);});
// Move the bars so that there is space on the left for the y-axis
bars.attr('transform', 'translate('+axisMargin+',0)');
// Create a new SVG group for the y-axis elements
// Generate the y-axis with 10 ticks and move into position
yAxis = chart.append('g')
.attr('id','y-axis')
.call(d3.axisLeft(yScale).ticks(10))
.attr('transform', 'translate('+axisMargin+',0)');
// Create another group for the x-axis elements
// Generate the x-axis using the our x scale and move into positon
// Select the text elements and rotate by 45 degrees
xAxis = chart.append('g')
.attr('id', 'x-axis')
.call(d3.axisBottom(xScale))
.attr('transform', 'translate('+axisMargin+','+barsHeight+')')
.selectAll("text")
.style("text-anchor",'start')
.attr('transform', 'rotate(45)');
}
CreatePieChart();
CreateBarChart();
});
</script>
</body>
</html>