-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxis2.html
86 lines (73 loc) · 2.29 KB
/
axis2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
html, body { padding:0; margin:0; }
</style>
</head>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var width = 500,
height = 500,
margin = 25;
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height)
var scaleX = d3.scaleLinear().domain([0, 10])
.range([0, width - 2 * margin]),
scaleY = d3.scaleLinear().domain([0, 10])
.range([-(height - 2*margin), 0]);
var axisX = d3.axisBottom().scale(scaleX),
axisY = d3.axisLeft().scale(scaleY);
svg.append('g')
.attr('class', 'xAxis')
.attr('transform', 'translate(' + margin + ',' + (height - margin) + ')')
.call(axisX);
svg.append('g')
.attr('class', 'yAxis')
.attr('transform', 'translate(' + margin + ',' + (height - margin) + ')')
.call(axisY);
d3.selectAll('.xAxis .tick')
.append('line')
.attr('stroke-width', 1)
.attr('stroke', '#eee')
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', 0)
.attr('y2', -(height - 2 * margin));
d3.selectAll('.yAxis .tick')
.append('line')
.attr('stroke-width', 1)
.attr('stroke', '#eee')
.attr('x1', 0)
.attr('y1', 0)
.attr('y2', 0)
.attr('x2', (width - 2 * margin))
var data = [
{x: 0, y: 5}, {x: 1, y: 9}, {x: 2, y: 7},
{x: 3, y: 5}, {x: 4, y: 3}, {x: 6, y: 4},
{x: 7, y: 2}, {x: 8, y: 3}, {x: 9, y: 2}
];
var line = d3.line()
.x(function(d){
return scaleX(d.x);
})
.y(function(d) {
return scaleY(d.y);
});
svg.selectAll('path.line')
.data(data)
.enter()
.append('path')
.attr('class', 'line');
svg.selectAll('path.line')
.data(data)
.attr('d', function(d) {
return line(d);
})
</script>
</body>
</html>