-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics-d3js.demo.html
163 lines (127 loc) · 3.78 KB
/
metrics-d3js.demo.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
margin: 10px 50px;
}
svg {
font: 10px sans-serif;
}
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<input type="text" name="host" value="localhost:9000"/>
<span id="value"></span>
<div id="metrics_selection"/>
<script>(function() {
var n = 300,
duration = 1000,
now = new Date(Date.now() - duration),
data = d3.range(n).map(function() { return 0; });
var random = d3.random.normal(0, .2);
var margin = {top: 6, right: 0, bottom: 20, left: 40},
width = 960 - margin.right,
height = 120 - margin.top - margin.bottom;
var x = d3.time.scale()
.domain([now - (n - 2) * duration, now - duration])
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var line = d3.svg.line()
.interpolate("basis")
.x(function(d, i) { return x(now - (n - 1 - i) * duration); })
.y(function(d, i) { return y(d); });
var svg = d3.select("body").insert("p", "#metrics_selection").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("margin-left", -margin.left + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var axis = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
var yAxis = svg.append("g")
.attr("class", "y axis")
.call(y.axis = d3.svg.axis().scale(y).ticks(5).tickFormat(d3.format("s")).orient("left"));
var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.data([data])
.attr("class", "line");
tick();
function update(value) {
// update the domains
now = new Date();
x.domain([now - (n - 2) * duration, now - duration]);
y.domain([0, d3.max(data)]);
data.push(value);
// redraw the line
svg.select(".line")
.attr("d", line)
.attr("transform", null);
// slide the x-axis left
axis.transition()
.duration(duration)
.ease("linear")
.call(x.axis);
yAxis.call(y.axis);
// slide the line left
path.transition()
.duration(duration)
.ease("linear")
.attr("transform", "translate(" + x(now - (n - 1) * duration) + ")")
.each("end", tick);
// pop the old data point off the front
data.shift();
}
previousCounterValues = d3.map({})
function tick() {
host = d3.select("input[name='host']").property("value")
url = "http://" + host + "/metrics.json"
d3.json(url, function(metrics) {
metrics = d3.map(metrics)
metrics.forEach(function(key, value) {
if (d3.select("#metrics_selection input[value='" + key + "']").empty()) {
metricsSelector = d3.select("#metrics_selection").append("p").text(key)
metricsSelector.insert("input").attr("type","radio").attr("name", "selected_metric").attr("value",key)
}
})
checkedMetric = d3.select("#metrics_selection input:checked")
if (!checkedMetric.empty()) {
key = checkedMetric.attr("value")
metric = d3.map(metrics.get(key))
value = 0
if (metric.has("value")) {
value = metric.get("value")
} else {
currentCount = metric.get("count")
if (previousCounterValues.has(key)) {
value = currentCount - previousCounterValues.get(key)
}
previousCounterValues.set(key, currentCount)
}
d3.select("#value").text(value)
update(value)
} else {
update(0);
}
});
}
})()</script>