Advertisement
Guest User

Javascript

a guest
Nov 30th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.29 KB | None | 0 0
  1. <html>
  2.  
  3. <style>
  4.  
  5. body {
  6.   font: 10px sans-serif;
  7. }
  8.  
  9. .axis path,
  10. .axis line {
  11.   fill: none;
  12.   stroke: #000;
  13.  shape-rendering: crispEdges;
  14. }
  15.  
  16. .x.axis path {
  17.   display: none;
  18. }
  19.  
  20. .area.above {
  21.   fill: rgb(252,141,89);
  22. }
  23.  
  24. .area.below {
  25.   fill: rgb(145,207,96);
  26. }
  27.  
  28. .line {
  29.   fill: none;
  30.   stroke: #000;
  31.  stroke-width: 1.5px;
  32. }
  33.  
  34. </style>
  35. <body>
  36. <script src="d3.v3.min.js"></script>
  37.  
  38. <script>
  39.  
  40. var format = d3.time.format("%Y-%m-%d");
  41.  
  42. var margin = {top: 20, right: 20, bottom: 30, left: 50},
  43.     width  = 960 - margin.left - margin.right,
  44.     height  = 500 - margin.top - margin.bottom;
  45.  
  46. var xstream  = d3.time.scale()
  47.     .range([0, width]);
  48.  
  49. var ystream = d3.scale.linear()
  50.     .range([height, 0]);
  51.  
  52. var xAxisstream = d3.svg.axis()
  53.     .scale(xstream)
  54.     .orient("bottom");
  55.  
  56. var yAxisstream = d3.svg.axis()
  57.     .scale(ystream)
  58.     .orient("left");
  59.  
  60. var linestream = d3.svg.area()
  61.     .interpolate("basis")
  62. .x(function(dstream) { return xstream(dstream["unixtime"]); })
  63.     .y(function(dstream) { return ystream(dstream["stream"]); });
  64.  
  65. var areastream = d3.svg.area()
  66.     .interpolate("basis")
  67.     .x(function(dstream) { return xstream(dstream["unixtime"]); })
  68.     .y1(function(dstream) { return ystream(dstream["stream"]); });
  69.  
  70. var svgstream = d3.select("body").append("svg")
  71.     .attr("width", width + margin.left + margin.right)
  72.     .attr("height", height + margin.top + margin.bottom)
  73.   .append("g")
  74.     .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  75.  
  76. d3.csv("streamvalues.csv", function(errorstream, datastream) {
  77.   datastream.forEach(function(dstream) {
  78.     dstream["unixtime"] = format(new Date(dstream["unixtime"]));
  79.     dstream["stream"]= +dstream["stream"];
  80.     dstream["stream-7.5%"]= +dstream["stream"] * .925;
  81.     dstream["stream+7.5%"]= +dstream["stream"] * .1075;
  82.   });
  83.  
  84.   xstream.domain(d3.extent(datastream, function(dstream) { return dstream["unixtime"] }));
  85.  
  86.   ystream.domain([
  87.     d3.min(datastream, function(dstream) { return dstream["stream-7.5%"]  }),
  88.     d3.max(datastream, function(dstream) { return dstream["stream+7.5%"] }),
  89.   ]);
  90.  
  91.   svgstream.datum(datastream);
  92.  
  93.   svgstream.append("clipPath")
  94.       .attr("id", "clip-below")
  95.     .append("path")
  96. .attr("dstream", areastream.y0(height));
  97.  
  98.   svgstream.append("clipPath")
  99.       .attr("id", "clip-above")
  100.     .append("path")
  101.       .attr("dstream", areastream.y0(0));
  102.  
  103.   svgstream.append("path")
  104.       .attr("class", "area above")
  105.       .attr("clip-path", "url(#clip-above)")
  106.       .attr("dstream", areastream.y0(function(dstream) { return ystream(dstream["stream-7.5%"]); }));
  107.  
  108.   svgstream.append("path")
  109.       .attr("class", "area below")
  110.       .attr("clip-path", "url(#clip-below)")
  111.       .attr("dstream", areastream);
  112.  
  113.   svgstream.append("path")
  114.       .attr("class", "line")
  115.       .attr("dstream", linestream);
  116.  
  117.   svgstream.append("g")
  118.       .attr("class", "x axis")
  119.       .attr("transform", "translate(0," + height + ")")
  120.       .call(xAxisstream);
  121.  
  122.   svgstream.append("g")
  123.       .attr("class", "y axis")
  124.       .call(yAxisstream)
  125.     .append("text")
  126.       .attr("transform", "rotate(-90)")
  127.       .attr("ystream", 6)
  128.       .attr("dystream", ".71em")
  129.       .style("text-anchor", "end")
  130.       .text("Mbps");
  131. });
  132. </script>
  133. </body>
  134. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement