-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataBinding.html
More file actions
executable file
·123 lines (113 loc) · 4.19 KB
/
Copy pathdataBinding.html
File metadata and controls
executable file
·123 lines (113 loc) · 4.19 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
<!DOCTYPE html>
<html>
<head>
<meta name="generator"
content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" />
<title>Data Binding</title>
<link rel="stylesheet" type="text/css" href="allStyle.css" />
<link rel="stylesheet" type="text/css" href="https://codemirror.net/theme/solarized.css" />
<script src="http://d3js.org/d3.v3.min.js"></script>
<link rel="stylesheet" href="CodeMirror/lib/codemirror.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.17/require.min.js"></script>
<script src="dataBinding.js"></script>
</head>
<body>
<aside>
<ul>
<li>
<a href="main.html">Home</a>
</li>
<li>
<a href="start.html">Getting Started</a>
</li>
<li>
<a href="svg.html">SVG</a>
</li>
<li>
<a href="dataBinding.html">Data Binding</a>
</li>
<li>
<a href="lineGraph.html">Line Graph</a>
</li>
</ul>
</aside>
<section>
<header>Data Binding</header>
<p> Data Binding is how D3 automatically genereates SVG/DOM elements. D3 binds data with DOM elements and will
update your page based on changing data. This is possible by using D3's <a href = "https://github.com/mbostock/d3/wiki/API-Reference#d3-core">Selection module</a>.
The following example shows how D3 can create circle SVG objects automatically based on the array of circle objects stored in the variable data. The functions
that are used in this example are the following;
<br/>
<br/>.select() - as stated by the <a href = "https://github.com/mbostock/d3/wiki/Selections">d3 wiki page</a> "you can select by tag ("div"), class (".awesome"), unique identifier ("#foo"), attribute ("[color=red]"),
or containment ("parent child"). Selectors can also be intersected (".this.that" for logical AND) or unioned (".this, .that" for logical OR)",
<br/>
<br/>
.selectAll()
<br/>
<br/>
.data()
<br/>
<br/>
.enter()
<br/>
<br/>
.append()
<br/>
<br/>
.exit()
<br/>
<br/>
If you run the following code, you will see three pink circles. The data was generated by creating an array of circle objects. Note that anonymous functions are used in
order to access the individual objects in the array.
<img name = "images" src= "dataBinding/3circles.png" style="width:150px; height:240px"/>
<div class="outerDiv">
<textarea rows="4" cols="20" id="db1">
<svg id="isSvg" height = 400 width = 400></svg>
<script>
function circle (x,y,r) {
this.x = x;
this.y = y;
this.radius=r;
}
var data = [new circle(100,30,20), new circle(100,70,30), new circle(100,120,40)];
console.log(data);
var svg = d3.select("#isSvg").selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(circle){return circle.x;})
.attr("cy", function(circle){return circle.y;})
.attr("r", function(circle){return circle.radius;})
.style("fill", "pink");
</script>
</textarea>
</div>
<p> Now we will add another circle object "new circle(100,270,50)" to the data array.
The data is the only code that has been modified, so with the same code, another circle is generated.</p>
<div class="outerDiv">
<textarea rows="4" cols="20" id="db2">
<svg id="isSvg" height = 400 width = 400></svg>
<script>
function circle (x,y,r) {
this.x = x;
this.y = y;
this.radius=r;
}
var data = [new circle(100,30,20), new circle(100,90,30),
new circle(100,170,40), new circle(100,270,50)];
console.log(data);
var svg = d3.select("#isSvg").selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(circle){return circle.x;})
.attr("cy", function(circle){return circle.y;})
.attr("r", function(circle){return circle.radius;})
.style("fill", "pink");
</script>
</textarea>
</div>
<img name = "images" src= "dataBinding/4circles.png" style="width:150px; height:240px""/>
</section>
</body>
</html>