Skip to content

Instantly share code, notes, and snippets.

@Guerino1
Last active October 2, 2015 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guerino1/2164562 to your computer and use it in GitHub Desktop.
Save Guerino1/2164562 to your computer and use it in GitHub Desktop.
Multiple D3 Vertical Bar Charts Mixed In With Common HTML Layout Constructs
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en-US"
lang="en-US">
<head profile="http://www.w3.org/2005/10/profile">
<title>IF4IT Sample Charts Web Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Description" content="This page tries to mix charts with html formatting and layout constructs." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="-l" />
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
// This example draws vertical bar charts...
// Created by Frank Guerino : "http://www.guerino.net"
// Data Used for this example...
var dataSet1 = [
{xCoordinate: "Legend String 1", magnitude: 54, link: "http://www.if4it.com"},
{xCoordinate: "Legend String 2", magnitude: 21, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 3", magnitude: 31, link: "http://www.if4it.com/resources.html"},
{xCoordinate: "Legend String 4", magnitude: 14, link: "http://www.if4it.com/taxonomy.html"},
{xCoordinate: "Legend String 5", magnitude: 19, link: "http://www.if4it.com/disciplines.html"},
{xCoordinate: "Legend String 6", magnitude: 47, link: "http://www.if4it.com"},
{xCoordinate: "Legend String 7", magnitude: 27, link: "http://www.if4it.com/glossary.html"}];
var dataSet2 = [
{xCoordinate: "Legend String 8", magnitude: 31, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 9", magnitude: 54, link: "http://www.if4it.com/resources.html"},
{xCoordinate: "Legend String 10", magnitude: 21, link: "http://www.if4it.com"},
{xCoordinate: "Legend String 11", magnitude: 31, link: "http://www.if4it.com/taxonomy.html"},
{xCoordinate: "Legend String 12", magnitude: 54, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 13", magnitude: 14, link: "http://www.if4it.com/resources.html"},
{xCoordinate: "Legend String 14", magnitude: 14, link: "http://www.if4it.com/disciplines.html"},
{xCoordinate: "Legend String 15", magnitude: 27, link: "http://www.if4it.com/glossary.html"}];
var dataSet3 = [
{xCoordinate: "Legend String 16", magnitude: 21, link: "http://www.if4it.com"},
{xCoordinate: "Legend String 17", magnitude: 41, link: "http://www.if4it.com/resources.html"},
{xCoordinate: "Legend String 18", magnitude: 34, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 19", magnitude: 9, link: "http://www.if4it.com/taxonomy.html"},
{xCoordinate: "Legend String 20", magnitude: 47, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 21", magnitude: 17, link: "http://www.if4it.com/resources.html"},
{xCoordinate: "Legend String 22", magnitude: 47, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 23", magnitude: 37, link: "http://www.if4it.com/disciplines.html"},
{xCoordinate: "Legend String 24", magnitude: 47, link: "http://www.if4it.com/resources.html"},
{xCoordinate: "Legend String 25", magnitude: 4, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 26", magnitude: 18, link: "http://www.if4it.com"},
{xCoordinate: "Legend String 27", magnitude: 41, link: "http://www.if4it.com/resources.html"},
{xCoordinate: "Legend String 28", magnitude: 37, link: "http://www.if4it.com/glossary.html"},
{xCoordinate: "Legend String 29", magnitude: 27, link: "http://www.if4it.com"}];
function drawVerticalBarChart(chartID, dataSet, selectString, colors) {
// chartID => A unique drawing identifier that has no spaces, no "." and no "#" characters.
// dataSet => Input Data for the chart, itself.
// selectString => String that allows you to pass in
// a D3 select string.
// colors => String to set color scale. Values can be...
// => "colorScale10"
// => "colorScale20"
// => "colorScale20b"
// => "colorScale20c"
var canvasWidth = 700;
var barsWidthTotal = 300
var barWidth = barsWidthTotal / dataSet.length;
//var canvasHeight = 200;
var canvasHeight = dataSet.length * 20 + 40;
var legendOffset = 30;
var legendBulletOffset = 30;
var legendTextOffset = 20;
var x = d3.scale.linear().domain([0, dataSet.length]).range([0, barsWidthTotal]);
var y = d3.scale.linear().domain([0, d3.max(dataSet, function(d) { return d.magnitude; })]).rangeRound([0, canvasHeight]);
//document.writeln(selectString);
// Color Scale Handling...
var colorScale = d3.scale.category20c();
switch (colors)
{
case "colorScale10":
colorScale = d3.scale.category10();
break;
case "colorScale20":
colorScale = d3.scale.category20();
break;
case "colorScale20b":
colorScale = d3.scale.category20b();
break;
case "colorScale20c":
colorScale = d3.scale.category20c();
break;
default:
colorScale = d3.scale.category20c();
};
var synchronizedMouseOver = function() {
var bar = d3.select(this);
var indexValue = bar.attr("index_value");
var barSelector = "." + "bars-" + chartID + "-bar-" + indexValue;
var selectedBar = d3.selectAll(barSelector);
selectedBar.style("fill", "Maroon");
var bulletSelector = "." + "bars-" + chartID + "-legendBullet-" + indexValue;
var selectedLegendBullet = d3.selectAll(bulletSelector);
selectedLegendBullet.style("fill", "Maroon");
var textSelector = "." + "bars-" + chartID + "-legendText-" + indexValue;
var selectedLegendText = d3.selectAll(textSelector);
selectedLegendText.style("fill", "Maroon");
};
var synchronizedMouseOut = function() {
var bar = d3.select(this);
var indexValue = bar.attr("index_value");
var barSelector = "." + "bars-" + chartID + "-bar-" + indexValue;
var selectedBar = d3.selectAll(barSelector);
var colorValue = selectedBar.attr("color_value");
selectedBar.style("fill", colorValue);
var bulletSelector = "." + "bars-" + chartID + "-legendBullet-" + indexValue;
var selectedLegendBullet = d3.selectAll(bulletSelector);
var colorValue = selectedLegendBullet.attr("color_value");
selectedLegendBullet.style("fill", colorValue);
var textSelector = "." + "bars-" + chartID + "-legendText-" + indexValue;
var selectedLegendText = d3.selectAll(textSelector);
selectedLegendText.style("fill", "Blue");
};
// Create the svg drawing canvas...
var canvas = d3.select(selectString)
.append("svg:svg")
//.style("background-color", "yellow")
.attr("width", canvasWidth)
.attr("height", canvasHeight);
// Draw individual hyper text enabled bars...
canvas.selectAll("rect")
.data(dataSet)
.enter().append("svg:a")
.attr("xlink:href", function(d) { return d.link; })
.append("svg:rect")
.attr("x", function(d, i) { return x(i); })
// NOTE: The following "+15" adds an offset that ensures some space
// between the top of the canvas and the top of the highest bar, so
// that text can be added in that space, later.
.on('mouseover', synchronizedMouseOver)
.on("mouseout", synchronizedMouseOut)
.attr("y", function(d) { return canvasHeight - y(d.magnitude) + 15; })
.attr("height", function(d) { return y(d.magnitude); })
.attr("width", barWidth)
.style("fill", "White" )
.style("stroke", "White" )
.transition()
.duration(150)
.delay(function(d, i) { return i * 100; })
.style("fill", function(d, i) { colorVal = colorScale(i); return colorVal; } )
.attr("index_value", function(d, i) { return "index-" + i; })
.attr("class", function(d, i) { return "bars-" + chartID + "-bar-index-" + i; })
.attr("color_value", function(d, i) { return colorScale(i); }) // Bar fill color...
.style("stroke", "white"); // Bar border color...
// Create text values that go at top of each bar...
canvas.selectAll("text")
.data(dataSet) // Instruct to bind dataSet to text elements
.enter().append("svg:text") // Append text elements
// Identify root coordinate (x,y)
//.attr("x", function(d, i) { return x(i) + barWidth; }) // <-- Can't use because of bug in FireFox
.attr("x", function(d, i) { return x(i) + barWidth / 2; }) // <-- Using because of bug in Firefox
// Note: the following "+1" offset places value above bar in
// Space between the top of the bar and the top of the canvas.
.attr("y", function(d) {
return canvasHeight - y(d.magnitude ); })
//.attr("dx", -barWidth/2) // <-------------- Can't use because of bug in FireFox
.attr("dy", "1em") // Controls padding to place text above bars
.attr("text-anchor", "middle")
.text(function(d) { return d.magnitude;})
.attr("fill", "Black");
// Plot the bullet circles...
canvas.selectAll("circle")
.data(dataSet).enter().append("svg:circle") // Append circle elements
.attr("cx", barsWidthTotal + legendBulletOffset)
.attr("cy", function(d, i) { return legendOffset + i*20; } )
.attr("stroke-width", ".5")
.style("fill", function(d, i) { return colorScale(i); }) // Bar fill color
.attr("index_value", function(d, i) { return "index-" + i; })
.attr("class", function(d, i) { return "bars-" + chartID + "-legendBullet-index-" + i; })
.attr("r", 5)
.attr("color_value", function(d, i) { return colorScale(i); }) // Bar fill color...
.on('mouseover', synchronizedMouseOver)
.on("mouseout", synchronizedMouseOut);
// Create hyper linked text at right that acts as label key...
canvas.selectAll("a.legend_link")
.data(dataSet) // Instruct to bind dataSet to text elements
.enter().append("svg:a") // Append legend elements
.attr("xlink:href", function(d) { return d.link; })
.append("text")
.attr("text-anchor", "left")
.attr("x", barsWidthTotal + legendBulletOffset + legendTextOffset)
.attr("y", function(d, i) { return legendOffset + i*20 - 10; })
.attr("dx", 0)
.attr("dy", "1em") // Controls padding to place text above bars
.text(function(d) { return d.xCoordinate;})
.style("fill", "Blue")
.attr("index_value", function(d, i) { return "index-" + i; })
.attr("class", function(d, i) { return "bars-" + chartID + "-legendText-index-" + i; })
.on('mouseover', synchronizedMouseOver)
.on("mouseout", synchronizedMouseOut);
};
</script>
<STYLE type="text/css">
div.div_Header {
width: 100%;
border:2px solid White;
border-radius:7px;
background: WhiteSmoke;
font: bold 14px Arial;
font-family:Arial, Helvetica, sans-serif;
text-align:center;
}
h1.h1_BodyHeader {
text-align:center;
font: bold 1.5em Arial;
}
h2.h2_LeftMenuHeader {
text-align:center;
font: bold 1.2em Arial;
}
h3.h3_Body {
text-align:center;
}
p.p_Red {
color:Red;
}
table.table_Header {
width: 100%;
text-align:center;
}
td.td_HeaderLeft {
text-align:left;
}
td.td_HeaderRight {
text-align:right;
}
div.div_Menu {
width: 100%;
border:2px solid White;
border-radius:7px;
background: MidnightBlue;
font: bold 14px Arial;
font-family:Arial, Helvetica, sans-serif;
color:White;
text-align:center;
}
p.p_Left {
font-family:Arial, Helvetica, sans-serif;
color:Black;
text-align:left;
padding-left: 5px;
font: normal 14px Arial;
}
table.table_Body {
width: 100%;
height: 100%;
padding: 0;
}
td.td_BodyLeft {
width: 250px;
height: 100%;
padding: 0;
}
li.li_LeftMenu {
text-align:left;
font: normal 14px Arial;
}
a.a_LeftMenuNoUnderLine {
text-decoration: none;
}
div.div_Body {
height: 100%;
width: 100%;
position: relative;
border:2px solid White;
border-radius:7px;
background: WhiteSmoke;
font: bold 14px Arial;
font-family:Arial, Helvetica, sans-serif;
color:Black;
text-align:center;
}
div.div_Footer {
width: 100%;
border:2px solid White;
border-radius:7px;
background: MidnightBlue;
font: bold 14px Arial;
font-family:Arial, Helvetica, sans-serif;
color:White;
text-align:center;
}
p.p_if4itMessage {
width: 100%;
background: White;
font: bold .75em Arial;
font-family:Arial, Helvetica, sans-serif;
color:GoldenRod;
text-align:center;
}
.menuButton{
background-color: MidnightBlue;
}
.menuButton li{
height: 100%;
list-style: none;
display: inline;
}
.menuButton li a{
height: 100%;
padding: 3px 0.5em;
text-decoration: none;
color: White;
background-color: MidnightBlue;
border: 2px solid MidnightBlue;
}
.menuButton li a:hover{
height: 100%;
color: MidnightBlue;
background-color: White;
border-style: outset;
background-color: White;
}
.menuButton li a:active{
height: 100%;
border-style: inset;
color: MidnightBlue;
background-color: White;
}
.menuButton li a.disabled{
height: 100%;
padding: 3px 0.5em;
text-decoration: none;
color: MidnightBlue;
background-color: White;
border: 2px solid MidnightBlue;
border-style: inset;
border-color: White;
}
</STYLE>
<STYLE type="text/css">
div.div_RootBody {
position: relative;
border:2px solid White;
border-radius:7px;
background: WhiteSmoke;
font: normal 14px Arial;
font-family:Arial, Helvetica, sans-serif;
color:Black;
padding: 0px 1em;
text-align:left;
}
</STYLE>
<!--[if gt IE 7]>
<style>body { overflow-y:scroll; } </style>
<![endif]-->
</head>
<body>
<p id="dateString"></p>
<div>
<h1 style="text-align:center; font: bold 1.7em Arial;"><a href="http://www.if4it.com">The International Foundation for Information Technology (IF4IT)</a></h1>
</div>
<div class="div_Menu">
<ul class="menuButton">
<li><a href="http://www.if4it.com">HOME</a></li>
<li><a href="http://www.if4it.com/resources.html">RESOURCES</a></li>
<li><a href="http://www.if4it.com">PRODUCTS</a></li>
<li><a href="http://www.if4it.com">SERVICES</a></li>
<li><a href="http://www.if4it.com">SUPPORT</a></li>
<li><a href="http://www.if4it.com">HELP</a></li>
</ul>
</div>
<table class="table_Body">
<tr>
<td class="td_BodyLeft">
<div class="div_Body">
<h2 class="h2_LeftMenuHeader">Sample Left Menu Links</h2>
<ul>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com">IF4IT Home</a></li>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com/disciplines.html">IF4IT Disciplines</a></li>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com/glossary.html">IF4IT Glossary</a></li>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com/taxonomy.html">IF4IT Taxonomies</a></li>
</ul>
<p class="p_Left">This is dummy paragraph 1 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 2 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 3 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 4 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 5 text that would go in this section of the page.</p>
<ul>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com">IF4IT Home</a></li>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com/disciplines.html">IF4IT Disciplines</a></li>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com/glossary.html">IF4IT Glossary</a></li>
<li class="li_LeftMenu"><a class="a_LeftMenuNoUnderLine" href="http://www.if4it.com/taxonomy.html">IF4IT Taxonomies</a></li>
</ul>
<p class="p_Left">This is dummy paragraph 1 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 2 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 3 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 4 text that would go in this section of the page.</p>
<p class="p_Left">This is dummy paragraph 5 text that would go in this section of the page.</p>
</div>
</td>
<td>
<div class="div_RootBody">
<h1 style="text-align:center; font: bold 1.5em Arial;">Multiple Interactive D3 Vertical Bar Charts on an HTML Page</h1>
</div>
<div class="div_RootBody">
<h3 class="h3_Body">Example Description</h3>
<p class="p_Red">This example shows how to mix multiple interactive D3 based Bar Charts with multiple common HTML page layout constructs. Cut, paste, and run the code into your own index.html file to see the multiple charts.</p>
</div>
<div class="div_RootBody" id="bar_chart_1">
<h3 class="h3_Body">Bar Chart Number 1</h3>
<div class="chart"></div>
</div>
<div class="div_RootBody" id="bar_chart_2">
<h3 class="h3_Body">Bar Chart Number 2</h3>
<div class="chart"></div>
</div>
<div class="div_RootBody">
<h3 class="h3_Body">Content Section: No Charts Here</h3>
<p>This is dummy paragraph 1 text that would go in this section of the page.</p>
<p>This is dummy paragraph 2 text that would go in this section of the page.</p>
</div>
<div class="div_RootBody" id="bar_chart_3">
<h3 class="h3_Body">Bar Chart Number 3</h3>
<div class="chart"></div>
</div>
</td>
</tr>
</table>
<div class="div_Footer"><p><p>This is the IF4IT Footer Message for this web page.</p></p></div>
<div><p class="p_if4itMessage">This Site Has Been Created and Published by The International Foundation for Information Technology (IF4IT).</p></div>
<script type="text/javascript">
drawVerticalBarChart("Bars1", dataSet1, "#bar_chart_1 .chart", "colorScale20b");
drawVerticalBarChart("Bars2", dataSet2, "#bar_chart_2 .chart", "colorScale10");
drawVerticalBarChart("Bars3", dataSet3, "#bar_chart_3 .chart", "colorScale20c");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment