-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.html
90 lines (79 loc) · 2.23 KB
/
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
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div.h-bar { height:20px; background: blue; margin-bottom:5px }
</style>
</head>
<body>
<!--# include file="a.html" -->
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var width = 500,
height = 500,
margin = 25,
xScale, yScale,
svg = d3.select('body').append('svg'),
xAxis, yAxis, xAxisLength, yAxisLength;
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},
],
d3.range(10).map(function(d){
return {x:d, y:Math.sin(d) + 5}
})
];
xScale = d3.scaleLinear().domain([0,10]).range([25, width - 25]);
yScale = d3.scaleLinear().domain([10,0]).range([25, height - 25]);
xAxis = d3.axisBottom().scale(xScale).ticks(10);
yAxis = d3.axisLeft().scale(yScale).ticks(10);
svg.attr('width', width)
.attr('height', height)
.append('g')
.attr('class', 'x-axis')
.attr('transform', 'translate(' + margin + ',' + (height - margin) + ')')
.call(xAxis);
svg.append('g')
.attr('class', 'y-axis')
.attr('transform', 'translate(' + margin + ',' + margin + ')')
.call(yAxis);
d3.selectAll('.x-axis .tick')
.append('line')
.attr('stroke', '#ccc')
.attr('stroke-width', 1)
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', 0)
.attr('y2', -(height-50));
d3.selectAll('.y-axis .tick')
.append('line')
.attr('stroke', '#ccc')
.attr('stroke-width', 1)
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', width-50)
.attr('y2', 0);
var line = d3.line()
.x(function(d){
return xScale(d.x);
})
.y(function(d){
return yScale(d.y);
})
.curve(d3.curveLinear);
svg.selectAll('path.line')
.data(data)
.enter()
.append('path')
// .attr('stroke', '#ddd')
// .attr('stroke-width', 1)
.attr('class', 'line')
.attr('d', function(d) {
return line(d);
})
</script>
</body>
</html>