-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxis.html
76 lines (68 loc) · 2.06 KB
/
axis.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var height = 500,
width = 500,
margin = 25,
xAxis, yAxis, xAxisLength, yAxisLength;
var svg = d3.select('body').append('svg')
.attr('class', 'axis')
.attr('width', width)
.attr('height', height);
function renderXAxis() {
xAxisLength = width - 2*margin;
var scale = d3.scaleLinear().domain([0,100]).range([0, xAxisLength]);
xAxis = d3.axisBottom().scale(scale).ticks(10);
svg.append('g')
.attr('class', 'x-axis')
.attr('transform','translate(' + margin + ',' + (height - margin) + ')')
.call(xAxis);
d3.selectAll('g.x-axis g.tick')
.append('line')
.attr('stroke-width',1)
.attr('stroke', '#eeeeee')
.classed('grid-line', true)
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', 0)
.attr('y2', -(height-2*margin));
}
function renderYAxis() {
yAxisLength = height - 2*margin;
var scale = d3.scaleLinear().domain([100,0]).range([0, yAxisLength]);
yAxis = d3.axisLeft().scale(scale).ticks(10);
svg.append('g')
.attr('class','y-axis')
.attr('transform', 'translate(' + margin + ',' + margin + ')')
.call(yAxis);
d3.selectAll('g.y-axis g.tick')
.append('line')
.attr('stroke', '#eee')
.attr('stroke-width', 1)
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', yAxisLength)
.attr('y2', 0);
}
function rescale() {
var max = Math.round(Math.random()*100);
xAxis.scale().domain([0,max]);
svg.select('g.x-axis')
.transition()
.call(xAxis);
svg.select('g.y-axis')
.transition()
.call(yAxis);
}
renderXAxis();
renderYAxis();
setTimeout(rescale,2000);
</script>
</body>
</html>