-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0_tutorial.html
83 lines (69 loc) · 1.96 KB
/
0_tutorial.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
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
<title>0_tutorial</title>
</head>
<body>
<div id="myDiv1">
Here is some text.
</div>
<ul id="list1">
<li></li>
<li></li>
</ul>
<!-- Just illustrating how SVG works: -->
<div id="myDiv2">
<svg width = "300" height = "300">
<line x1 = "100" y1 = "100"
x2 = "200" y2 = "200" style = "stroke:rgb(255,0,0);
stroke-width:2"/>
</svg>
</div>
<div id="myDiv3">
</div>
<!-- The <g> tag groups together a bunch of graphics: -->
<svg width = "300" height = "300">
<g transform = "translate(65,65), rotate(30)">
<rect x = "20"
y = "20"
width = "60"
height = "30"
fill = "green">
</rect>
<circle cx = "0"
cy = "0"
r = "30"
fill = "red"/>
</g>
</svg>
</body>
<script>
// We can use select() to select an element
// Let's use select() to get the text from myDiv1, and print it to the console!
console.log(d3.select("#myDiv1").text());
// Let's use D3 to append a new element to the DOM
d3.select("body").append("div").text("New Text");
//Data Join
//Since list1 only has 2 "li" elements, only 2 of the 5 elements of the array get joined to it
d3.select("#list1")
.selectAll("li")
.data([10, 20, 30, 25, 15])
.text(function(d) { return "This is pre-existing element and the value is " + d; })
.enter()
.append("li")
.text(function(d) { return "This is dynamically created element and the value is " + d; });
//Adding Graphics Using D3
d3.select("#myDiv3")
.append("svg")
.attr("width", 300)
.attr("height", 300)
.append("line")
.attr("x1", 100)
.attr("y1", 100)
.attr("x2", 200)
.attr("y2", 200)
.style("stroke", "rgb(255,0,0)")
.style("stroke-width", 2);
</script>
</html>