Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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.

+ add another resource

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <form>
    <label><input type="radio" name="mode" id="stacked" value="stacked" checked> Stacked</label>
  <label><input type="radio" name="mode" id="grouped" value="grouped"> Grouped</label>
</form>

<div class="info-chart">
  <div class="tooltip hidden">
		<p><span class="value">100</span> thousand</p>
	</div>
</div>

<table id="infoTable"> 
<thead> 
  <caption>Annual consumption of bananas (thousands)</caption>
    <tr> 
         <th>1993</th>  <th>1994</th> <th>1994</th> <th>1995</th> <th>1996</th> <th>1997</th> <th>1998</th> <th>1999</th> <th>2000</th> <th>2001</th> <th>2002</th> <th>2003</th> <th>2004</th> <th>2005</th> <th>2006</th> <th>2007</th> <th>2008</th> <th>2009</th> <th>2010</th> <th>2011</th> <th>2012</th> <th>2013</th>
    </tr> 
</thead> 
<tbody> 
    <tr class="men"> 
        <td>8</td>  <td>10</td> <td>13</td> <td>19</td> <td>21</td> <td>25</td> <td>22</td> <td>18</td> <td>15</td> <td>13</td> <td>11</td> <td>12</td> <td>15</td> <td>20</td> <td>18</td> <td>17</td> <td>16</td> <td>18</td> <td>23</td> <td>25</td>  <td>22</td> <td>18</td>  
    </tr>
        <tr class="women"> 
         <td>25</td> <td>22</td> <td>18</td> <td>15</td> <td>13</td> <td>11</td> <td>12</td> <td>15</td> <td>20</td> <td>18</td> <td>17</td> <td>16</td> <td>18</td> <td>23</td> <td>25</td>  <td>22</td> <td>18</td>  <td>21</td> <td>19</td> <td>13</td> <td>10</td> <td>8</td>
    </tr>
  
</tbody> 
</table> 

              
            
!

CSS

              
                body {
    color: #000000;
    font-family: Verdana,arial,Helvetica,sans-serif;
    font-size: 90%;
}

.info-chart {
  position: relative;
}

.info-chart rect:hover {
	fill: #33ADBC;
}

.tooltip {
	position: absolute;
	width: 120px;
	height: auto;
	padding: 10px;
	background-color: #000;
	pointer-events: none;
}


.tooltip-left:before {
	content: "";
	position:absolute;
	bottom: 11px;
	right: -8px;
	border-left: 8px solid #000;
	border-top: 8px solid transparent;
	border-bottom: 8px solid transparent;
	border-right: 0;
}

.tooltip-right:before {
	content: "";
	position:absolute;
	bottom: 11px;
	left: -8px;
	border-right: 8px solid #000;
	border-top: 8px solid transparent;
	border-bottom: 8px solid transparent;
	border-left: 0;
}


.tooltip.hidden {
	display: none;
}

.tooltip p {
	margin: 0;
	color: #fff;
	font-family: sans-serif;
	font-size: 16px;
	line-height: 20px;
}
              
            
!

JS

              
                	var dataset  = [],
	menArray  = [], 
	womenArray = [],
	menObjectArray = [],
	womenObjectArray = [],
	stack = d3.layout.stack(),
  menRow = d3.selectAll("tr.men"),
	womenRow = d3.selectAll("tr.women"),
	/*Width and height*/
	w = 630,
	h = 200,
	barPadding = 5,
	padding = 60
  duration = 500,
  grouped = false;
        

	menRow.each(function() {
		d3.select(this).selectAll("td").each(function() { 
			menArray.push(parseInt(d3.select(this).text()));
		})
	});

	womenRow.each(function() {
		d3.select(this).selectAll("td").each(function() { 
			womenArray.push(parseInt(d3.select(this).text()));
		})
	});

	for (var i = 0; i < menArray.length; i++) {
		var newObject = {};
		newObject.x = i;
		newObject.y = menArray[i];
		menObjectArray.push(newObject);
	};

	for (var i = 0; i < womenArray.length; i++) {
		var newObject = {};
		newObject.x = i;
		newObject.y = womenArray[i];
		womenObjectArray.push(newObject);
	};

	dataset.push(menObjectArray);
	dataset.push(womenObjectArray);

	stack(dataset);

	/* Define Y scale */
	var yScale = d3.scale.linear()
		.domain([0,				
			d3.max(dataset, function(d) {
				return d3.max(d, function(d) {
					return d.y0 + d.y;
				});
			})
		])
		.range([h, padding]);	
        
	/* Create SVG element */
	var svg = d3.select(".info-chart")
		.append("svg")
		.attr("width", w)
		.attr("height", h);

	/* Add a group for each row of data */
	var groups = svg.selectAll("g")
		.data(dataset)
		.enter()
		.append("g")
		.style("fill", function(d, i) {
			return	i === 0 ? "#E53524" : "#F8B436"; 
		});		
                    
	groups.selectAll("rect")
		.data(function(d) { return d; })
		.enter()
		.append("rect")
		.attr("x", function(d, i){
			return i * (w / dataset[0].length); 
		})
		.attr("y", function(d){
			return yScale(d.y) + yScale(d.y0) - h; 
		})
		.attr("width", w / dataset[0].length - barPadding )
		.attr("height", function(d){
			return h - yScale(d.y);
		})
		.on("mouseover", function(d) {

			/* Get this bar's x/y values, then augment for the tooltip */
			var xPosition,
			yPosition = parseInt(d3.select(this).attr("y") );
						
	
			if (d3.select(this).attr("x") < 350) {
        
        if (grouped) { 
            console.log("We be grouped!");
            xPosition = parseFloat(d3.select(this).attr("x")) + 14;
          } else  { 
            xPosition = parseFloat(d3.select(this).attr("x")) + 27;
          }
           
				d3.select(".tooltip").classed("tooltip-left", false).classed("tooltip-right", true);
			} else {
				xPosition = parseFloat(d3.select(this).attr("x")) - 143;
				d3.select(".tooltip").classed("tooltip-left", true).classed("tooltip-right", false);;
			}
	
			/* Update the tooltip position and value */
			d3.select(".tooltip")
				.style("left", xPosition + "px")
				.style("top", yPosition + "px")
				.select(".value")
				.text(d.y);
	
			/* Show the tooltip */
			d3.select(".tooltip").classed("hidden", false);
	
			})
			.on("mouseout", function() {
				/* Hide the tooltip */
				d3.select(".tooltip").classed("hidden", true);			
			});

d3.selectAll("input").on("change", change);		

function change() {
  if (this.value === "grouped") { 
    grouped = true;
    transitionGrouped(); 
  } else  {
    grouped = false;
    transitionStacked();
  }
}

var transitionGrouped = function() {
	groups.selectAll("rect")
		.transition()
		.duration(duration)
    .delay(function(d, i) { return i / dataset[0].length * duration; })
		.attr("width", (w / dataset[0].length - barPadding) / 2 )
		.transition()
		.duration(duration)
		.attr("x", function(d, i, j){
			return i * (w / dataset[0].length) + ((w / dataset[0].length - barPadding)/2) * j ; 
		})
		.transition()
    .duration(duration)
		.attr("y", function(d, i, j){
			return yScale(d.y); 
		});
};

var transitionStacked = function() {
	groups.selectAll("rect")
		.transition()
    .duration(duration)
    .delay(function(d, i) { return i / dataset[0].length * duration; })
		.attr("y", function(d){
			return yScale(d.y) + yScale(d.y0) - h; 
		})
		.transition()
		.duration(duration)
		.attr("x", function(d, i){
			return i * (w / dataset[0].length); 
		})
		.transition()
		.duration(duration)
		.attr("width", w / dataset[0].length - barPadding );
};

              
            
!
999px

Console