/
grid-line.html
82 lines (66 loc) · 2.24 KB
/
grid-line.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Axis Grid Lines</title>
<link rel="stylesheet" type="text/css" href="../../css/styles.css"/>
<script type="text/javascript" src="../../lib/d3.js"></script>
</head>
<body>
<script type="text/javascript">
var height = 500,
width = 500,
margin = 25;
var svg = d3.select("body").append("svg")
.attr("class", "axis")
.attr("width", width)
.attr("height", height);
function renderXAxis(){
var axisLength = width - 2 * margin;
var scale = d3.scale.linear()
.domain([0, 100])
.range([0, axisLength]);
var xAxis = d3.svg.axis()
.scale(scale)
.orient("bottom");
svg.append("g")
.attr("class", "x-axis")
.attr("transform", function(){ // <-A
return "translate(" + margin + "," + (height - margin) + ")";
})
.call(xAxis);
d3.selectAll("g.x-axis g.tick") // <-B
.append("line") // <-C
.classed("grid-line", true)
.attr("x1", 0) // <-D
.attr("y1", 0)
.attr("x2", 0)
.attr("y2", - (height - 2 * margin)); // <-E
}
function renderYAxis(){
var axisLength = height - 2 * margin;
var scale = d3.scale.linear()
.domain([100, 0])
.range([0, axisLength]);
var yAxis = d3.svg.axis()
.scale(scale)
.orient("left");
svg.append("g")
.attr("class", "y-axis")
.attr("transform", function(){
return "translate(" + margin + "," + margin + ")";
})
.call(yAxis);
d3.selectAll("g.y-axis g.tick")
.append("line")
.classed("grid-line", true)
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", axisLength) // <-F
.attr("y2", 0);
}
renderYAxis();
renderXAxis();
</script>
</body>
</html>