Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
        <style type="text/css">
            
            .axis path,
            .axis line {
                fill: none;
                stroke: black;
                shape-rendering: crispEdges;
            }
            
            .axis text {
                font-family: sans-serif;
                font-size: 11px;
            }
            .y1 {
                fill: white;
                stroke: orange;
                stroke-width: 1.5px;
            }
            .y2 {
                fill: white;
                stroke: red;
                stroke-width: 1.5px;
            }
            .y3 {
                fill: white;
                stroke: steelblue;
                stroke-width: 1.5px;
            }
            .line {
              fill: none;
              stroke: steelblue;
              stroke-width: 1.5px;
            }
        </style>
    </head>
    <body>
        <script type="text/javascript">
    //Width and height
    var w = 500;
    var h = 300;
    var padding = 30;
      
    var now = d3.time.hour.utc(new Date);
    var dataset = [
        [
            {x: d3.time.hour.utc.offset(now, -5), y: 1}, 
            {x: d3.time.hour.utc.offset(now, -4), y: 2}, 
            {x: d3.time.hour.utc.offset(now, -3), y: 3}, 
            {x: d3.time.hour.utc.offset(now, -2), y: 4}, 
            {x: d3.time.hour.utc.offset(now, -1), y: 5}, 
            {x: now, y: 1}
        ],
        [
            {x: d3.time.hour.utc.offset(now, -5), y: 3}, 
            {x: d3.time.hour.utc.offset(now, -4), y: 1}, 
            {x: d3.time.hour.utc.offset(now, -3), y: 3}, 
            {x: d3.time.hour.utc.offset(now, -2), y: 1}, 
            {x: d3.time.hour.utc.offset(now, -1), y: 5}, 
            {x: now, y: 1}
        ],
        [
            {x: d3.time.hour.utc.offset(now, -5), y: 2}, 
            {x: d3.time.hour.utc.offset(now, -4), y: 4}, 
            {x: d3.time.hour.utc.offset(now, -3), y: 1}, 
            {x: d3.time.hour.utc.offset(now, -2), y: 2}, 
            {x: d3.time.hour.utc.offset(now, -1), y: 3}, 
            {x: now, y: 1}
        ]
    ];                          
              
    var yExtents = d3.extent(d3.merge(dataset), function (d) { return d.y; });
    var xExtents = d3.extent(d3.merge(dataset), function (d) { return d.x; });
         
          console.log(xExtents)
      //Create scale functions
      var xScale = d3.time.scale()
               .domain([xExtents[0], xExtents[1]])
               .range([padding, w - padding * 2]);
      var yScale = d3.scale.linear()
               .domain([0, yExtents[1]])
               .range([h - padding, padding]);
      //Define X axis
      var xAxis = d3.svg.axis()
                .scale(xScale)
                .orient("bottom")
                .ticks(5);
      //Define Y axis
      var yAxis = d3.svg.axis()
                .scale(yScale)
                .orient("left")
                .ticks(5);
      //Define lines
      var line = d3.svg.line()
               .x(function(d) { return x(d.x); })
               .y(function(d) { return y(d.y1, d.y2, d.y3); });
      //Create SVG element
      var svg = d3.select("body")
            .append("svg")
            .attr("width", w)
            .attr("height", h);
      var pathContainers = svg.selectAll('g.line')
        .data(dataset);
      pathContainers.enter().append('g')
        .attr('class', 'line');
      pathContainers.selectAll('path')
        .data(function (d) { return [d]; }) // continues the data from the pathContainer
        .enter().append('path')
          .attr('d', d3.svg.line()
            .x(function (d) { return xScale(d.x); })
            .y(function (d) { return yScale(d.y); })
            .interpolate('monotone')
          );
      pathContainers.selectAll('circle')
        .data(function (d) { return d; })
        .enter().append('circle')
        .attr('cx', function (d) { return xScale(d.x); })
        .attr('cy', function (d) { return yScale(d.y); })
        .attr('r', 5); 
          
      //Create X axis
      svg.append("g")
        .attr("class", "axis")
        .attr("transform", "translate(0," + (h - padding) + ")")
        .call(xAxis);
      
      //Create Y axis
      svg.append("g")
        .attr("class", "axis")
        .attr("transform", "translate(" + padding + ",0)")
        .call(yAxis);
        </script>
    </body>
</html>
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers