Skip to content

Instantly share code, notes, and snippets.

@phil-pedruco
Created December 29, 2014 17:03
Show Gist options
  • Select an option

  • Save phil-pedruco/e927fefcce408ddbbe6d to your computer and use it in GitHub Desktop.

Select an option

Save phil-pedruco/e927fefcce408ddbbe6d to your computer and use it in GitHub Desktop.

Revisions

  1. phil-pedruco created this gist Dec 29, 2014.
    8 changes: 8 additions & 0 deletions data.csv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    client_ip,timestamp,bytes_transfered
    92.239.29.77,1412109000000,577818
    92.239.29.77,1412110830000,108257
    92.239.29.77,1412112600000,20922726
    92.239.29.77,1412132430000,3190
    92.239.29.78,1412109000000,57818
    92.239.29.78,1412110830000,10257
    92.239.29.78,1412112600000,2022726
    112 changes: 112 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Timeseries</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
    <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <style type="text/css">
    body {
    font: 10px sans-serif;
    }
    .axis path,
    .axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
    }
    .x.axis path {
    display: none;
    }
    .line {
    fill: none;
    stroke: steelblue;
    stroke-width: 1.5px;
    }
    </style>
    </head>
    <body>

    </body>
    <script type="text/javascript">
    var margin = {top: 20, right: 80, bottom: 30, left: 100},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

    var x = d3.time.scale()
    .range([0, width]);

    var y = d3.scale.linear()
    .range([height, 0]);

    var color = d3.scale.category10();

    var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

    var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

    var line = d3.svg.line()
    .interpolate("basis")
    .x(function(d) { return x(d.timestamp); })
    .y(function(d) { return y(d.bytes_transfered); });

    var svg = d3.select("body").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 + ")");

    d3.csv("data.csv", function(error, data) {

    data.forEach(function(d) {
    d.timestamp = new Date(+d.timestamp);
    });

    var clients = d3.nest()
    .key(function(d) { return d.client_ip; })
    .entries(data);

    x.domain(d3.extent(data, function(d) { return d.timestamp; }));

    y.domain([
    d3.min(clients, function(c) { return d3.min(c.values, function(v) { return +v.bytes_transfered; }); }),
    d3.max(clients, function(c) { return d3.max(c.values, function(v) { return +v.bytes_transfered; }); })
    ]);

    svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

    svg.append("g")
    .attr("class", "y axis")
    .call(yAxis)
    .append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 6)
    .attr("dy", ".71em")
    .style("text-anchor", "end")
    .text("Bytes");

    var client = svg.selectAll(".cli")
    .data(clients)
    .enter().append("g")
    .attr("class", "cli");

    client.append("path")
    .attr("class", "line")
    .attr("d", function(d) { return line(d.values); })
    .style("stroke", function(d,i) { return color(i); });

    });

    </script>

    </html>
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    This is response to [this](http://stackoverflow.com/questions/27654447/d3-multiseries-chart-drawing) Stackoverflow question. The main issue was restructuring the data so it worked with the code in this [Bl.ock](http://bl.ocks.org/mbostock/3884955)