cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              #svgParent {
  width: 100%;
  height: 350px;
}
            
          
!
            
              d3.sketchy = {};

d3.sketchy.rect = function(selection) {
  
  var rh = 50,rw = 10,w = 2,c = [0,0],fillColor = "red",strokeColor="black",jostle = 0;
  
  function d3_sketchyRect(selection) {

  selection.append("rect").attr("class", "sketchy").attr("x", c[0]).attr("y", c[1]).attr("height", rh).attr("width", rw).style("fill", fillColor)

  var randomJostle = d3.scaleLinear().domain([0,1]).range([-jostle,jostle]);
 
  var j = [];
  j.push(randomJostle(Math.random()));
  j.push(randomJostle(Math.random()));
  j.push(randomJostle(Math.random()));
  j.push(randomJostle(Math.random()));
  j.push(randomJostle(Math.random()));
  j.push(randomJostle(Math.random()));
  
  //divide width by two to get the offset
  var z = w /2;
  var gRectPoints = [];
  gRectPoints.push([c[0] - (z),c[1] + rh]);
  gRectPoints.push([c[0] - (z * .55) + j[0],c[1] - (z * .55) + j[1]]);
  gRectPoints.push([c[0] + rw + (z * .35) + j[2],c[1] - (z * .35) + j[3]]);
  gRectPoints.push([c[0] + rw + (z * .25) + j[4],c[1] + rh + (z * .25) + j[5]]);
  gRectPoints.push([c[0] + (z),c[1] + rh + (z * .1)]);
  gRectPoints.push([c[0] + (z),c[1] + rh - (z * .1)]);
  gRectPoints.push([c[0] + rw - (z * .25) + j[4],c[1] + rh - (z * .25) + j[5]]);
  gRectPoints.push([c[0] + rw - (z * .35) + j[2],c[1] + (z * .35) + j[3]]);
  gRectPoints.push([c[0] + (z * .55) + j[0],c[1] + (z * .55) + j[1]]);
  gRectPoints.push([c[0] + (z),c[1] + rh]);

  
      sketchyC = d3.line()
        .x(function(d,i) {
        return d[0]
    })
        .y(function(d) {
        return d[1]
    })
        .curve(d3.curveLinear)
  
  selection.append("path").attr("class", "sketchy").attr("d", sketchyC(gRectPoints)).style("stroke", "none").style("fill", strokeColor)
  return this;
  }
  
    d3_sketchyRect.height = function(data) {
      if (!arguments.length) return rh;
      rh = data;
      return this;
      }

    d3_sketchyRect.width = function(data) {
      if (!arguments.length) return rw;
      rw = data;
      return this;
      }

  d3_sketchyRect.x = function(data) {
      if (!arguments.length) return c[0];
      c[0] = data;
      return this;
      }

  d3_sketchyRect.y = function(data) {
      if (!arguments.length) return c[1];
      c[1] = data;
      return this;
      }
  
    d3_sketchyRect.fill = function(data) {
      if (!arguments.length) return fillColor;
      fillColor = data;
      return this;
      }

    d3_sketchyRect.stroke = function(data) {
      if (!arguments.length) return strokeColor;
      strokeColor = data;
      return this;
      }
      
      d3_sketchyRect.strokeWidth = function(data) {
      if (!arguments.length) return w;
      w = data;
      return this;
      }
      
      d3_sketchyRect.jostle = function(data) {
      if (!arguments.length) return jostle;
      jostle = data;
      return this;
      }
      
  return d3_sketchyRect;

}



fetch('https://cdn.rawgit.com/ArvinH/f47671a9ff33b6719b043945d36054ac/raw/09a6ea23ee40d4a83205eef6d4fd4e5efa072c2b/104life.json')
  .then(function (response) {
    return response.json();
  }).then(function (parsedJson) {
    generateGraph(parsedJson);
  }).catch(function (ex) {
    console.log('parsing failed', ex)
  })

function generateGraph(jsonData) {
  var data = jsonData.map(function(obj) {return obj.value;});
  var hscale = d3.scaleLinear()
    .domain([0, d3.max(data)])
    .range([0, 500]);

  var color = d3.scaleLinear().domain([0, d3.max(data)])
    .interpolate(d3.interpolateHcl).range(['#ffb3ba', '#bae1ff']);

  d3.select('#svgParent').selectAll("g").data(data)
    .enter()
    .append("g")
    .attr("class", "bar");

  d3.select("#svgParent")
    .selectAll("g.bar")
    .each(function (d, i) {
      // d3.select(this).selectAll("*").remove();
      var x = i * 60 + 25;
      var y = 300 - hscale(d) / 2;
      var rw = 60;
      var rh = hscale(d) / 2;
      var sketchyBar = d3.sketchy.rect();
      sketchyBar
        .height(rh)
        .width(rw)
        .x(x)
        .y(y)
        .fill(color)
        .stroke("black")
        .strokeWidth(10)
        .jostle(5)
      d3.select(this).call(sketchyBar)
    })
}

            
          
!
999px
Loading ..................

Console