-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale-liner-range.html
87 lines (74 loc) · 1.71 KB
/
scale-liner-range.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<svg id="s"></svg>
<script>
var data = [
{ x: 0, y: 1.89 },
{ x: 1, y: 2.77 },
{ x: 2, y: 0.86 },
{ x: 3, y: 3.45 },
{ x: 4, y: 4.13 },
{ x: 5, y: 3.59 },
{ x: 6, y: 2.33 },
{ x: 7, y: 3.79 },
{ x: 8, y: 2.61 },
{ x: 9, y: 2.15 }
];
var width = 240,
height = 120;
var s = d3.select('#s');
s.attr({
'width': width,
'height': height,
}).style({
'border': '1px solid #000'
});
//range()
var scaleX1 = d3.scale.linear()
.range([0, width])
.domain([0, 9]);
var scaleY1 = d3.scale.linear()
.range([120.967, 0.678])
.domain([0, 5]);
var line1 = d3.svg.line()
.x(function(d) {
return scaleX1(d.x);
}).y(function(d) {
return scaleY1(d.y);
});
//rangeRound()
var scaleX2 = d3.scale.linear()
.rangeRound([0, width])
.domain([0, 9]);
var scaleY2 = d3.scale.linear()
.rangeRound([120.967, 0.678])
.domain([0, 5]);
var line2 = d3.svg.line()
.x(function(d) {
return scaleX2(d.x);
}).y(function(d) {
return scaleY2(d.y);
});
s.append('path')
.attr({
'd': line1(data),
'stroke': '#09c',
'fill': 'none'
});
s.append('path')
.attr({
'd': line2(data),
'stroke': 'red',
'fill': 'none'
});
</script>
</body>
</html>