-
-
Save mayblue9/5cab2556eda26bd9502f to your computer and use it in GitHub Desktop.
Bubble chart w/ mouse over
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
____________________________________ | |
I___|___|___|___|___|__|____|___|__|_I | |
I_|___|___| |___|___|__I | |
)\ I___|__ | ..,a@@@a,a@@@a,.. |___|____I /( | |
( )) I_|__ .,;*;;@@@@@a@@@@@;;;;,. ___|__I (( ) | |
: I__| ;;;;;;;;;a@@^@@a;;;*;;;;; __|_I : | |
,uU I_| ;;;;*;;;a@@@ @@@a;;;;*;;; |__I Uu. | |
:Uu I__|;;;;;;;a@@@@ .@@@@@;;;;;;;; __|I uU: | |
| | I_| ;;*;;;a@@@@@ @@'`@@@;;;;;*; _|_I | | | |
|_| I__ ;;;;;;@@;;@@ `@ `@;;;*;;;; ___I |_| | |
_ (___) _ I_|_ ;;;*;;@;;;;@;;;;;*;;;;;;;;; _|__I___ (___) | |
,-' ) ( ~~~~~ `;;;;;;*;;;;;;;;;;;*;;;;' ~~~~~ ) (`-. | |
,-____=====_____________`;;;;;;;;*;;;;;;;'_______________=====___`. | |
|~~| _________________________________________________/o\___ |~~| | |
|_|| ||____|____|____|____|____|____|____|____|____|_/ /,\__| ||_| | |
|__| |___|____|____|____|____|____|____|____|____|__/ /,,,\|| ||_| | |
|_||__||____|____|____|____|____|____|____|____|____|\/,,,,,\|__|__| | |
|____|____|____|____|____|____|____|____|____|____|___\,,,,,,\___|_| | |
|_|____|__I####I.......... /%%%%%%%%%%%\ ..........I##\,,,,( )|___| | |
|____|____I####I.......... .%%%%%( )%%%%%. .........I###\,,,,\/__|_| | |
|_|____|__I####I.......... @@%%%%0%0%%%%@@ ........I## /,,,,/_|___| | |
|____|____I####I.......... `@@@@@@@@@@@@@@' ........I# /,,,,/____|_| | |
|_|____|__I####I............ \\\\\\\\\\\\\) ........I ( \,,/___|___| | |
|____|____I####I............. `\\\\\\\\\\) ........I \_)/_|____|_| | |
|_|____|__I####I............ A `\\\\\\\' .. .. I# :_|____|___| | |
|____|____I####I......... AAA .. `\\\' .. A. .I###I___|____|_| | |
|_|____|__I####I...... .A `AAA .... * .. AAA. I ##I_|____|___| | |
|____|____I####I.... AAA AA;AA ... ... `AAA.I ##I___|____|_| | |
|_|____|__I####/~~~,-.A;;A'-A;;;;;A-----A-----,A;;;A ##I_|____|___| | |
|____|____I###/ I.;;;;; ;;;;;;; AAA I;;;A'\###I___|____|_| | |
|_|____|__I##/ I;;;;;; ;;;;;;; A;;;A I;;;; \##I_|____|___| | |
|____|____I#/ !~;;;;;;~~~~~~~~~~~;;;;;;~~~~~!;' \#I___|____|_| | |
|_|____|__I/______! ::::;; ;;;;;; !______\I_|____|___| | |
~~~~~~~~~/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \~~~~~~~~~~ | |
/_______________________________________________\ | |
/\____/\ __ | |
.' """" `,-' `--.__ | |
__,- : - - ; " :: `-. -.__ | |
,-sssss `._ `' _,'" ,'~~~::`.sssss-. | |
|ssssss ,' ,_`--'_ __,' :: ` `.ssssss| | |
|sssssss `-._____~ `,,'_______,---_;; ssssss| | |
|ssssssssss `--'~{__ ____ ,'ssssss| | |
`-ssssssssssssssssss ~~~~~~~~~~~~ ssss.-' | |
`---.sssssssssssssssssssss.---' | |
*/ | |
var w = 800; // width | |
var h = 600; // height | |
var margin = 100; // margin | |
var xlabel = "murder"; // what we want on the x axis | |
var ylabel = "robbery"; // what we want on the y axis | |
var rlabel = "population"; // what we want for the bubble area | |
d3.select(".container").append("svg:svg") | |
.attr("width", w+margin) | |
.attr("height", h+margin) | |
.attr("class","chart"); | |
var chart = d3.select(".chart").append("svg:g") | |
.attr("class","content") | |
.attr("transform", "translate(" + margin/2 + "," + margin/2 + ")"); // The transform with trasnlate will shift our group down and to the right | |
/* | |
The following statement creates a line for our x axis | |
*/ | |
chart.append("svg:line") | |
.attr("stroke-width", 1) | |
.attr( "stroke","gray") | |
.attr("x1", 0) | |
.attr("y1", h) | |
.attr("x2", w) | |
.attr("y2", h); | |
/* | |
The following statement creates a line for our y axis | |
*/ | |
chart.append("svg:line") | |
.attr("stroke-width", 1) | |
.attr( "stroke","gray") | |
.attr("x1", 1) | |
.attr("y1", 0) | |
.attr("x2", 1) | |
.attr("y2", h-1); | |
d3.csv( "data.csv", function(csv) { | |
var xMax = d3.max( csv.map( function(d) { | |
return parseFloat(d[xlabel]); | |
})); | |
var yMax = d3.max( csv.map( function(d) { | |
return parseFloat( d[ylabel] ); | |
})); | |
var xgrid = d3.scale.linear() | |
.domain([0.0,xMax]) // range of data | |
.range([0.0,w]); // range of width | |
// 0 in, 0 out | |
// 10 in, w out | |
var ygrid = d3.scale.linear() | |
.domain([0.0,yMax]) // range of data | |
.range([h,0.0]); // range of height | |
// 0 in, h out | |
// 260 in, 0 out | |
// I didn't want mine to change colors, but left this here for just in case | |
var colorScale = d3.scale.linear() | |
.domain([0,500]) | |
.range(["steelblue", "steelblue"]); | |
var radius = function(d,i) { | |
return Math.sqrt( d[rlabel] ) / 150; // FUN FACT: we could use another scale function here instead of arbitrarily using 150 | |
} | |
var xpos = function(d,i) { | |
return xgrid( d[xlabel] ); // d.murder in, corresponding x value out | |
} | |
var ypos = function(d,i) { | |
return ygrid( d[ylabel] ); // d.robbery in, corresponding x value out | |
} | |
var color = function(d,i) { | |
return colorScale( d["aggravated_assault"] ); | |
} | |
var mouseMove = function() { | |
var infobox = d3.select(".infobox"); | |
var coord = d3.svg.mouse(this) | |
// now we just position the infobox roughly where our mouse is | |
infobox.style("left", coord[0] + 15 + "px" ); | |
infobox.style("top", coord[1] + "px"); | |
} | |
var mouseOver = function(d) { | |
var bubble = d3.select(this); | |
bubble.attr("stroke", "#000") | |
.attr("stroke-width", 4 ); | |
var infobox = d3.select(".infobox") | |
.style("display", "block" ); | |
infobox.select("p.state") | |
.text( d.state ); | |
infobox.select("p.xdata") | |
.text( xlabel + ": " + d[xlabel] ); | |
infobox.select("p.ydata") | |
.text( ylabel + ": " + d[ylabel] ); | |
} | |
var mouseOut = function() { | |
var infobox = d3.select(".infobox"); | |
infobox.style("display", "none" ) | |
var bubble = d3.select(this); | |
bubble.attr("stroke", "none") | |
} | |
// attach function to run when mouse is moved anywhere on svg | |
d3.select("svg") | |
.on("mousemove", mouseMove ); | |
// add <p> elements to our infobox. later we will enter our crime data there | |
var infobox = d3.select(".infobox"); | |
infobox.append("p") | |
.attr("class", "state" ); | |
infobox.append("p") | |
.attr("class", "xdata" ); | |
infobox.append("p") | |
.attr("class", "ydata" ); | |
// append bubbles and attach function to run when bubbles moused over and moused out | |
chart.selectAll("circle.bubble").data(csv) | |
.enter().append("svg:circle") | |
.attr("class", "bubble") | |
.attr("cx", xpos ) | |
.attr("cy", ypos ) | |
.attr("r", radius ) | |
.attr("fill", color ) | |
.on( "mouseover", mouseOver ) | |
.on( "mouseout", mouseOut ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
state | murder | forcible_rape | robbery | aggravated_assault | burglary | larceny_theft | motor_vehicle_theft | population | |
---|---|---|---|---|---|---|---|---|---|
Alabama | 8.2 | 34.3 | 141.4 | 247.8 | 953.8 | 2650 | 288.3 | 4545049 | |
Alaska | 4.8 | 81.1 | 80.9 | 465.1 | 622.5 | 2599.1 | 391 | 669488 | |
Arizona | 7.5 | 33.8 | 144.4 | 327.4 | 948.4 | 2965.2 | 924.4 | 5974834 | |
Arkansas | 6.7 | 42.9 | 91.1 | 386.8 | 1084.6 | 2711.2 | 262.1 | 2776221 | |
California | 6.9 | 26 | 176.1 | 317.3 | 693.3 | 1916.5 | 712.8 | 35795255 | |
Colorado | 3.7 | 43.4 | 84.6 | 264.7 | 744.8 | 2735.2 | 559.5 | 4660780 | |
Connecticut | 2.9 | 20 | 113 | 138.6 | 437.1 | 1824.1 | 296.8 | 3477416 | |
Delaware | 4.4 | 44.7 | 154.8 | 428.2 | 688.9 | 2144 | 278.5 | 839906 | |
Florida | 5 | 37.1 | 169.4 | 496.6 | 926.3 | 2658.3 | 423.3 | 17783868 | |
Georgia | 6.2 | 23.6 | 154.8 | 264.3 | 931 | 2751.1 | 490.2 | 9097428 | |
Hawaii | 1.9 | 26.9 | 78.5 | 147.8 | 767.9 | 3308.4 | 716.4 | 1266117 | |
Idaho | 2.4 | 40.4 | 18.6 | 195.4 | 564.4 | 1931.7 | 201.8 | 1425862 | |
Illinois | 6 | 33.7 | 181.7 | 330.2 | 606.9 | 2164.8 | 308.6 | 12674452 | |
Indiana | 5.7 | 29.6 | 108.6 | 179.9 | 697.6 | 2412 | 346.7 | 6253120 | |
Iowa | 1.3 | 27.9 | 38.9 | 223.3 | 606.4 | 2042.7 | 184.6 | 2949450 | |
Kansas | 3.7 | 38.4 | 65.3 | 280 | 689.2 | 2758.1 | 339.6 | 2741771 | |
Kentucky | 4.6 | 34 | 88.4 | 139.8 | 634 | 1685.8 | 210.8 | 4182293 | |
Louisiana | 9.9 | 31.4 | 118 | 435.1 | 870.6 | 2494.5 | 318.1 | 4497691 | |
Maine | 1.4 | 24.7 | 24.4 | 61.7 | 478.5 | 1832.6 | 102 | 1311631 | |
Maryland | 9.9 | 22.6 | 256.7 | 413.8 | 641.4 | 2294.3 | 608.4 | 5582520 | |
Massachusetts | 2.7 | 27.1 | 119 | 308.1 | 541.1 | 1527.4 | 295.1 | 6453031 | |
Michigan | 6.1 | 51.3 | 131.8 | 362.9 | 696.8 | 1917.8 | 476.5 | 10090554 | |
Minnesota | 2.2 | 44 | 92 | 158.7 | 578.9 | 2226.9 | 278.2 | 5106560 | |
Mississippi | 7.3 | 39.3 | 82.3 | 149.4 | 919.7 | 2083.9 | 256.5 | 2900116 | |
Missouri | 6.9 | 28 | 124.1 | 366.4 | 738.3 | 2746.2 | 443.1 | 5806639 | |
Montana | 1.9 | 32.2 | 18.9 | 228.5 | 389.2 | 2543 | 210.7 | 934801 | |
Nebraska | 2.5 | 32.9 | 59.1 | 192.5 | 532.4 | 2574.3 | 316.5 | 1751721 | |
Nevada | 8.5 | 42.1 | 194.7 | 361.5 | 972.4 | 2153.9 | 1115.2 | 2408804 | |
New Hampshire | 1.4 | 30.9 | 27.4 | 72.3 | 317 | 1377.3 | 102.1 | 1301415 | |
New Jersey | 4.8 | 13.9 | 151.6 | 184.4 | 447.1 | 1568.4 | 317.5 | 8621837 | |
New Mexico | 7.4 | 54.1 | 98.7 | 541.9 | 1093.9 | 2639.9 | 414.5 | 1916538 | |
New York | 4.5 | 18.9 | 182.7 | 239.7 | 353.3 | 1569.6 | 185.6 | 19330891 | |
North Carolina | 6.7 | 26.5 | 145.5 | 289.4 | 1201.1 | 2546.2 | 327.8 | 8669452 | |
North Dakota | 1.1 | 24.2 | 7.4 | 65.5 | 311.9 | 1500.3 | 166 | 635365 | |
Ohio | 5.1 | 39.8 | 163.1 | 143.4 | 872.8 | 2429 | 360.9 | 11475262 | |
Oklahoma | 5.3 | 41.7 | 91 | 370.5 | 1006 | 2644.2 | 391.8 | 3532769 | |
Oregon | 2.2 | 34.8 | 68.1 | 181.8 | 758.6 | 3112.2 | 529 | 3617869 | |
Pennsylvania | 6.1 | 28.9 | 154.6 | 235 | 451.6 | 1729.1 | 236.5 | 12418161 | |
Rhode Island | 3.2 | 29.8 | 72.1 | 146.1 | 494.2 | 1816 | 408.7 | 1064989 | |
South Carolina | 7.4 | 42.5 | 132.1 | 579 | 1000.9 | 2954.1 | 384.4 | 4256199 | |
South Dakota | 2.3 | 46.7 | 18.6 | 108.1 | 324.4 | 1343.7 | 108.4 | 780084 | |
Tennessee | 7.2 | 36.4 | 167.3 | 541.9 | 1026.9 | 2828.1 | 420.6 | 5995748 | |
Texas | 6.2 | 37.2 | 156.6 | 329.8 | 961.6 | 2961.7 | 408.7 | 22801920 | |
Utah | 2.3 | 37.3 | 44.3 | 143.4 | 606.2 | 2918.8 | 343.9 | 2499637 | |
Vermont | 1.3 | 23.3 | 11.7 | 83.5 | 491.8 | 1686.1 | 102.9 | 618814 | |
Virginia | 6.1 | 22.7 | 99.2 | 154.8 | 392.1 | 2035 | 211.1 | 7563887 | |
Washington | 3.3 | 44.7 | 92.1 | 205.8 | 959.7 | 3149.5 | 783.9 | 6261282 | |
West Virginia | 4.4 | 17.7 | 44.6 | 206.1 | 621.2 | 1794 | 210 | 1803920 | |
Wisconsin | 3.5 | 20.6 | 82.2 | 135.2 | 440.8 | 1992.8 | 226.6 | 5541443 | |
Wyoming | 2.7 | 24 | 15.3 | 188.1 | 476.3 | 2533.9 | 145.1 | 506242 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Bubble Chart</title> | |
<script type="text/javascript" src="https://github.com/mbostock/d3/raw/master/d3.js"></script> | |
<script type="text/javascript" src="https://github.com/mbostock/d3/raw/master/d3.csv.js"></script> | |
<style type="text/css"> | |
.container { | |
position: relative; | |
} | |
.infobox { | |
position: absolute; | |
display: none; | |
background-color: rgba(255,255,255,.75); | |
width: 200px; | |
padding: 10px; | |
} | |
p { | |
margin: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="infobox"> | |
</div> | |
</div> | |
</body> | |
<script type="text/javascript" src="bubble.js"></script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment