Skip to content

Instantly share code, notes, and snippets.

@abbhakan
Last active August 29, 2015 14:18
Show Gist options
  • Save abbhakan/bb491f4a6945d9c6b169 to your computer and use it in GitHub Desktop.
Save abbhakan/bb491f4a6945d9c6b169 to your computer and use it in GitHub Desktop.
Assignment 4
.d3-slider {
position: relative;
font-family: Verdana,Arial,sans-serif;
font-size: 1.1em;
border: 1px solid #aaaaaa;
z-index: 2;
}
.d3-slider-horizontal {
height: .8em;
}
.d3-slider-range {
background:#2980b9;
left:0px;
right:0px;
height: 0.8em;
position: absolute;
}
.d3-slider-range-vertical {
background:#2980b9;
left:0px;
right:0px;
position: absolute;
top:0;
}
.d3-slider-vertical {
width: .8em;
height: 100px;
}
.d3-slider-handle {
position: absolute;
width: 1.2em;
height: 1.2em;
border: 1px solid #d3d3d3;
border-radius: 4px;
background: #eee;
background: linear-gradient(to bottom, #eee 0%, #ddd 100%);
z-index: 3;
}
.d3-slider-handle:hover {
border: 1px solid #999999;
}
.d3-slider-horizontal .d3-slider-handle {
top: -.3em;
margin-left: -.6em;
}
.d3-slider-axis {
position: relative;
z-index: 1;
}
.d3-slider-axis-bottom {
top: .8em;
}
.d3-slider-axis-right {
left: .8em;
}
.d3-slider-axis path {
stroke-width: 0;
fill: none;
}
.d3-slider-axis line {
fill: none;
stroke: #aaa;
shape-rendering: crispEdges;
}
.d3-slider-axis text {
font-size: 11px;
}
.d3-slider-vertical .d3-slider-handle {
left: -.25em;
margin-left: 0;
margin-bottom: -.6em;
}
/*
D3.js Slider
Inspired by jQuery UI Slider
Copyright (c) 2013, Bjorn Sandvik - http://blog.thematicmapping.org
BSD license: http://opensource.org/licenses/BSD-3-Clause
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['d3'], factory);
} else if (typeof exports === 'object') {
if (process.browser) {
// Browserify. Import css too using cssify.
require('./d3.slider.css');
}
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('d3'));
} else {
// Browser globals (root is window)
root.d3.slider = factory(root.d3);
}
}(this, function (d3) {
return function module() {
"use strict";
// Public variables width default settings
var min = 0,
max = 100,
step = 0.01,
animate = true,
orientation = "horizontal",
axis = false,
margin = 50,
value,
active = 1,
snap = false,
scale;
// Private variables
var axisScale,
dispatch = d3.dispatch("slide", "slideend"),
formatPercent = d3.format(".2%"),
tickFormat = d3.format(".0"),
handle1,
handle2 = null,
divRange,
sliderLength;
function slider(selection) {
selection.each(function() {
// Create scale if not defined by user
if (!scale) {
scale = d3.scale.linear().domain([min, max]);
}
// Start value
value = value || scale.domain()[0];
// DIV container
var div = d3.select(this).classed("d3-slider d3-slider-" + orientation, true);
var drag = d3.behavior.drag();
drag.on('dragend', function () {
dispatch.slideend(d3.event, value);
})
// Slider handle
//if range slider, create two
// var divRange;
if (toType(value) == "array" && value.length == 2) {
handle1 = div.append("a")
.classed("d3-slider-handle", true)
.attr("xlink:href", "#")
.attr('id', "handle-one")
.on("click", stopPropagation)
.call(drag);
handle2 = div.append("a")
.classed("d3-slider-handle", true)
.attr('id', "handle-two")
.attr("xlink:href", "#")
.on("click", stopPropagation)
.call(drag);
} else {
handle1 = div.append("a")
.classed("d3-slider-handle", true)
.attr("xlink:href", "#")
.attr('id', "handle-one")
.on("click", stopPropagation)
.call(drag);
}
// Horizontal slider
if (orientation === "horizontal") {
div.on("click", onClickHorizontal);
if (toType(value) == "array" && value.length == 2) {
divRange = d3.select(this).append('div').classed("d3-slider-range", true);
handle1.style("left", formatPercent(scale(value[ 0 ])));
divRange.style("left", formatPercent(scale(value[ 0 ])));
drag.on("drag", onDragHorizontal);
var width = 100 - parseFloat(formatPercent(scale(value[ 1 ])));
handle2.style("left", formatPercent(scale(value[ 1 ])));
divRange.style("right", width+"%");
drag.on("drag", onDragHorizontal);
} else {
handle1.style("left", formatPercent(scale(value)));
drag.on("drag", onDragHorizontal);
}
sliderLength = parseInt(div.style("width"), 10);
} else { // Vertical
div.on("click", onClickVertical);
drag.on("drag", onDragVertical);
if (toType(value) == "array" && value.length == 2) {
divRange = d3.select(this).append('div').classed("d3-slider-range-vertical", true);
handle1.style("bottom", formatPercent(scale(value[ 0 ])));
divRange.style("bottom", formatPercent(scale(value[ 0 ])));
drag.on("drag", onDragVertical);
var top = 100 - parseFloat(formatPercent(scale(value[ 1 ])));
handle2.style("bottom", formatPercent(scale(value[ 1 ])));
divRange.style("top", top+"%");
drag.on("drag", onDragVertical);
} else {
handle1.style("bottom", formatPercent(scale(value)));
drag.on("drag", onDragVertical);
}
sliderLength = parseInt(div.style("height"), 10);
}
if (axis) {
createAxis(div);
}
function createAxis(dom) {
// Create axis if not defined by user
if (typeof axis === "boolean") {
axis = d3.svg.axis()
.ticks(Math.round(sliderLength / 100))
.tickFormat(tickFormat)
.orient((orientation === "horizontal") ? "bottom" : "right");
}
// Copy slider scale to move from percentages to pixels
axisScale = scale.ticks ? scale.copy().range([0, sliderLength]) : scale.copy().rangePoints([0, sliderLength], 0.5);
axis.scale(axisScale);
// Create SVG axis container
var svg = dom.append("svg")
.classed("d3-slider-axis d3-slider-axis-" + axis.orient(), true)
.on("click", stopPropagation);
var g = svg.append("g");
// Horizontal axis
if (orientation === "horizontal") {
svg.style("margin-left", -margin + "px");
svg.attr({
width: sliderLength + margin * 2,
height: margin
});
if (axis.orient() === "top") {
svg.style("top", -margin + "px");
g.attr("transform", "translate(" + margin + "," + margin + ")");
} else { // bottom
g.attr("transform", "translate(" + margin + ",0)");
}
} else { // Vertical
svg.style("top", -margin + "px");
svg.attr({
width: margin,
height: sliderLength + margin * 2
});
if (axis.orient() === "left") {
svg.style("left", -margin + "px");
g.attr("transform", "translate(" + margin + "," + margin + ")");
} else { // right
g.attr("transform", "translate(" + 0 + "," + margin + ")");
}
}
g.call(axis);
}
function onClickHorizontal() {
if (toType(value) != "array") {
var pos = Math.max(0, Math.min(sliderLength, d3.event.offsetX || d3.event.layerX));
moveHandle(scale.invert ?
stepValue(scale.invert(pos / sliderLength))
: nearestTick(pos / sliderLength));
}
}
function onClickVertical() {
if (toType(value) != "array") {
var pos = sliderLength - Math.max(0, Math.min(sliderLength, d3.event.offsetY || d3.event.layerY));
moveHandle(scale.invert ?
stepValue(scale.invert(pos / sliderLength))
: nearestTick(pos / sliderLength));
}
}
function onDragHorizontal() {
if ( d3.event.sourceEvent.target.id === "handle-one") {
active = 1;
} else if ( d3.event.sourceEvent.target.id == "handle-two" ) {
active = 2;
}
var pos = Math.max(0, Math.min(sliderLength, d3.event.x));
moveHandle(scale.invert ?
stepValue(scale.invert(pos / sliderLength))
: nearestTick(pos / sliderLength));
}
function onDragVertical() {
if ( d3.event.sourceEvent.target.id === "handle-one") {
active = 1;
} else if ( d3.event.sourceEvent.target.id == "handle-two" ) {
active = 2;
}
var pos = sliderLength - Math.max(0, Math.min(sliderLength, d3.event.y))
moveHandle(scale.invert ?
stepValue(scale.invert(pos / sliderLength))
: nearestTick(pos / sliderLength));
}
function stopPropagation() {
d3.event.stopPropagation();
}
});
}
// Move slider handle on click/drag
function moveHandle(newValue) {
var currentValue = toType(value) == "array" && value.length == 2 ? value[active - 1]: value,
oldPos = formatPercent(scale(stepValue(currentValue))),
newPos = formatPercent(scale(stepValue(newValue))),
position = (orientation === "horizontal") ? "left" : "bottom";
if (oldPos !== newPos) {
if (toType(value) == "array" && value.length == 2) {
value[ active - 1 ] = newValue;
if (d3.event) {
dispatch.slide(d3.event, value );
};
} else {
if (d3.event) {
dispatch.slide(d3.event.sourceEvent || d3.event, value = newValue);
};
}
if ( value[ 0 ] >= value[ 1 ] ) return;
if ( active === 1 ) {
if (toType(value) == "array" && value.length == 2) {
(position === "left") ? divRange.style("left", newPos) : divRange.style("bottom", newPos);
}
if (animate) {
handle1.transition()
.styleTween(position, function() { return d3.interpolate(oldPos, newPos); })
.duration((typeof animate === "number") ? animate : 250);
} else {
handle1.style(position, newPos);
}
} else {
var width = 100 - parseFloat(newPos);
var top = 100 - parseFloat(newPos);
(position === "left") ? divRange.style("right", width + "%") : divRange.style("top", top + "%");
if (animate) {
handle2.transition()
.styleTween(position, function() { return d3.interpolate(oldPos, newPos); })
.duration((typeof animate === "number") ? animate : 250);
} else {
handle2.style(position, newPos);
}
}
}
}
// Calculate nearest step value
function stepValue(val) {
if (val === scale.domain()[0] || val === scale.domain()[1]) {
return val;
}
var alignValue = val;
if (snap) {
alignValue = nearestTick(scale(val));
} else{
var valModStep = (val - scale.domain()[0]) % step;
alignValue = val - valModStep;
if (Math.abs(valModStep) * 2 >= step) {
alignValue += (valModStep > 0) ? step : -step;
}
};
return alignValue;
}
// Find the nearest tick
function nearestTick(pos) {
var ticks = scale.ticks ? scale.ticks() : scale.domain();
var dist = ticks.map(function(d) {return pos - scale(d);});
var i = -1,
index = 0,
r = scale.ticks ? scale.range()[1] : scale.rangeExtent()[1];
do {
i++;
if (Math.abs(dist[i]) < r) {
r = Math.abs(dist[i]);
index = i;
};
} while (dist[i] > 0 && i < dist.length - 1);
return ticks[index];
};
// Return the type of an object
function toType(v) {
return ({}).toString.call(v).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
};
// Getter/setter functions
slider.min = function(_) {
if (!arguments.length) return min;
min = _;
return slider;
};
slider.max = function(_) {
if (!arguments.length) return max;
max = _;
return slider;
};
slider.step = function(_) {
if (!arguments.length) return step;
step = _;
return slider;
};
slider.animate = function(_) {
if (!arguments.length) return animate;
animate = _;
return slider;
};
slider.orientation = function(_) {
if (!arguments.length) return orientation;
orientation = _;
return slider;
};
slider.axis = function(_) {
if (!arguments.length) return axis;
axis = _;
return slider;
};
slider.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return slider;
};
slider.value = function(_) {
if (!arguments.length) return value;
if (value) {
moveHandle(stepValue(_));
};
value = _;
return slider;
};
slider.snap = function(_) {
if (!arguments.length) return snap;
snap = _;
return slider;
};
slider.scale = function(_) {
if (!arguments.length) return scale;
scale = _;
return slider;
};
d3.rebind(slider, dispatch, "on");
return slider;
}
}));
Country 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
Australia 414973.7 416477.89 420764.25 422801.08 423232.13 436863.96 443213.16 455692.63 470580.22 479616.71 489812.92 502347.13 503584.93 506235.37 519037.24 523479.26 529885.15 537930.78 544573.76 541177.63 540210.87 541542.76 543648.45
Austria 78086.35 82135.09 75410.77 75484.12 76345.45 79743.56 82754.78 82277.81 81653.02 79966.28 80276.96 84274.66 85975.57 91984.6 91569.35 92580.94 89710.79 86967.42 86882.03 80147.97 84807.85 82760.84 80059.36
Belgium 142952.13 144950.64 143694.93 142765.54 148485.04 150326.89 154307.66 145679.42 151211.31 144947.2 145856.88 145182.68 144717.56 145316.45 146397.6 142063.28 138341.86 133440.16 135823.29 123208.52 130610.94 120145.51 116520.32
Canada 590908.11 583211.91 600162.23 602008.18 622358.35 639072.03 661055.11 675981.97 683279.18 696158.27 721362.48 713949.96 719623.39 740178.7 743568.33 735829.05 727849.65 749288.91 731080.7 689313.24 699302.26 701212.37 698626.47
Czech Republic 196145.7 182192.75 165624.18 159466.81 149435.24 151773.53 155539.54 151816.23 144667.47 137106.75 146330.13 146326.41 142844.95 145827.26 147274.23 145965.05 147021.15 147245.85 142184.64 134205.66 137007.81 135276.54 131466.12
Denmark 70020.49 80532.19 74462.61 76641.83 80590.54 77280.44 90235.74 80739.74 76937.81 74271 69954.8 71548.51 70932.84 75837.35 69889.78 65588.79 73469.67 68920.42 65404.39 62511.41 63006.53 58051.67 53118.01
Estonia 40614.54 37439.42 27385.1 21251.03 21900.65 20064.37 20726.23 20331.52 18811.16 17450.52 17156.96 17542.39 16935.3 18810.48 19129.07 18421.21 17837.32 20948.75 19545.92 16188.5 19892.34 20483.96 19188.43
Finland 70328.96 68141.96 66720.51 68814.65 74204.2 70767.9 76491.71 75111.84 71531.01 70985.09 69188.4 74400.02 76624.5 84577.2 80583.77 68624.26 79900.3 78248.9 70126.26 66003.04 74397.39 66861.11 60965.73
France 560383.96 584125.17 574864.49 547997.28 548585.62 556875.45 571572.27 566289.54 581450.75 567058.44 564597.28 562987.91 557941.21 563354.03 561771.57 563576.88 551867.76 542720.66 537952.87 514380.38 522155.78 495981.68 496221.21
Germany 1248048.77 1201034.15 1150981.16 1141687.05 1121879.99 1117579.85 1136718.31 1100977.55 1075180.38 1041303.66 1040367.33 1055173.88 1033944.94 1032297.37 1019806.05 994459.68 1002426.45 976583.75 979802.7 912605.83 946388.27 928694.56 939083.31
Greece 104926.55 104525 105963.36 105049.8 107814.49 109717.66 112765.57 117585.67 123176.58 123027.51 126578.61 127510.38 127446.22 131257.18 131706.68 135310.59 131793.69 134636.56 130758.06 124109.98 117877.65 114728.07 110985.47
Hungary 97602.59 89744.26 79723.71 80262.61 79214.82 78474.71 80706.49 79269.91 79095.35 79687.63 76504.3 78359.24 76879.85 79604.12 79106.67 78376.04 77485.25 75650.65 73327.97 66975.65 67637.97 66034.09 61980.66
Iceland 3538.08 3373.69 3280.55 3342.65 3275.95 3315.39 3405.79 3559.14 3690.18 3919.17 3902.65 3869.83 3902.75 3878.58 3931.18 3859.26 4390.94 4619.47 5021.79 4779.27 4646.16 4441.13 4467.73
Ireland 55246.27 56017.48 56006.31 56322.71 57752.38 58903.21 61001.23 62510.37 65317.47 66281.89 68216.34 70207.5 68337.7 68467.13 68184.47 69655.66 69165.75 68370.74 68020.49 62312.26 61894.9 57749.96 58531.24
Italy 519054.9 520606.01 517782.73 511266.65 503551.64 530332.58 524037.79 530463.76 541857.68 548318.91 551237.06 557143.87 558301.69 573604.49 576843.08 574261.76 563373.42 555077.9 540620.5 490112.81 499358.6 486601.13 460083.45
Japan 1234320.12 1245823.29 1256111.75 1250768.52 1314837.52 1335888.27 1349668.79 1343308.19 1300747.04 1321896 1340522.55 1315673.33 1347823.81 1351663.74 1347639.97 1350321.36 1332532.63 1364257.69 1280903.32 1205672.64 1256094.69 1306517.93 1343117.72
Latvia 26212.92 24298.68 19568.81 15818.01 13893.49 12502.58 12517.29 11961 11437.09 10635.69 9993.58 10625.78 10604.55 10856.07 10852.07 11056.34 11522.36 11978.93 11496.15 10849.53 11987.43 11139.89 10978.48
Lithuania 48720.77 50105.04 30206.45 24301.31 22861.75 22072.26 23358.61 23055.05 23813.2 21255.73 19632.39 20716.15 21231.87 21447.71 22231.13 23318.5 23707.75 26119.08 24932.26 20431.63 21118.58 21679.98 21622.29
Luxembourg 12901.05 13446.77 13221.73 13333.88 12505.1 10177.49 10238.59 9534.45 8644.45 9062.37 9762 10258.25 11036.51 11381.76 12862.16 13095.2 12946.34 12360.72 12188.4 11683.85 12249.56 12124.93 11839.24
Netherlands 211849.59 216385.16 215098.97 219979.89 219933.69 223161.32 231327.16 224605.61 225494.05 213316.45 213023.12 214494.43 213544.38 214303.67 215514.09 209448.24 205558.92 204199.49 203313.53 197787.1 209286.43 195063.51 191668.7
New Zealand 60641.44 61615.13 62986.84 62644.42 63734.65 64464.84 66618.86 69138.05 67013.77 68979.31 70898.9 73619.06 74330.99 76807.65 76206.61 78286.62 78185.8 76221.51 75763.7 73101.16 73491.33 74393.45 76047.98
Norway 50409.35 48320.85 46573.39 48491.89 50447.41 50241.89 53377.75 53329.81 53502.03 54529.36 54058.49 55276.06 54140.02 54886.35 55438.48 54469.02 54288 56006.29 54424.51 51808.95 54346.96 53294.03 52733.24
Poland 466371.96 456203.77 442010.1 442072.58 438414.18 441102.72 454106.41 445808.23 416877.25 406496.5 396103.65 392886.22 380353.5 393407.05 398043.98 398827.04 414148.4 415449.44 406081.06 387700.41 407474.65 405741.44 399267.97
Portugal 60766.81 62683.22 67105.23 65821.06 66889.55 71398.88 69096.22 72159.32 77107.15 85223.52 84100.41 83872.45 88037.62 82327.79 85298.69 87685.99 82647.14 80269.39 78031.86 74853.97 70634.19 69316.55 68751.89
Russia 3363342.44 3186714.69 2698720.99 2561126.04 2291944.08 2207675.85 2144998.5 2039481.49 2003522.12 2036766.56 2053320.98 2078134.5 2080417.35 2118715.58 2152593.01 2135398.16 2201493.69 2206099.7 2245851.34 2130321.36 2221342.01 2284292.82 2295045.38
Slovak Republic 73226.77 63395.54 57780.37 54214.37 51801.73 53231.67 53658.4 52560.77 51859.38 50809.84 48947.36 51478.96 49919.34 50670.53 50933.1 50263.74 50317.6 48395.32 49001.04 44690.17 45382.46 44697.89 42710.2
Slovenia 18444.42 17320.7 17208.76 17450.39 17640.88 18548.59 19224.38 19585.6 19345.12 18693.85 18953.36 19819.74 19977.38 19672.15 19980.12 20313.71 20526.18 20671.8 21384.37 19373.15 19411.38 19462.56 18910.98
Spain 283749.23 293164.79 301709.76 291118.16 307473.16 322108.19 314841.92 328188.89 338022.05 364001.49 380004.18 376963.35 394905.23 402420.16 417194.61 431392.66 423788.78 432111.64 398444.15 359659.15 347181 345887.15 340808.59
Sweden 72713.85 72884.06 72412.49 72442.83 74898.45 74151.76 78017.33 72928.08 73396.87 70050.09 68562.87 69344.23 70067.57 70469.96 69698.88 66912.77 66778.43 65232.81 63013.87 59097.38 65071.97 60754.24 57604.15
Switzerland 52889.94 54607.28 54370.31 51594.95 50760.13 51575.62 52232.84 51318.61 52663.38 52679.67 51774.81 52804.53 51710.22 52835.35 53503.19 54209.49 53845.81 51910.3 53653.05 52366.48 54095.22 49973.23 51449.02
Turkey 188434.23 200654 211729.35 223080.22 218530.04 238820.28 259939.01 273172.46 275193.34 275905.57 298090.87 279074.9 287052.04 303537.09 313071.02 330740.34 350881.15 382378.4 368734.42 371149.35 403494.7 424090.95 439873.73
United Kingdom 778805.3 786300.13 762457.67 743586.13 733508.83 726758.05 748199.17 723818.76 722987.22 692181.89 693693.37 699212.55 680632.45 687042.24 684160.99 678252.8 675547.22 666079.29 646736 593379.87 609147.47 566268.75 584304.29
United States 6219524.02 6195074.54 6295803.99 6430339.4 6499923.37 6597665.21 6812323.32 6867121.18 6868760.62 6930957.16 7075609.42 6979196.38 7011195.7 7057538.8 7198378.99 7228293.16 7150743.56 7287750.12 7090753.13 6642319.59 6854728.19 6716993.02 6487847.05
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Greenhouse gas emissions</title>
<link rel="stylesheet" href="d3.slider.css" />
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<script type="text/javascript" src="d3.slider.js"></script>
<style type="text/css">
body {
background-color: white;
}
svg {
background-color: white;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.y.axis path,
.y.axis line {
opacity: 0;
}
.slider {
float: left;
width: 1000px;
margin-left: 200px;
}
.chart {
float: left;
width:1200px;
}
.countries {
float: left;
width: 100px;
}
.flags {
float: left;
width: 60px;
}
.selectedYear {
display: block;
position: absolute;
top:400px;
left:600px;
font-size: 7em;
}
</style>
</head>
<body>
<h1>Greenhouse gas emissions</h1>
<p>Greenhouse gas emissions from 1990 - 2012 by OECD country. The unit is in houndred thousands tonnes of CO2 equivalent. Source: <a href="http://www.oecd-ilibrary.org/environment/data/oecd-environment-statistics/greenhouse-gas-emissions_data-00594-en">OECD</a>, 2015</p>
<div id="myFlags" class="flags"></div>
<div id="myCountries" class="countries"></div>
<div id="myChart" class="chart" ></div>
<div id="mySlider" class="slider"></div>
<span id="mySlidertext" class="selectedYear">0</span>
<script type="text/javascript">
var w = 1000;
var h = 800;
var padding = [ 20, 10, 20, 100 ]; //Top, right, bottom, left
var barPadding = 5;
var offsetX = 140;
//var barMax = 8000;
var color = "Blue";
var focusColor = "Magenta";
var borderColor = "Black";
var baseUrl = "http://localhost:99/module4/assignment/bb491f4a6945d9c6b169/";
// Check if running on localhost or not
var host = window.location.host;
if (host.indexOf("localhost") == -1)
{
baseUrl = "http://bl.ocks.org/abbhakan/raw/bb491f4a6945d9c6b169/";
}
var widthScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1);
var xScale = d3.scale.linear()
.range([ 0, w - padding[1] - padding[3] ]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("top");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
var svg = d3.select("#myChart")
.append("svg")
.attr("width", w)
.attr("height", h);
var myFlags = d3.select("#myFlags")
.append("svg")
.attr("width", 60)
.attr("height", h);
function draw(year) {
d3.csv("GreenhouseEmissions.csv", function(data) {
data.sort(function(a, b) {
//return d3.descending(a["1990"], b["1990"]);
//If your numeric values aren't sorting properly,
//try commenting out the line above, and instead using:
//
return d3.descending(+a[year], +b[year]);
//
//Data coming in from the CSV is saved as strings (text),
//so the + signs here force JavaScript to treat those
//strings instead as numeric values, thereby fixing the
//sort order (hopefully!).
});
widthScale.domain([ 0, d3.max(data, function(d) {
return +d[year];
}) ]);
heightScale.domain(data.map(function(d) { return d.Country; } ));
xScale.domain([ 0, d3.max(data, function(d) {
return +d[year]/1000;
}) ]);
// enter selection
var rects = svg.selectAll("rect")
.data(data);
var labels = svg.selectAll("text")
.data(data);
var imgs = myFlags.selectAll("images")
.data(data);
var imgBorders = myFlags.selectAll("rect")
.data(data);
// enter data
rects.enter().append("rect")
.attr("id", function(d,i) {
return "rect" + i;
})
.attr("x", offsetX)
.attr("y", function(d) {
return heightScale(d.Country).toString();
})
.attr("width", function(d) {
return widthScale(d[year]);
})
.attr("height", heightScale.rangeBand())
.style("fill", color)
.on("mouseover", function(d,i) { setFocus(i);})
.on("mouseout", function(d,i) { unFocus(i);})
.append("title")
.text(function(d) {
return d.Country + "'s emission is " + d3.round(d[year]/1000);
});
labels.enter().append("text")
.text(function(d) {
return d3.round(d[year]/1000);
})
.attr("height", heightScale.rangeBand())
.style("fill", color)
.style("text-anchor", "end")
.on("mouseover", function(d,i) { setFocus(i);})
.on("mouseout", function(d,i) { unFocus(i);})
.attr("id", function(d,i) {
return "label" + i;
})
.attr("x", function(d, i) {
return w - 870;
})
.attr("y", function(d,i) {
return heightScale(d.Country) + 15;
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + (padding[3] + offsetX - 100) + "," + padding[0] + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.style("fill", color)
.attr("transform", "translate(" + (padding[3] - 5 ) + ",0)")
.call(yAxis);
imgs.enter().append("svg:image")
.style("float", "left")
.attr("height", heightScale.rangeBand())
.on("mouseover", function(d,i) { setFocus(i);})
.on("mouseout", function(d,i) { unFocus(i);})
.attr("id", function(d,i) {
return "img" + i;
})
.attr("xlink:href", function(d, i) { return baseUrl + "" + d.Country + "S.png";})
.attr("width", "30")
.attr("height", "20")
.attr("x", "30")
.attr("y", function(d, i) {
return heightScale(d.Country);;
});
imgBorders.enter().append("rect")
.attr("id", function(d,i) {
return "border" + i;
})
.attr("x", "30")
.attr("y", function(d, i) {
return heightScale(d.Country);
})
.attr("width", "30")
.attr("height", "20")
.style("stroke", borderColor)
.style("fill", "none")
.on("mouseover", function(d,i) { setFocus(i);})
.on("mouseout", function(d,i) { unFocus(i);});
// remove data
rects.exit().remove();
labels.exit().remove();
imgs.exit().remove();
imgBorders.exit().remove();
// update data
rects
.transition()
.duration(750)
.attr("y", function(d) {
return heightScale(d.Country);
})
.attr("width", function(d) {
return widthScale(d[year]);
})
.select("title").text(function(d) {
return d.Country + "'s emission is " + d3.round(d[year]/1000);
});
labels
.text(function(d) {
return d3.round(d[year]/1000);
});
svg.selectAll("x.axis")
.call(xAxis);
svg.selectAll("y.axis")
.call(yAxis);
imgs
.attr("xlink:href", function(d, i) { return baseUrl + "" + d.Country + "S.png";});
})
};
function setFocus(index)
{
var rectName = "rect" + index;
var labelName = "label" + index;
var countryName = "country" + index;
var imgName = "img" + index;
var borderName = "border" + index;
d3.select("#" + rectName).style("fill", focusColor)
d3.select("#" + labelName).style("fill", focusColor)
d3.select("#" + imgName).style("fill", focusColor)
d3.select("#" + borderName).style("stroke", focusColor)
}
function unFocus(index)
{
var rectName = "rect" + index;
var labelName = "label" + index;
var countryName = "country" + index;
var imgName = "img" + index;
var borderName = "border" + index;
d3.select("#" + rectName).style("fill", color)
d3.select("#" + labelName).style("fill", color)
d3.select("#" + imgName).style("fill", color)
d3.select("#" + borderName).style("stroke", borderColor)
}
function initAxis() {
}
function onLoad(year) {
initAxis();
draw(year);
}
function update(value) {
draw(value);
};
var startYear = "1990";
window.onload = onLoad(startYear);
d3.select("#mySlidertext").text(startYear);
d3.select("#mySlider").call(d3.slider().axis(true).min(1990).max(2012).step(1).on("slide", function(evt, value) {
d3.select("#mySlidertext").text(value);
update(value.toString());
}));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment