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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              <h1>
  Hours of the week viz sketches 
  <button class="btn" onclick="handleRefresh()">Refresh Data</button>
</h1>

<div class="graphs">
  <div class="graph density-graph">
    <p>Heatmap punch card (24 hour x 7 day)</p>
  </div>
  <div class="graph heatbar-graph">
    <p>Heat+Bar punch card (24 hour x 7 day)</p>
  </div>
  <div class="graph bar-graph">
    <p>Bar punch card (24 hour x 7 day)</p>
  </div>
  <div class="graph diameter-graph">
    <p>Circle Diameter punch card (24 hour x 7) day</p>
  </div>
</div>
            
          
!
            
              body, h1, h2, .btn {
  font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 
  font-weight: 300;  
  background-color: #fdfdfd;
  color: #666;
  margin: 0;
  padding: 0;
}

h1 {
  background-color: #f0f0f0;
  position:fixed;
  top:0;
  width:100%;
  z-index:100;
  margin: 0;
  padding: 10px;
}

.graphs {
  margin: 100px 0 0 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-around;
  width: 100%;
}

.btn {
  padding: 10px 20px;
  margin: 0 60px;
  font-size: 16px;
  border-radius: 5px;
  background-color: #fff;
  border: 1px solid #ccc;
  vertical-align: middle;
  touch-action: manipulation;
  cursor: pointer;
  float: right;
}
.btn:hover {
  background-color: #ddd;
}
.btn:active {
  border-color: #999;
}
rect, circle {
  fill: #1f9489;
}
.graph {
  width: 360px;
}
            
          
!
            
              var size = 11;
var noise = 5; // 0.1 - 5,  > 1 is more sporadic data
var animTime = 1.0; // seconds
var heatmapOpcaityMagicNumberToPlayWith = 1.25; // higher emphasizes maximums more
var max = 1;
var defaultOpacity = 0.5;
var data = [];

function genData() {
  data = [];
  var surges = Math.floor(Math.random() * 5 + 1);
  surges = surges === 5 ? 0.5 : surges;
  for (d = 0; d < 7 * 24; d++){
      data.push(Math.abs(Math.sin(surges * Math.PI * d/24)) * (Math.pow(Math.random(), noise) + max));
  }
}

function setup() {
  var graphs = [".bar-graph", ".diameter-graph", ".density-graph",".area-graph", ".heatbar-graph"];
  for (i in graphs) {
    d3.select(graphs[i]).append("svg")
      .attr("width", size * 60)
      .attr("height", size * 20);
  }
}

function render() {
  
  /* heatmap */
  var svgDensity = d3.select(".density-graph svg");
  var dens = svgDensity.selectAll(".dens")
    .data(data);

  dens.enter()
    .append("rect")
    .attr("class", "dens")
    .attr("opacity", 1);

  dens.attr("x", function(d, i) {return 1.1 * size * (i % 24) } )
    .attr("y", function(d, i) {return size * 1.1 * Math.floor(i / 24)} )
    .attr("width", size )
    .attr("height", size )
    .transition()
    .duration(animTime*1000)
    .attr("opacity", function(d){ return 0.05 + Math.pow(d / (max+1), heatmapOpcaityMagicNumberToPlayWith) });


  /* punch card*/
  var svgCircle = d3.select(".diameter-graph svg");
  var punch = svgCircle.selectAll(".punchDatum")
    .data(data);

  punch.enter()
    .append("circle")
    .attr("class", "punchDatum")
    .attr("r", size * 0.5)
    .attr("cy", function(d, i) {return size * 2 * Math.floor(i / 24) + 2 * size} );

  punch.attr("cx", function(d, i) {return size * 2 + size * (i % 24) } )  
    .transition()
    .duration(animTime*1000)
    .attr("cy", function(d, i) {return size * 2 * Math.floor(i / 24) - d + 2 * size} )
    .attr("r", function(d) {return (size * d/2) })
    .attr("opacity", defaultOpacity);


  /* bar */
  var svg = d3.select(".bar-graph svg");
  var bar = svg.selectAll(".bar")
    .data(data);
  bar.enter()
    .append("rect")
    .attr("class", "bar")
    .attr("y", function(d, i) {return size * 2 * Math.floor(i / 24) + 2 * size} )
    .attr("x", function(d, i) {return 1.1 * size * (i % 24) } )
    .attr("width", size)
    .attr("height", 0.1 * size);
  bar.transition()
    .duration(animTime*1000)
    .attr("y", function(d, i) {return size * 2 * Math.floor(i / 24) - size * d + 2 * size} )
    .attr("height", function(d) {return size * d} )
    .attr("opacity", defaultOpacity);

  /* heat bar */
  var svgHeatBar = d3.select(".heatbar-graph svg");
  var heatBar = svgHeatBar.selectAll(".heatbar")
    .data(data);

  heatBar.enter()
    .append("rect")
    .attr("class", "heatbar")
    .attr("height", size)
    .attr("y", function(d, i) {return size * 2 * Math.floor(i / 24) + 2 * size} )
    .attr("opacity", 1);

  heatBar.transition()
    .duration(animTime*1000)
    .attr("x", function(d, i) {return 1.1 * size * (i % 24) } )
    .attr("y", function(d, i) {return size * 2 * Math.floor(i / 24) - size * d/2 + 2 * size} )
    .attr("width", size)
    .attr("height", function(d) {return size * d} )
    .attr("opacity", function(d){ return 0.05 + Math.pow(d / (max+1), heatmapOpcaityMagicNumberToPlayWith) });
}

function handleRefresh() {
  genData();
  render();
}

setup();
handleRefresh();


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

Console