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.

            
              <button onclick="app.refreshChart(app.genData(25))"> Regenerate Data </button>
<p> Mouse over and click for some interaction.<br/> Bar state stored as data- attribute</p>

<div class="chart">  
</div>

            
          
!
            
              $barBorder: 2px solid white;

body {
  font-size: 10px;
  font-family: sans-serif;
  background-color:#e6e6e6;
}
p {
  font-size: 2.0em;
}
.chart div {
  font-size: 1em;
  font-family: sans-serif;
  background-color: steelblue;
  text-align: right;
  padding: 3px;
  margin: 2px;
  color: white;
  height: 10px;
  width: 14px;
  float: left;
  text-align: center;
  box-shadow: 7px 7px 15px 0px rgba(50, 50, 50, 0.35);
  border-bottom: $barBorder;
  border-top: $barBorder;
}

button {
  display: block;
  float: left;
  font-size:1.6em;
  float: left;
  margin: 0 10px 10px 20px;
  padding: 10px;
}
            
          
!
            
              "use strict";
/*
 * fun with dependency injection:
 * http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html
 */
var dep = {chartContainer: ".chart",
          chartLib: d3};

//closure-up the the app to hide private data and methods
var app = (function(dep){
  var d3 = dep.chartLib;
  var genData = function(len) {
    var data = [5];
    for (var i = 1; i < len; i++ ) {
      data.push(Math.max(4,data[i-1] + Math.floor(Math.random() * 12 - 5)));
    }
    return data;
  }

  /* animations
   * note that some styles should bemoved to css so we can 
   * add or remove a class
   * note that we have to pass a reference for the current element "that"
   */
  var expandBar = function(d, that){
    var bg = "green";
    if(d3.select(that).attr("data-selected") === "true") {
      bg = "orange";
    }
   d3.select(that)
     .style("font-size","3em")
     .transition()
     .duration(200)
     .ease("linear")
     .style({
       "background-color":bg,
       "width":"50px", 
     })
   return this;
  }

  var contractBar = function(d, that) {
    var bg = "steelblue";
    if(d3.select(that).attr("data-selected") === "true") {
      bg = "orange";
    }
    d3.select(that)
      .style("font-size","1em")
      .transition()
      .ease("linear")
      .duration(200)
      .style({
        "background-color":bg,
        "width":"14px",
      });
    return this;
  }


  var selectBar = function(d, that) {
    if(d3.select(that).attr("data-selected") === "true") {
      d3.select(that).attr("data-selected", "false");
      expandBar(d, that);
      return;
    }
    d3.select(that)
      .attr("data-selected","true")
      .style("background-color","red")
      .transition()
      .duration(200)
      .style({
        "background-color":"orange"
      });
    return this;
  }

  var refreshChart = function(data) {
    //select any existing divs
    var sel = d3.select(dep.chartContainer).selectAll("div");
      sel.data(data)
      .enter().append("div")
      .on("mouseover", function(d) {
         expandBar(d, this);
      })
      .on("mouseout", function(d) {
        contractBar(d, this);
       })
       .on("click", function(d) {
        selectBar(d, this);
       });
      //reselect so we now have references to all divs:
      sel = d3.select(dep.chartContainer).selectAll("div");
    
    //method of pre-highlighting changing bar sequentially
    sel.transition()
      .duration(function(d,i){return 60;})
      .delay(function(d,i) {return i*80 - 80 })
      .style("background-color","#ddf")
      .text(function(d) { return d; });

    // using .each to determine if the bar was previously selected.
    // see: https://github.com/mbostock/d3/wiki/Selections#each
    sel.transition()
      .each(function(d,i) {
        var sel = d3.select(this);
        sel.transition()
        .duration(600)
        .delay(i*80)
        .style("height", d * 10 + "px")
        .style("background-color", function(d,i) {
          return sel.attr("data-selected") === "true" ? "orange" : "steelblue";
        })
     })
  }	


  //expose public methods
  var pubMethods = {};
  pubMethods.genData = genData;
  pubMethods.refreshChart = refreshChart;
  return pubMethods;

}(dep));

//initialize the chart
app.refreshChart(app.genData(25));



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

Console