improved graphs - the range is now based on the current time instead of the last...
[p2pool.git] / web-static / graphs.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <title>P2Pool Graphs</title>
5         <script type="text/javascript" src="d3.v2.min.js"></script>
6         
7         <style type="text/css">
8             body {
9                 font-family: Sans-serif;
10                 font-size: 12px;
11             }
12             
13             line {
14                 stroke: black;
15                 stroke-width: 1;
16                 shape-rendering: crispEdges;
17             }
18             
19             .rate {
20                 stroke: #0000FF;
21                 stroke-width: 1.4;
22                 fill: none;
23             }
24             
25             .deadrate {
26                 stroke: #FF0000;
27                 stroke-width: 1.4;
28                 fill: none;
29             }
30             
31             text {
32                 font-family: Sans-serif;
33                 font-size: 12px;
34             }
35         </style>
36     </head>
37     
38     <body>
39         <h1>P2Pool Graphs</h1>
40         
41         <h2>Last hour</h1>
42         <div id="graph_hour"></div>
43         
44         <h2>Last day</h1>
45         <div id="graph_day"></div>
46         
47         <h2>Last week</h1>
48         <div id="graph_week"></div>
49         
50         <h2>Last month</h1>
51         <div id="graph_month"></div>
52         
53         <script type="text/javascript">
54             function getData(url) {
55                 xmlhttp = new XMLHttpRequest();
56                 xmlhttp.open("GET",  url, false);
57                 xmlhttp.send();
58                 var data = JSON.parse(xmlhttp.responseText);
59                 
60                 return data;
61             }
62             
63             function plot(e, url) {
64                 data = getData("/web/graph_data/local_hash_rate/" + url);
65                 dead_data = getData("/web/graph_data/local_dead_hash_rate/" + url);
66                 
67                 var w = 640;
68                 var h = 300;
69                 var margin = 50;
70                 
71                 function itemgetter(i) {
72                     return function(x) {
73                         return x[i];
74                     }
75                 }
76                 function as_date(x) {
77                     return new Date(1000*x);
78                 }
79                 
80                 var x = d3.time.scale().domain([as_date(d3.min(data, itemgetter(0))), as_date(d3.max(data, itemgetter(0)))]).range([0 + margin * 2, w - margin * 2]);
81                 var y = d3.scale.linear().domain([0, d3.max(data, itemgetter(1))]).range([h - margin, margin]);
82                 
83                 var line = d3.svg.line().x(function(d, i) { return x(as_date(d[0])); }).y(function(d) { return y(d[1]); });
84                 
85                 e.selectAll("svg").remove();
86                 var g = e.insert("svg:svg").attr("width", w).attr("height", h);
87                 
88                 g.append("svg:path")
89                     .attr("d", line(data))
90                     .attr("class", "rate");
91                 
92                 g.append("svg:path")
93                     .attr("d", line(dead_data))
94                     .attr("class", "deadrate");
95                 
96                 
97                 g.append("svg:line")
98                     .attr("x1", margin * 2)
99                     .attr("y1", h - margin)
100                     .attr("x2", w - margin * 2)
101                     .attr("y2", h - margin);
102                 
103                 g.selectAll()
104                     .data(x.ticks(13))
105                     .enter().append("svg:g")
106                     .attr("transform", function(d) { return "translate(" + x(d) + "," + (h-margin/2) + ")"; })
107                     .append("svg:text")
108                     .attr("transform", "rotate(45)")
109                     .attr("text-anchor", "middle")
110                     .text(x.tickFormat(13));
111                 
112                 g.selectAll()
113                     .data(x.ticks(13))
114                     .enter().append("svg:line")
115                     .attr("x1", x)
116                     .attr("y1", h - margin)
117                     .attr("x2", x)
118                     .attr("y2", h - margin + 5);
119                 
120                 
121                 g.append("svg:line")
122                     .attr("x1", margin * 2)
123                     .attr("y1", h - margin)
124                     .attr("x2", margin * 2)
125                     .attr("y2", margin);
126                 
127                 g.selectAll()
128                     .data(y.ticks(18))
129                     .enter().append("svg:line")
130                     .attr("x1", margin * 2 - 5)
131                     .attr("y1", y)
132                     .attr("x2", margin * 2)
133                     .attr("y2", y);
134                 
135                 g.selectAll()
136                     .data(y.ticks(6))
137                     .enter().append("svg:text")
138                     .text(d3.format(".3s"))
139                     .attr("x", margin - 2.5)
140                     .attr("y", function(d) { return y(d) + 4; })
141                     .attr("text-anchor", "middle");
142             }
143             
144             function attachGraph(e, url) {
145                 plot(e, url);
146                 //setInterval(function() { plot(e, url); }, 5000);
147             }
148             
149             attachGraph(d3.select("#graph_hour"), "last_hour");
150             attachGraph(d3.select("#graph_day"), "last_day");
151             attachGraph(d3.select("#graph_week"), "last_week");
152             attachGraph(d3.select("#graph_month"), "last_month");
153         </script>
154     </body>
155 </html>