This repository was archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample3.html
More file actions
96 lines (88 loc) · 2.53 KB
/
Copy pathexample3.html
File metadata and controls
96 lines (88 loc) · 2.53 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
<html>
<head>
<title>MAS.S70 | D3 workshop</title>
<link href="https://fonts.googleapis.com/css?family=PT+Sans" rel="stylesheet">
<style>
body {
margin: 0;
padding: 3%;
font-family: 'PT Sans', sans-serif;
}
.page {
margin: 0 auto;
max-width: 800px;
width: 90%;
}
h1 {
font-weight: normal;
text-rendering: optimizeLegibility;
}
.chart rect {
fill: rgba(3, 168, 124, 0.8);
}
.chart text {
fill: white;
font: 10px sans-serif;
text-anchor: end;
}
</style>
</head>
<body>
<div class="page">
<h1>Constructing random data | Loops</h1>
<br />
<h2> What does a loop do? </h2>
<p>
Let's log every number between 1 and 100 in the console!
</p>
<br />
<h2> Introducing the <i>"if"</i> statement </h2>
<p>
Now, let's log every number except 42. For 42, we log
"The answer to Life, the Universe and Everything" instead.
</p>
<br />
<h2> Generating random data for our chart </h2>
<p>
Can we use what we've learned to generate random data for our bar chart?
</p>
<svg id="svg1"></svg>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// Log all numbers
var numberList = []
// Your loop
console.log(numberList)
// Log all numbers except 42
var numberList2 = []
// Your loop
console.log(numberList2)
// Generate random data and it will be plotted in our chart
var data = [];
// Your loop to generate random data (Math.random, Math.floor)
// Our old bar chart code
var width = 420;
var barHeight = 20;
var x = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, width]);
var chart1 = d3.select("#svg1")
.attr("width", width)
.attr("height", barHeight * data.length)
.attr("class", "chart");
var bar1 = chart1.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });
bar1.append("rect")
.attr("width", function(d, i) {return x(d)})
.attr("height", barHeight - 1);
bar1.append("text")
.attr("x", function(d) { return x(d) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d) { return d; });
</script>
</body>
</html>