- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline_tutorial.html
More file actions
Latest commit
85 lines (67 loc) · 1.88 KB
/
Copy pathline_tutorial.html
File metadata and controls
85 lines (67 loc) · 1.88 KB
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
<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8">
<styletype="text/css">
/* set the CSS */
.line {
fill: none;
stroke: steelblue;
stroke-width:2px;
}
</style>
<title></title>
</head>
<body>
<!-- load the d3.js library -->
<scriptsrc="https://d3js.org/d3.v4.min.js" type="text/javascript"></script>
<scripttype="text/javascript">
// set the dimensions and margins of the graph
varmargin={top: 20,right: 20,bottom: 30,left: 50},
width=960-margin.left-margin.right,
height=500-margin.top-margin.bottom;
// parse the date / time
varparseTime=d3.timeParse("%d-%b-%y");
// set the ranges
varx=d3.scaleTime().range([0,width]);
vary=d3.scaleLinear().range([height,0]);
// define the line
varvalueline=d3.line()
.x(function(d){returnx(d.date);})
.y(function(d){returny(d.close);});
// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
varsvg=d3.select("body").append("svg")
.attr("width",width+margin.left+margin.right)
.attr("height",height+margin.top+margin.bottom)
.append("g")
.attr("transform",
"translate("+margin.left+","+margin.top+")");
// Get the data
d3.csv("data/line_data.csv",function(error,data){
if(error)throwerror;
// format the data
data.forEach(function(d){
d.date=parseTime(d.date);
d.close=+d.close;
});
// Scale the range of the data
x.domain(d3.extent(data,function(d){returnd.date;}));
y.domain([0,d3.max(data,function(d){returnd.close;})]);
// Add the valueline path.
svg.append("path")
.data([data])
.attr("class","line")
.attr("d",valueline);
// Add the X Axis
svg.append("g")
.attr("transform","translate(0,"+height+")")
.call(d3.axisBottom(x));
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y));
});
</script>
</body>
</html>