/
line-chart.html
113 lines (96 loc) · 3.22 KB
/
line-chart.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<script src="/bower_components/d3/d3.min.js"></script>
<link rel="stylesheet" type="text/css" href="/bower_components/normalize.css/normalize.css" />
<link rel="stylesheet" type="text/css" href="/assets/css/d3-nationjs.css" />
</head>
<body>
<!-- Line Chart -->
<div>
<div class="line-chart"></div>
</div>
<!-- Line Chart Buttons -->
<div>
<div id="line-chart-button1" class="line-chart-button">Plot Data 1</div>
<div id="line-chart-button2" class="line-chart-button">Plot Data 2</div>
<div id="line-chart-button3" class="line-chart-button">Plot Data 3</div>
</div>
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script>
var data1 = [10, 7, 3, 5, 4, 9, 10, 8],
data2 = [9, 10, 4, 8, 5, 7],
data3 = [4, 7, 10, 8, 9, 4, 8, 5, 8, 8, 6, 5];
// ----- MOAR -----
/*
var data1 = [{'value': 10}, {'value':7}, {'value':3}, {'value':5}, {'value':4}, {'value':9}, {'value':10},{'value': 8}],
data2 = [{'value': 9}, {'value': 10}, {'value': 4}, {'value': 8}, {'value': 5}, {'value': 7}],
data3 = [{'value': 4}, {'value': 7}, {'value': 10}, {'value': 8}, {'value': 9}, {'value': 4}, {'value': 8}, {'value': 5}, {'value': 8}, {'value': 8}, {'value': 6}, {'value': 5}];
*/
function renderGraph(data) {
// ENTER - Build DOM elements we will need. Add constant styling.
d3.select('.line-chart').selectAll('div.line-chart-bar')
.data(data)
.enter()
.append('div')
.classed('line-chart-bar',true);
// UPDATE - Bind DOM elements with data. Add variable styling.
console.log('UPDATE')
d3.select('.line-chart').selectAll('div.line-chart-bar')
.data(data)
.text(function(d,i){
console.log('data: ' + d + ', index: ' + i);
return d;
})
.style('width', function(d,i){
return (d * 50) + 'px';
});
// EXIT - Remove DOM elements not needed.
d3.select('.line-chart').selectAll('div.line-chart-bar')
.data(data)
.exit()
.remove();
/*
// ----- MOAR -----
// ENTER - Build DOM elements we will need. Add constant styling.
d3.select('.line-chart').selectAll('div.line-chart-bar')
.data(data)
.enter()
.append('div')
.classed('line-chart-bar',true);
console.log('UPDATE')
// UPDATE - Bind DOM elements with data. Add variable styling.
d3.select('.line-chart').selectAll('div.line-chart-bar')
.data(data)
.text(function(d,i){
console.log('data: ' + d.value + ', index: ' + i);
return d.value;
})
.style('width', function(d,i){
return (d.value * 50) + 'px';
});
// EXIT - Remove DOM elements not needed.
d3.select('.line-chart').selectAll('div.line-chart-bar')
.data(data)
.exit()
.remove();
*/
}
// Bind render events to buttons
d3.select('#line-chart-button1').on('click',function(e){
renderGraph(data1);
});
d3.select('#line-chart-button2').on('click',function(e){
renderGraph(data2);
});
d3.select('#line-chart-button3').on('click',function(e){
renderGraph(data3);
});
</script>
</body>
</html>