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.

            
              <header>
  <div>
    <input placeholder="CSV, TSV, XML or JSON file" id="file" value="https://rawgit.com/anonymous/24251952a7e4d3c541a1/raw/fb8eaeeae97abf30b4ab75f34c72692f13ca2e9c/data.tsv"/>
    <button id="load">LOAD</button>
  </div>
  <div>load</div>
</header>
<svg id="con1svg"></svg>
            
          
!
            
              body{
  background-color: #222;
  font-family: sans-serif;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

*{
  color: #fff;
  font-family: sans-serif;
  fill: #fff;
}

.axis text{
  fill: #666;
  font-size: 80%;
}

.axis line, .axis path {
  stroke: #666;
  stroke-width: 1px;
  shape-rendering: crispEdges;
  fill: none;
}

.tooltip {
  opacity: 0;
}

header {
  position: fixed;
  background-color: #000;
  width: 100%;
  opacity: 0.2;
  top: -50px;
  vertical-align: bottom;
  text-align: right;
  transition: top 0.5s ease;
}
header div{
  margin: 10px;
}
header:hover {
  opacity: 1;
  top: 0px;
}
header button, header input{
  background-color: #333;
  border-style: none;
  padding: 10px;
}
header input{
  width: 50%;
}
header button:hover {
  background-color: #c61c6f;
}

            
          
!
            
              // "contstants"
var margin = {top: 40, right: 30, bottom: 40, left: 50},
  height = function(){ return window.innerHeight - 100; },
  width = function(){ return window.innerWidth - 100; };

// scales
var colors = d3.scale.linear()
  	.range(['#c61c6f','#9e1658']),
	yScale = d3.scale.linear(),
  xScale = d3.scale.ordinal();

// axes
var vAxis = d3.svg.axis()
    .scale(yScale)
    .orient('left')
    .ticks(10),
  hAxis = d3.svg.axis()
    .scale(xScale)
    .orient('bottom');

// static d3 selectors (not-data-driven)... order of appending is 
// important for SVG "z-axis"
var myChart = d3.select('#con1svg'),
  root = myChart.append('g')
    .classed("root", 1)
    .attr('transform','translate('+ [margin.left, margin.top] +')'),
  bars = root.append("g").classed("bars", 1),
  tooltip = root.append('text')
    .attr('class', 'tooltip'),
  vGuide = root.append('g')
    .classed("axis", 1),
  hGuide = root.append('g')
    .classed("axis", 1);

// leaving most of the form stuff to HTML/CSS
d3.select("#load")
  .on("click", loadData)
  // call it once, to set things up
  .on("click")();

// the main data-driven function
function render(err, bardata) {
	var max = d3.max(bardata, function(d) { return d.totalp; });
  
  // update scales
  colors.domain([0, bardata.length]);
  yScale.domain([0, max])
    .range([height(), 0]);
  xScale.domain(d3.range(1, bardata.length + 1))
    .rangeBands([0, width()], .1);

  // resize
  myChart.attr('width', width() + margin.left + margin.right)
    .attr('height', height() + margin.top + margin.bottom)

  // the individual bars
  var bar = bars.selectAll('.bar')
  	.data(bardata)
  
  // handle the case of loading new data on top of old
  bar.exit().remove();
  
  // a simple enter is good: always identity, and event functions are good
  bar.enter().append('rect')
  	.classed('bar', 1)
    .on('mouseover', showTooltip)
    .on('mouseout', hideTooltip);
  
  // do the implicit update
  bar
    .style('fill', function(d, i) { return colors(i); })
    .attr('width', xScale.rangeBand())
    .attr('height', 0) //changed to zero for making event purpose
    .attr('x', function(d, i) { return xScale(i + 1); })
    .attr('y', yScale.range()[0]) //only height coz event purpose

  bar.transition()
    .attr('height', function(d) { return height() - yScale(d.totalp); })
    .attr('y', function(d) { return yScale(d.totalp); })
    .delay(function(d, i) { return i * 20; })
    .duration(1500)
    .ease('elastic');

  vGuide.call(vAxis);
	hGuide.call(hAxis)
  	.attr("transform", "translate(0, " + yScale.range()[0] + ")");
}


function loadData(evt) {
  var file = d3.select("#file").property("value"),
    ext = file.split(".").slice(-1)[0];
  d3[ext](file, render);
}


function showTooltip(d, i) {
  tooltip.transition()
  	.style('opacity', .9)
    .text(d.Name + ": " + d.totalp)
    .attr("transform", "translate(" + [
        xScale(i + 1),
        yScale(d.totalp) - 10
      ] + ")");

 d3.select(this).style('opacity', .7);
}

function hideTooltip(d, i) {
  tooltip.transition().style('opacity', 0);
  
  d3.select(this).transition().style('opacity', 1);
}
            
          
!
999px
Loading ..................

Console