-
Notifications
You must be signed in to change notification settings - Fork 1
/
d3-temp.html
112 lines (96 loc) · 3.99 KB
/
d3-temp.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<div xmlns="http://www.w3.org/1999/xhtml" data-template="templates:surround" data-template-with="templates/page.html" data-template-at="content">
<div class="row" data-template="app:init">
<div class="col-md-12">
<div class="page-header">
<h1 data-template="config:app-title"/>
</div>
<div class="row">
<div class="col-md-12">
<h3>D3 Playground</h3>
<div id="line-chart" width="960" height="500"/>
<div>line chart end</div>
</div>
</div>
</div>
</div>
<script>
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// parse the date / time
// var parseTime = d3.timeParse("%Y");
//var dateTimeFormat = d3.timeFormat('%Y-%m-%dT%H:%M:%S');
// var parseTime = dateTimeFormat.parse();
// var parseTime = d3.timeFormat.iso();
// set the ranges
// var x = d3.scaleTime().range([0, width]);
// var y = d3.scaleLinear().range([height, 0]);
var x = d3.scaleTime().rangeRound([0, width]);
var y = d3.scaleLinear().rangeRound([height, 0]);
// define the line
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temp); });
// define the line
// append the svg obgect to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#line-chart").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 + ")");
function draw(data, temperature) {
var data = data[temperature];
// format the data
data.forEach(function(d) {
d.date = d3.isoParse(d.date);
d.temp = +d.temp;
});
// sort years ascending
data.sort(function(a, b){
return a["date"]-b["date"];
})
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
// y.domain([0, d3.max(data, function(d) { return d.temp; })]);
y.domain(d3.extent(data, function(d) { return d.temp; }));
// Add the valueline path.
svg.append("path")
.data([data])
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", valueline);
// Add the valueline path.
// Add the X Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x)).append("text")
.attr("fill", "#000")
.attr("x", 20)
.attr("y", -10)
.text("Date");
// Add the Y Axis
svg.append("g")
.call(d3.axisLeft(y)).append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("x", -60)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Temperature (C°)");
}
// Get the data
d3.json("data/data-Temperature.json", function(error, data) {
if (error) throw error;
// trigger render
draw(data, "Temperature");
});
</script>
</div>