00001 var n = 120,
00002 duration = 500;
00003
00004 function tick(paths, line, axes, x, y, ids, tag) {
00005 var transition = d3.select(tag).transition()
00006 .duration(duration)
00007 .ease("linear");
00008 transition = transition.each(function () {
00009
00010
00011 now = new Date();
00012 x.domain([now - (n - 1) * duration, now]);
00013
00014 var extent = [0x1000000, 0];
00015 for (var name in ids) {
00016 var group = ids[name];
00017 var thisExtent = d3.extent(group.data, function (d) { return d.value; });
00018 if (thisExtent[0] < extent[0]) { extent[0] = thisExtent[0]; }
00019 if (thisExtent[1] > extent[1]) { extent[1] = thisExtent[1]; }
00020 }
00021 y.domain([extent[0] * 98 / 100, extent[1] * 102 / 100]);
00022
00023 for (var name in ids) {
00024 d3.json(ids[name].jsonPath, function (json) {
00025 ids[json.name].data.push({ value: json.value, time: new Date(json.time) });
00026 });
00027
00028
00029 ids[name].path.attr("d", line).attr("transform", null);
00030 }
00031
00032
00033 axes[0].call(x.axis);
00034 axes[1].call(y.axis);
00035
00036 for (var name in ids) {
00037 ids[name].path
00038 .transition()
00039 .duration(duration)
00040 .ease("linear")
00041 .attr("transform", "translate(" + x(now - n * duration) + ")");
00042 }
00043
00044 for (var name in ids) {
00045 while (ids[name].data.length > 0 && ids[name].data[0].time < now - n * duration) {
00046 ids[name].data.shift();
00047 }
00048 }
00049
00050 }).transition().each("start", function () { setTimeout(tick(paths, line, axes, x, y, ids, tag), (duration * 3) / 4) });
00051 }
00052
00053 function makeGraph(tag, ids) {
00054 var now = new Date(Date.now() - duration);
00055
00056 var margin = { top: 6, right: 20, bottom: 20, left: 60 },
00057 width = $(tag).width() - margin.right - margin.left,
00058 height = 120 - margin.top - margin.bottom;
00059
00060 var x = d3.time.scale()
00061 .range([0, width]);
00062
00063 var y = d3.scale.linear()
00064 .range([height, 0]);
00065
00066 var line = d3.svg.line()
00067 .interpolate("linear")
00068 .x(function (d) { return x(d.time); })
00069 .y(function (d) { return y(d.value); });
00070
00071 var svg = d3.select(tag).append("svg")
00072 .attr("width", width + margin.left + margin.right)
00073 .attr("height", height + margin.top + margin.bottom)
00074 .style("margin-left", -margin.right + "px")
00075 .append("g")
00076 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
00077
00078 svg.append("defs").append("clipPath")
00079 .attr("id", "clip" + tag.replace('#', ""))
00080 .append("rect")
00081 .attr("width", width)
00082 .attr("height", height);
00083
00084 var xaxis = svg.append("g")
00085 .attr("class", "x axis")
00086 .attr("transform", "translate(0," + height + ")")
00087 .call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
00088
00089 var formatTick = function (d) {
00090 var prefix = d3.formatPrefix(d);
00091 return prefix.scale(d) + " " + prefix.symbol + "B/s";
00092 }
00093 var yaxis = svg.append("g")
00094 .attr("class", "y axis")
00095 .attr("transform", "translate(0,0)")
00096 .call(y.axis = d3.svg.axis().scale(y).tickFormat(formatTick).orient("left"));
00097
00098 var paths = svg.append("g")
00099 .attr("clip-path", "url(#clip" + tag.replace('#', "") + ")");
00100
00101 for (var name in ids) {
00102 ids[name].path = paths.append('path')
00103 .data([ids[name].data])
00104 .attr('class', 'line')
00105 .style('stroke', ids[name].color);
00106 }
00107
00108 tick(paths, line, [xaxis, yaxis], x, y, ids, tag);
00109 }