-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.html
146 lines (116 loc) · 3.71 KB
/
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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ODSC London - workshop introduction D3</title>
<meta name="description" content="ODSC London - workshop introduction D3">
<meta name="author" content="TULP interactive">
<style>
</style>
</head>
<body>
<button class="update">update</button>
<script src="js/d3.min.js" lang="Javascript"></script>
<script lang="Javascript">
var margin = {
left: 50,
right: 50,
top: 50,
bottom: 50
}
var width = 800 - margin.left - margin.right
var height = 400 - margin.top - margin.bottom
function generateData() {
var multiplier = Math.random() * 50
return d3.range(3)
.map(function(d) { return d3.range(10)
.map(function(d, i) { return Math.random() * multiplier })})
}
var data = generateData()
var svg = d3.select('body').append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')')
var x = d3.scaleLinear()
.domain([0, data[0].length])
.range([0, width])
function getYDomainExtent(data) {
return d3.extent(data
.reduce(function(acc, curr, prev) {
return acc.concat(curr)}, []))
}
var y = d3.scaleLinear()
.domain(getYDomainExtent(data))
.range([height, 0])
var c = d3.scaleOrdinal()
.range(d3.schemeCategory10)
var line = d3.line()
.x(function(d, i) { return x(i) })
.y(function(d) { return y(d) })
.curve(d3.curveLinear)
var xAxis = d3.axisBottom()
.scale(x)
svg.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0, ' + (height + 20) + ')')
.call(xAxis)
var yAxis = d3.axisLeft()
.scale(y)
svg.append('g')
.attr('class', 'y axis')
.attr('transform', 'translate(-20, 0)')
.call(yAxis)
function render() {
var result = svg.selectAll('.result')
.data(data)
var resultEnter = result.enter().append('g')
.attr('class', 'result')
resultEnter.append('path')
.style('fill', 'none')
.style('stroke', function(d, i) { return c(i) })
.style('stroke-dasharray', function(d, i) { return (i * 2) + ' ' + (i) })
.style('stroke-width', 3)
var resultMerge = resultEnter.merge(result)
resultMerge.select('path')
.transition(t)
.duration(800)
.delay(function(d, i) { return i * 50 })
.attr('d', line)
resultMerge.each(function(result, i) {
var mark = d3.select(this).selectAll('.mark')
.data(function(d) { return d }, function(d, i) { return i})
mark.transition()
.duration(800)
.attr('cx', function(d, i) { return x(i) })
.attr('cy', function(d) { return y(d) })
var markEnter = mark.enter().append('circle')
.attr('class', 'mark')
.attr('r', 0)
.style('stroke', c(i))
.style('fill', 'white')
.style('stroke-width', 3)
.attr('cx', function(d, i) { return x(i) })
.attr('cy', function(d) { return y(d) })
.transition()
.duration(800)
.delay(function(d, i) { return i * 100 })
.attr('r', 5)
})
var t = d3.transition()
.duration(800)
d3.select('.y')
.transition(t)
.call(yAxis)
}
render()
var button = d3.select('.update')
.on('click', function() {
data = generateData()
y.domain(getYDomainExtent(data))
yAxis.scale(y)
render()
})
</script>
</body>
</html>