Skip to content

Instantly share code, notes, and snippets.

@maelafifi
Created May 7, 2017 02:24
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 maelafifi/b8330d6835e91bbb8f2ff6ddf80c7a31 to your computer and use it in GitHub Desktop.
Save maelafifi/b8330d6835e91bbb8f2ff6ddf80c7a31 to your computer and use it in GitHub Desktop.
scatterFinalStuff
license: mit
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<!-- Example based on http://bl.ocks.org/mbostock/3887118 -->
<!-- Tooltip example from http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html -->
<!-- Coding style based on http://gist.github.com/mbostock/5977197 -->
<style>
body {
font: 11px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 50, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
/*
* value accessor - returns the value to encode for a given data object.
* scale - maps value to a visual display encoding, such as a pixel position.
* map function - maps from data value to display value
* axis - sets up axis
*/
// setup x
var xValue = function(d) { return d.numWorkers;}, // data -> value
xScale = d3.scale.linear().range([0, width]), // value -> display
xMap = function(d) { return xScale(xValue(d));}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
// setup y
var yValue = function(d) { return d.medianEarnings;}, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d));}, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
// setup fill color
var cValue = function(d) { return d.medianEarnings;},
color = d3.scale.category10();
// add the graph canvas to the body of the webpage
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// add the tooltip area to the webpage
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// load data
d3.csv("noNullData.csv", function(error, data) {
// change string (from CSV) into number format
data.forEach(function(d) {
d.medianEarnings = +d.medianEarnings;
d.numWorkers = +d.numWorkers;
// console.log(d);
});
// don't want dots overlapping axis, so add in buffer to data domain
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
yScale.domain([d3.min(data, yValue)-1, d3.max(data, yValue)+1]);
// x-axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Calories");
// y-axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Protein (g)");
// draw dots
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", function(d) {
var x = d.Occupation;
if(x.includes("women"))
{
return "pink";
}
else
{
return "blue";
}
})
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.Occupation + "<br/> (" + xValue(d)
+ ", " + yValue(d) + ")")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
// draw legend
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
// draw legend colored rectangles
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
// draw legend text
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d;})
});
</script>
</body>
Occupation numWorkers medianEarnings
Chief executives 832 2419
General and operations managers 626 1358
Marketing and sales managers 518 1745
Administrative services managers 101 1398
Computer and information systems managers 443 1756
Financial managers 527 1670
Human resources managers 73 1737
Industrial production managers 201 1409
Purchasing managers 95 1400
Transportation storage and distribution managers 237 990
Education administrators 297 1528
Food service managers 410 853
Lodging managers 59 1068
Medical and health services managers 133 1610
Property real estate and community association managers 181 1157
Social and community service managers 115 1206
Managers all other 1845 1542
Wholesale and retail buyers except farm products 68 942
Purchasing agents except wholesale retail and farm products 128 1037
Claims adjusters appraisers examiners and investigators 114 1139
Compliance officers 106 1274
Human resources workers 166 1364
Management analysts 332 1586
Market research analysts and marketing specialists 116 1345
Business operations specialists all other 110 1263
Accountants and auditors 559 1441
Financial analysts 173 1747
Personal financial advisors 258 1714
Credit counselors and loan officers 151 1331
Computer systems analysts 314 1475
Computer programmers 300 1466
Software developers applications and systems software 1084 1863
Computer support specialists 367 1078
Computer occupations all other 418 1244
Operations research analysts 59 1382
Engineers all other 430 1659
Engineering technicians except drafters 275 1075
Medical scientists 69 1250
Physical scientists all other 131 1727
Miscellaneous life physical and social science technicians 62 949
Counselors 184 892
Social workers 127 1039
Clergy 315 1070
Lawyers 446 2086
Postsecondary teachers 535 1356
Elementary and middle school teachers 605 1126
Secondary school teachers 403 1146
Other teachers and instructors 156 1135
Teacher assistants 56 501
Designers 290 1267
Producers and directors 78 1228
Editors 64 1112
Pharmacists 89 2096
Physicians and surgeons 497 2343
Physical therapists 69 1348
Registered nurses 285 1261
Clinical laboratory technologists and technicians 97 1071
Diagnostic related technologists and technicians 79 1156
Emergency medical technicians and paramedics 114 821
Health practitioner support technologists and technicians 113 712
Nursing psychiatric and home health aides 194 534
Bailiffs correctional officers and jailers 275 793
Police and sheriff's patrol officers 609 1008
Security guards and gaming surveillance officers 576 597
Chefs and head cooks 288 632
First-line supervisors of food preparation and serving workers 174 666
Cooks 892 459
Food preparation workers 215 432
Bartenders 131 701
Combined food preparation and serving workers including fast food 72 381
Waiters and waitresses 342 504
Dining room and cafeteria attendants and bartender helpers 82 465
First-line supervisors of housekeeping and janitorial workers 119 702
Janitors and building cleaners 1139 564
Maids and housekeeping cleaners 120 497
First-line supervisors of gaming workers 68 840
Personal care aides 125 514
Recreation and fitness workers 92 668
First-line supervisors of retail sales workers 1321 857
First-line supervisors of non-retail sales workers 552 1161
Cashiers 402 475
Retail salespersons 1103 730
Advertising sales agents 122 1034
Insurance sales agents 218 1166
Securities commodities and financial services sales agents 155 1458
Sales representatives services all other 282 1202
Sales representatives wholesale and manufacturing 819 1140
Real estate brokers and sales agents 209 1222
Sales and related workers all other 103 946
First-line supervisors of office and administrative support workers 383 942
Bookkeeping accounting and auditing clerks 115 790
Customer service representatives 664 676
Receptionists and information clerks 103 600
Dispatchers 126 734
Postal service clerks 58 999
Postal service mail carriers 185 1020
Production planning and expediting clerks 113 935
Shipping receiving and traffic clerks 322 602
Stock clerks and order fillers 643 533
Secretaries and administrative assistants 133 831
Data entry keyers 51 655
Office clerks general 140 707
Office and administrative support workers all other 107 855
Miscellaneous agricultural workers 547 524
First-line supervisors of production and operating workers 599 972
Electrical electronics and electromechanical assemblers 55 618
Miscellaneous assemblers and fabricators 645 625
Bakers 55 562
Butchers and other meat poultry and fish processing workers 183 571
Laundry and dry-cleaning workers 55 486
Sewing machine operators 52 407
Inspectors testers sorters samplers and weighers 445 834
Packaging and filling machine operators and tenders 112 574
Production workers all other 641 690
Bus drivers 197 691
Driver/sales workers and truck drivers 2689 787
Industrial truck and tractor operators 510 604
Laborers and freight stock and material movers hand 1196 580
Packers and packagers hand 166 468
Chief executives women 318 1876
General and operations managers women 250 1037
Marketing and sales managers women 403 1142
Administrative services managers women 71 952
Computer and information systems managers women 150 1680
Financial managers women 578 1157
Human resources managers women 190 1283
Industrial production managers women 66 1219
Purchasing managers women 92 1169
Transportation storage and distribution managers women 51 988
Education administrators women 541 1280
Food service managers women 350 632
Lodging managers women 64 764
Medical and health services managers women 399 1254
Property real estate and community association managers women 221 815
Social and community service managers women 248 992
Managers all other women 1115 1188
Wholesale and retail buyers except farm products women 83 765
Purchasing agents except wholesale retail and farm products women 125 974
Claims adjusters appraisers examiners and investigators women 206 912
Compliance officers women 146 1163
Human resources workers women 465 1089
Management analysts women 253 1342
Market research analysts and marketing specialists women 130 1097
Business operations specialists all other women 146 1001
Accountants and auditors women 892 1018
Financial analysts women 104 1252
Personal financial advisors women 142 953
Credit counselors and loan officers women 200 926
Computer systems analysts women 182 1328
Computer programmers women 104 1312
Software developers applications and systems software women 266 1553
Computer support specialists women 124 1014
Computer occupations all other women 106 1055
Operations research analysts women 66 1300
Engineers all other women 63 1401
Engineering technicians except drafters women 75 793
Medical scientists women 57 1169
Physical scientists all other women 111 1323
Miscellaneous life physical and social science technicians women 57 826
Counselors women 473 907
Social workers women 557 884
Clergy women 53 893
Lawyers women 299 1619
Postsecondary teachers women 445 1152
Elementary and middle school teachers women 2231 981
Secondary school teachers women 562 1074
Other teachers and instructors women 214 786
Teacher assistants women 532 525
Designers women 297 922
Producers and directors women 50 1030
Editors women 56 1035
Pharmacists women 133 1839
Physicians and surgeons women 308 1476
Physical therapists women 128 1306
Registered nurses women 2213 1143
Clinical laboratory technologists and technicians women 198 834
Diagnostic related technologists and technicians women 175 936
Emergency medical technicians and paramedics women 60 650
Health practitioner support technologists and technicians women 361 643
Nursing psychiatric and home health aides women 1192 498
Bailiffs correctional officers and jailers women 91 672
Police and sheriff's patrol officers women 100 938
Security guards and gaming surveillance officers women 152 516
Chefs and head cooks women 68 519
First-line supervisors of food preparation and serving workers women 251 485
Cooks women 515 421
Food preparation workers women 262 413
Bartenders women 122 498
Combined food preparation and serving workers including fast food women 125 402
Waiters and waitresses women 607 441
Dining room and cafeteria attendants and bartender helpers women 51 407
First-line supervisors of housekeeping and janitorial workers women 71 524
Janitors and building cleaners women 441 476
Maids and housekeeping cleaners women 661 427
First-line supervisors of gaming workers women 59 717
Personal care aides women 636 469
Recreation and fitness workers women 100 517
First-line supervisors of retail sales workers women 1047 630
First-line supervisors of non-retail sales workers women 230 1004
Cashiers women 965 403
Retail salespersons women 728 514
Advertising sales agents women 81 999
Insurance sales agents women 205 676
Securities commodities and financial services sales agents women 63 951
Sales representatives services all other women 145 826
Sales representatives wholesale and manufacturing women 302 872
Real estate brokers and sales agents women 284 780
Sales and related workers all other women 68 709
First-line supervisors of office and administrative support workers women 819 809
Bookkeeping accounting and auditing clerks women 641 716
Customer service representatives women 1185 623
Receptionists and information clerks women 848 581
Dispatchers women 153 671
Postal service clerks women 53 805
Postal service mail carriers women 119 931
Production planning and expediting clerks women 111 765
Shipping receiving and traffic clerks women 151 583
Stock clerks and order fillers women 371 526
Secretaries and administrative assistants women 2078 708
Data entry keyers women 170 630
Office clerks general women 760 648
Office and administrative support workers all other women 351 751
Miscellaneous agricultural workers women 111 423
First-line supervisors of production and operating workers women 131 723
Electrical electronics and electromechanical assemblers women 50 554
Miscellaneous assemblers and fabricators women 345 559
Bakers women 83 480
Butchers and other meat poultry and fish processing workers women 61 449
Laundry and dry-cleaning workers women 80 436
Sewing machine operators women 114 452
Inspectors testers sorters samplers and weighers women 241 584
Packaging and filling machine operators and tenders women 113 425
Production workers all other women 246 515
Bus drivers women 146 589
Driver/sales workers and truck drivers women 120 630
Industrial truck and tractor operators women 54 563
Laborers and freight stock and material movers hand women 221 509
Packers and packagers hand women 216 437
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment