Skip to content

Instantly share code, notes, and snippets.

@CJKraenzle
Forked from ckraenzle/.block
Created November 12, 2017 01:58
Show Gist options
  • Save CJKraenzle/1da0f34a439cf89ddcc4e18ed9558124 to your computer and use it in GitHub Desktop.
Save CJKraenzle/1da0f34a439cf89ddcc4e18ed9558124 to your computer and use it in GitHub Desktop.
World Happiness Ranking
license: mit

Built with blockbuilder.org

This example summarizes a data table using Datalib.

This data is from Kaggle - World Happiness Report.

This dataset is described as "The World Happiness Report is a landmark survey of the state of global happiness."

It goes on to state "The World Happiness 2017, which ranks 155 countries by their happiness levels, was released at the United Nations at an event celebrating International Day of Happiness on March 20th."

For the data visualization assignments, I chose to build the charts from scratch instead of copying a block as a starting point to help me better understand how to use D3. I looked at a number of examples and have used the book "Interactive Data Visualization for the Web" by Scott Murray.

Country Happiness_Rank Happiness_Score Whisker_high Whisker_low Economy__GDP_per_Capita_ Family Health__Life_Expectancy_ Freedom Generosity Trust__Government_Corruption_ Dystopia_Residual
Norway 1 7.537000179 7.594444821 7.479555538 1.616463184 1.53352356 0.796666503 0.635422587 0.362012237 0.315963835 2.277026653
Denmark 2 7.521999836 7.581728065 7.462271607 1.482383013 1.551121593 0.792565525 0.626006722 0.355280489 0.400770068 2.313707352
Iceland 3 7.504000187 7.622030473 7.385969901 1.48063302 1.610574007 0.833552122 0.627162635 0.475540221 0.153526559 2.322715282
Switzerland 4 7.493999958 7.56177242 7.426227496 1.564979553 1.516911745 0.858131289 0.620070577 0.290549278 0.367007285 2.276716232
Finland 5 7.468999863 7.527542076 7.41045765 1.443571925 1.540246725 0.80915767 0.617950857 0.245482773 0.382611543 2.430181503
Netherlands 6 7.376999855 7.427425841 7.326573869 1.503944635 1.428939223 0.810696125 0.585384488 0.47048983 0.282661825 2.294804096
Canada 7 7.315999985 7.384402835 7.247597134 1.479204416 1.481348991 0.834557652 0.611100912 0.435539722 0.287371516 2.187264442
New Zealand 8 7.31400013 7.379510442 7.248489818 1.405706048 1.548195124 0.816759706 0.61406213 0.500005126 0.382816702 2.046456337
Sweden 9 7.28399992 7.344094877 7.223904962 1.494387269 1.478162169 0.830875158 0.612924099 0.385399252 0.384398729 2.097537994
Australia 10 7.28399992 7.356651225 7.211348615 1.484414935 1.510041952 0.843886793 0.601607382 0.47769925 0.30118373 2.065210819
Israel 11 7.212999821 7.279853256 7.146146385 1.375382423 1.376289964 0.838404 0.405988604 0.330082655 0.0852421 2.801757336
Costa Rica 12 7.078999996 7.168111666 6.989888326 1.109706283 1.416403651 0.759509265 0.58013165 0.214613229 0.100106589 2.898639202
Austria 13 7.006000042 7.070669812 6.941330272 1.487097263 1.459944963 0.815328419 0.56776619 0.316472322 0.221060365 2.138506413
United States 14 6.993000031 7.074656748 6.911343313 1.546259284 1.419920564 0.774286628 0.505740523 0.392578781 0.135638788 2.218113422
Ireland 15 6.977000237 7.043351668 6.910648805 1.535706639 1.558231115 0.809782624 0.573110342 0.427858323 0.298388153 1.773869038
Germany 16 6.951000214 7.005381569 6.896618858 1.487923384 1.472520351 0.798950732 0.562511384 0.33626917 0.276731938 2.015769958
Belgium 17 6.890999794 6.95582075 6.826178838 1.463780761 1.462312698 0.818091869 0.539770722 0.231503338 0.251343131 2.124210358
Luxembourg 18 6.862999916 6.9236861 6.802313732 1.741943598 1.457583666 0.845089495 0.596627891 0.283180982 0.318834424 1.619512081
United Kingdom 19 6.714000225 6.783791761 6.644208689 1.44163394 1.49646008 0.805335939 0.508190036 0.492774159 0.265428066 1.704143524
Chile 20 6.65199995 6.73925056 6.564749341 1.25278461 1.284024954 0.819479704 0.376895279 0.326662421 0.082287982 2.509585857
United Arab Emirates 21 6.647999763 6.722047304 6.573952223 1.626343369 1.266410232 0.726798236 0.60834527 0.360941947 0.324489564 1.734703541
Brazil 22 6.635000229 6.725469502 6.544530956 1.10735321 1.431306005 0.616552353 0.437453747 0.162349895 0.111092761 2.769267082
Czech Republic 23 6.609000206 6.683862463 6.534137949 1.352682352 1.433885217 0.754444003 0.490946174 0.088106759 0.036872927 2.451861858
Argentina 24 6.598999977 6.690085086 6.507914868 1.185295463 1.440451145 0.695137084 0.494519204 0.109457061 0.059739888 2.614005327
Mexico 25 6.578000069 6.671148908 6.48485123 1.153183818 1.21086216 0.709978998 0.412730008 0.120990433 0.132774115 2.837154865
Singapore 26 6.572000027 6.636723069 6.507276984 1.69227767 1.353814363 0.949492395 0.549840569 0.345965981 0.464307785 1.216362
Malta 27 6.52699995 6.598396773 6.455603128 1.343279839 1.488411665 0.821944237 0.588767052 0.574730575 0.153066069 1.556862831
Uruguay 28 6.453999996 6.545906218 6.362093775 1.217559695 1.412227869 0.719216824 0.579392254 0.175096929 0.178061873 2.172409534
Guatemala 29 6.453999996 6.566873973 6.34112602 0.872001946 1.255585194 0.54023999 0.531310618 0.283488393 0.077223279 2.893891096
Panama 30 6.452000141 6.557130716 6.346869566 1.233748436 1.373192549 0.706156135 0.550026834 0.210556939 0.070983924 2.307199955
France 31 6.441999912 6.515767802 6.368232022 1.430923462 1.387776852 0.844465852 0.470222116 0.129762307 0.172502428 2.005954742
Thailand 32 6.423999786 6.509116856 6.338882717 1.127868772 1.425792456 0.647239029 0.580200732 0.57212311 0.031612735 2.039508343
Taiwan Province of China 33 6.421999931 6.494596022 6.349403841 1.433626533 1.384565353 0.793984234 0.361466587 0.258360475 0.063829236 2.126607418
Spain 34 6.402999878 6.471054845 6.334944911 1.384397864 1.532090902 0.8889606 0.40878123 0.190133572 0.070914097 1.92775774
Qatar 35 6.375 6.568476814 6.181523186 1.870765686 1.27429688 0.710098088 0.604130983 0.33047387 0.439299256 1.14546442
Colombia 36 6.356999874 6.452020054 6.261979694 1.070622325 1.402182937 0.595027924 0.477487415 0.149014473 0.046668742 2.616068125
Saudi Arabia 37 6.343999863 6.444166612 6.243833113 1.530623555 1.286677599 0.59014833 0.449750572 0.147616014 0.273432255 2.065429688
Trinidad and Tobago 38 6.168000221 6.381533891 5.954466552 1.361355901 1.380228519 0.519983292 0.518630743 0.325296462 0.008964816 2.053247452
Kuwait 39 6.105000019 6.191956989 6.018043049 1.632952452 1.259698749 0.632105708 0.496337593 0.228289798 0.21515955 1.640425205
Slovakia 40 6.09800005 6.177348412 6.018651687 1.325393558 1.505059242 0.712732911 0.295817465 0.136544481 0.024210852 2.097776651
Bahrain 41 6.086999893 6.178989068 5.995010719 1.488412261 1.323110461 0.653133035 0.536746919 0.172668487 0.25704217 1.656149387
Malaysia 42 6.084000111 6.179979637 5.988020585 1.29121542 1.284646034 0.618784428 0.402264982 0.41660893 0.065600708 2.004448891
Nicaragua 43 6.071000099 6.1865836 5.955416598 0.737299204 1.28721571 0.653095961 0.447551847 0.301674217 0.130687982 2.513930559
Ecuador 44 6.007999897 6.105847675 5.910152119 1.000820398 1.286168814 0.685636222 0.455198199 0.150112465 0.140134647 2.290352583
El Salvador 45 6.002999783 6.108635122 5.897364443 0.909784496 1.182125092 0.596018553 0.43245253 0.078257985 0.08998096 2.714593887
Poland 46 5.97300005 6.053908341 5.892091758 1.291787863 1.44571197 0.699475348 0.520342112 0.158465967 0.059307806 1.797722816
Uzbekistan 47 5.971000195 6.065537573 5.876462816 0.786441088 1.54896915 0.498272628 0.658248663 0.415983647 0.246528223 1.816913605
Italy 48 5.964000225 6.042736906 5.885263544 1.395066619 1.444923282 0.853144348 0.256450713 0.172789648 0.028028091 1.813312054
Russia 49 5.962999821 6.030274907 5.895724734 1.281778097 1.469282389 0.547349334 0.373783112 0.052263822 0.032962881 2.205607414
Belize 50 5.955999851 6.197242318 5.714757385 0.907975316 1.081417799 0.450191766 0.547509372 0.240015641 0.096581072 2.631955624
Japan 51 5.920000076 5.990719445 5.849280708 1.416915178 1.436337829 0.913475871 0.505625546 0.120572768 0.163760737 1.363223553
Lithuania 52 5.90199995 5.982669641 5.821330259 1.314582348 1.473516107 0.62894994 0.234231785 0.010164657 0.011865643 2.228440523
Algeria 53 5.872000217 5.978286434 5.765714001 1.091864467 1.146217465 0.617584646 0.233335808 0.069436647 0.14609611 2.567603827
Latvia 54 5.849999905 5.920263538 5.779736271 1.260748625 1.404714942 0.638566971 0.325707912 0.153074786 0.073842727 1.993655205
South Korea 55 5.837999821 5.922559028 5.753440613 1.401678443 1.128274441 0.900214076 0.257921666 0.206674367 0.063282669 1.880378008
Moldova 56 5.837999821 5.908370838 5.767628803 0.72887063 1.251825571 0.589465201 0.240729049 0.208779126 0.010091286 2.807808399
Romania 57 5.824999809 5.919694157 5.730305462 1.217683911 1.15009129 0.685158312 0.457003742 0.133519918 0.004387901 2.176831484
Bolivia 58 5.822999954 5.903976903 5.742023006 0.833756566 1.227619052 0.47363025 0.558732927 0.225560725 0.060477726 2.443279028
Turkmenistan 59 5.822000027 5.885180876 5.758819178 1.130776763 1.493149161 0.43772608 0.418271929 0.249924988 0.25927034 1.832909822
Kazakhstan 60 5.818999767 5.903641775 5.73435776 1.28455627 1.384369016 0.606041551 0.437454283 0.201964423 0.119282886 1.784892559
North Cyprus 61 5.809999943 5.897366465 5.722633421 1.346911311 1.186303377 0.834647238 0.471203625 0.266845703 0.155353352 1.549157619
Slovenia 62 5.757999897 5.84222516 5.673774634 1.341205955 1.452518821 0.790828228 0.572575808 0.242649093 0.045128979 1.313317299
Peru 63 5.715000153 5.811946779 5.618053527 1.035225272 1.218770385 0.630166113 0.450002879 0.126819715 0.047049087 2.20726943
Mauritius 64 5.629000187 5.729862192 5.528138182 1.189395547 1.20956099 0.638007462 0.491247326 0.360933751 0.042181555 1.697583914
Cyprus 65 5.620999813 5.714692699 5.527306927 1.355938077 1.131363273 0.844714701 0.355111539 0.271254301 0.041237976 1.621249199
Estonia 66 5.611000061 5.688139874 5.533860248 1.32087934 1.4766711 0.695168316 0.47913143 0.098890811 0.183248922 1.357508659
Belarus 67 5.568999767 5.646114244 5.491885291 1.15655756 1.444945216 0.637714267 0.295400262 0.155137509 0.156313822 1.723232985
Libya 68 5.525000095 5.676953807 5.373046384 1.101803064 1.35756433 0.52016902 0.46573323 0.152073666 0.09261021 1.835011244
Turkey 69 5.5 5.594864963 5.405135037 1.198274374 1.337753177 0.637605608 0.3007406 0.046693042 0.09967158 1.879277945
Paraguay 70 5.493000031 5.57738127 5.408618791 0.932537317 1.50728488 0.579250693 0.473507792 0.224150658 0.091065913 1.68533349
Hong Kong S.A.R. 71 5.472000122 5.549594173 5.394406071 1.551674843 1.262790918 0.943062425 0.490968645 0.374465793 0.293933749 0.554633141
Philippines 72 5.429999828 5.545335054 5.314664602 0.857699215 1.253917575 0.468009055 0.585214674 0.193513423 0.099331893 1.972604752
Serbia 73 5.394999981 5.491569656 5.298430306 1.069317579 1.258189797 0.650784671 0.208715528 0.220125884 0.040903781 1.947084427
Jordan 74 5.335999966 5.448410022 5.223589909 0.991012394 1.239088893 0.604590058 0.418421149 0.17217046 0.119803272 1.791176558
Hungary 75 5.323999882 5.403039709 5.244960054 1.286011934 1.343133092 0.687763453 0.175863519 0.078401662 0.036636937 1.716459274
Jamaica 76 5.31099987 5.581398728 5.040601012 0.925579309 1.368218064 0.641022384 0.474307239 0.233818337 0.055267781 1.612325668
Croatia 77 5.293000221 5.391777205 5.194223238 1.222556233 0.967983007 0.701288521 0.255772293 0.248002976 0.04310311 1.854492426
Kosovo 78 5.278999805 5.364847995 5.193151615 0.951484382 1.137853503 0.54145205 0.260287941 0.319931448 0.057471618 2.010540724
China 79 5.272999763 5.31927809 5.226721437 1.081165791 1.160837412 0.741415501 0.472787708 0.028806841 0.022794275 1.764938593
Pakistan 80 5.269000053 5.359983641 5.178016465 0.726883531 0.67269069 0.402047783 0.235215262 0.315446019 0.124348067 2.79248929
Indonesia 81 5.262000084 5.352888598 5.17111157 0.995538592 1.274444699 0.492345721 0.443323463 0.611704588 0.015317135 1.429476976
Venezuela 82 5.25 5.370031946 5.129968054 1.128431201 1.431337595 0.617144227 0.153997123 0.06501963 0.064491123 1.789463758
Montenegro 83 5.236999989 5.341044441 5.132955537 1.121129036 1.238376498 0.667464674 0.194989055 0.197911024 0.088174194 1.729191542
Morocco 84 5.235000134 5.318340965 5.151659302 0.878114581 0.774864435 0.597710669 0.408158332 0.032209955 0.087763183 2.456189394
Azerbaijan 85 5.234000206 5.299286535 5.168713877 1.153601766 1.152400255 0.540775776 0.398155838 0.04526934 0.180987507 1.762481689
Dominican Republic 86 5.230000019 5.349060885 5.110939153 1.079373837 1.402416706 0.574873745 0.552589834 0.18696785 0.113945253 1.31946516
Greece 87 5.227000237 5.325246169 5.128754304 1.289487481 1.239414573 0.810198903 0.095731251 0 0.043289777 1.749221563
Lebanon 88 5.224999905 5.318882288 5.131117521 1.074987531 1.129624248 0.735081077 0.288515985 0.264450759 0.03751383 1.695073843
Portugal 89 5.195000172 5.285041733 5.10495861 1.315175295 1.367043018 0.795843542 0.4984653 0.095102713 0.015869452 1.107682705
Bosnia and Herzegovina 90 5.18200016 5.276335687 5.087664634 0.982409418 1.069335938 0.705186307 0.204403177 0.328867495 0 1.892172575
Honduras 91 5.181000233 5.301582797 5.060417669 0.730573118 1.143944979 0.58256948 0.34807986 0.236188874 0.073345453 2.065811157
Macedonia 92 5.175000191 5.27217264 5.077827742 1.064577937 1.207893014 0.644948184 0.325905979 0.253760964 0.060277794 1.617469311
Somalia 93 5.151000023 5.24248371 5.059516336 0.022643184 0.721151352 0.113989137 0.602126956 0.291631311 0.282410324 3.11748457
Vietnam 94 5.073999882 5.147280765 5.000718999 0.788547575 1.277491331 0.652168989 0.571055591 0.234968051 0.087633237 1.462318659
Nigeria 95 5.073999882 5.209500135 4.938499628 0.783756256 1.215770483 0.05691573 0.394952565 0.230947196 0.026121566 2.365390539
Tajikistan 96 5.040999889 5.111425596 4.970574183 0.524713635 1.271463275 0.529235125 0.471566707 0.248997644 0.146377146 1.84904933
Bhutan 97 5.011000156 5.079334562 4.942665751 0.885416389 1.340126514 0.495879292 0.501537681 0.474054545 0.17338039 1.140184402
Kyrgyzstan 98 5.004000187 5.089919903 4.918080471 0.596220076 1.394238591 0.553457797 0.454943389 0.428580374 0.039439179 1.536723137
Nepal 99 4.961999893 5.067356079 4.856643707 0.479820192 1.179283261 0.504130781 0.440305948 0.394096166 0.072975546 1.891241074
Mongolia 100 4.954999924 5.021679509 4.888320338 1.027235866 1.493011236 0.557783484 0.394143969 0.33846423 0.032902289 1.111292362
South Africa 101 4.828999996 4.929435188 4.728564805 1.054698706 1.384788632 0.18708007 0.479246736 0.13936238 0.072509497 1.510908604
Tunisia 102 4.804999828 4.884367007 4.72563265 1.007265806 0.86835146 0.613212049 0.28968069 0.049693357 0.086723149 1.89025116
Palestinian Territories 103 4.775000095 4.881848343 4.668151848 0.716249228 1.155647159 0.565666974 0.254711062 0.114173174 0.089282602 1.878890276
Egypt 104 4.735000134 4.82513379 4.644866477 0.989701807 0.997471392 0.520187259 0.282110155 0.128631443 0.114381365 1.702161074
Bulgaria 105 4.714000225 4.803694706 4.624305744 1.161459088 1.434379458 0.70821768 0.289231718 0.113177694 0.011051531 0.996139288
Sierra Leone 106 4.709000111 4.850643335 4.567356886 0.368420929 0.984136045 0.005564754 0.318697691 0.293040901 0.071095176 2.668459892
Cameroon 107 4.695000172 4.796540857 4.593459486 0.564305365 0.946018219 0.132892117 0.430388749 0.236298457 0.051306631 2.333645582
Iran 108 4.691999912 4.798224708 4.585775117 1.156873107 0.711551249 0.639333189 0.249322608 0.387242913 0.048761073 1.498734951
Albania 109 4.644000053 4.752464006 4.5355361 0.996192753 0.803685248 0.731159747 0.381498635 0.201312944 0.039864216 1.490441561
Bangladesh 110 4.607999802 4.689821658 4.526177945 0.586682975 0.735131741 0.533241034 0.478356659 0.172255352 0.123717859 1.978736162
Namibia 111 4.573999882 4.770354741 4.377645023 0.964434326 1.098470807 0.338611811 0.520303547 0.077133745 0.093146972 1.481890202
Kenya 112 4.552999973 4.655691591 4.450308356 0.560479462 1.067950726 0.30998835 0.452763766 0.444860309 0.064641319 1.651902199
Mozambique 113 4.550000191 4.774102326 4.325898055 0.23430565 0.870701015 0.106654435 0.480791092 0.322228104 0.179436386 2.355650902
Myanmar 114 4.545000076 4.614739946 4.475260206 0.36711055 1.123235941 0.397522569 0.514492035 0.838075161 0.188816205 1.115290403
Senegal 115 4.534999847 4.601603781 4.468395914 0.479309022 1.179691911 0.409362853 0.377922267 0.183468893 0.115460448 1.789646149
Zambia 116 4.513999939 4.644105501 4.383894376 0.636406779 1.003187299 0.257835895 0.461603492 0.249580145 0.07821355 1.826705456
Iraq 117 4.497000217 4.62259141 4.371409025 1.102710485 0.978613198 0.50118047 0.288555533 0.199637264 0.107215755 1.318907261
Gabon 118 4.465000153 4.557361766 4.37263854 1.198210239 1.155620217 0.356578588 0.312328577 0.043785378 0.076046787 1.322916269
Ethiopia 119 4.460000038 4.542728677 4.3772714 0.339233845 0.864669204 0.353409708 0.408842742 0.31265074 0.165455714 2.015743732
Sri Lanka 120 4.440000057 4.553447192 4.326552922 1.009850144 1.259976387 0.625130832 0.561213255 0.490863562 0.073653966 0.419389248
Armenia 121 4.375999928 4.466734612 4.285265243 0.900596738 1.007483721 0.637524426 0.198303267 0.083488092 0.026674422 1.521499157
India 122 4.315000057 4.371522017 4.258478097 0.792221248 0.754372597 0.455427617 0.469987005 0.231538489 0.092226885 1.519117117
Mauritania 123 4.291999817 4.377163616 4.206836018 0.648457289 1.27203083 0.28534928 0.096098043 0.201870024 0.136957005 1.651637316
Congo (Brazzaville) 124 4.290999889 4.410053505 4.171946274 0.808964252 0.832044363 0.289957434 0.435025871 0.120852128 0.079618134 1.724135637
Georgia 125 4.285999775 4.374933966 4.197065584 0.950612664 0.570614934 0.649546981 0.309410036 0.054008815 0.251666635 1.500137806
Congo (Kinshasa) 126 4.28000021 4.357810833 4.202189587 0.092102349 1.229023457 0.191407025 0.235961348 0.246455833 0.060241356 2.224958658
Mali 127 4.190000057 4.269670711 4.110329403 0.476180494 1.281473398 0.169365674 0.306613743 0.183354199 0.104970247 1.668190956
Ivory Coast 128 4.179999828 4.275182563 4.084817093 0.603048921 0.90478003 0.04864217 0.447706193 0.20123747 0.130061775 1.844964266
Cambodia 129 4.168000221 4.278517814 4.057482629 0.601765096 1.006238341 0.429783404 0.633375823 0.385922968 0.068105951 1.042941093
Sudan 130 4.138999939 4.345747165 3.932252713 0.659516692 1.21400857 0.290920824 0.014995855 0.182317451 0.08984752 1.68706584
Ghana 131 4.119999886 4.222707209 4.017292563 0.667224824 0.873664737 0.295637727 0.423026294 0.256923944 0.02533637 1.577867508
Ukraine 132 4.096000195 4.185410105 4.006590285 0.894651949 1.394537568 0.575903952 0.122974776 0.270061463 0.023029471 0.814382315
Uganda 133 4.080999851 4.195799967 3.966199735 0.381430715 1.129827738 0.217632607 0.443185955 0.325766057 0.057069719 1.526362658
Burkina Faso 134 4.032000065 4.124059064 3.939941065 0.350227714 1.043280005 0.215844259 0.324367851 0.250864685 0.120328106 1.727212906
Niger 135 4.027999878 4.11194682 3.944052936 0.161925331 0.993025005 0.268505007 0.363658696 0.228673846 0.138572946 1.873983383
Malawi 136 3.970000029 4.077478817 3.86252124 0.233442038 0.512568831 0.315089583 0.466914654 0.28717047 0.072711654 2.081786156
Chad 137 3.936000109 4.034711524 3.837288693 0.438012987 0.953855872 0.041134715 0.162342027 0.21611385 0.053581882 2.071238041
Zimbabwe 138 3.875 3.978699643 3.771300357 0.375846535 1.083095908 0.196763754 0.336384207 0.189143494 0.095375381 1.597970247
Lesotho 139 3.808000088 4.044343975 3.5716562 0.521021247 1.190095186 0 0.390661299 0.157497272 0.11909464 1.42983532
Angola 140 3.795000076 3.951641935 3.638358217 0.85842818 1.10441196 0.049868666 0 0.09792649 0.069720335 1.614482403
Afghanistan 141 3.79399991 3.873661415 3.714338405 0.401477218 0.581543326 0.180746779 0.10617952 0.311870933 0.06115783 2.150801182
Botswana 142 3.766000032 3.874122666 3.657877399 1.122094154 1.221554995 0.341755509 0.505196333 0.099348448 0.098583199 0.377913713
Benin 143 3.657000065 3.745783551 3.568216578 0.431085408 0.435299844 0.209930211 0.425962776 0.207948461 0.060929015 1.885630965
Madagascar 144 3.644000053 3.714319106 3.573681001 0.305808693 0.913020372 0.375223309 0.189196765 0.20873253 0.067231975 1.584612608
Haiti 145 3.602999926 3.734714798 3.471285053 0.368610263 0.640449822 0.27732113 0.030369857 0.489203781 0.09987215 1.697167635
Yemen 146 3.592999935 3.69275032 3.49324955 0.591683447 0.935382247 0.310080916 0.249463722 0.104125209 0.056767423 1.345600605
South Sudan 147 3.59100008 3.725538586 3.456461574 0.397248626 0.601323128 0.163486004 0.147062436 0.285670817 0.116793513 1.879567385
Liberia 148 3.532999992 3.653756261 3.412243724 0.119041793 0.872117937 0.229918197 0.332881182 0.266549885 0.038948249 1.673285961
Guinea 149 3.506999969 3.584428129 3.42957181 0.24454993 0.791244686 0.194129139 0.348587513 0.264815092 0.110937618 1.552311897
Togo 150 3.494999886 3.594038112 3.395961659 0.305444717 0.43188253 0.247105569 0.380426139 0.196896151 0.095665015 1.837229252
Rwanda 151 3.470999956 3.543030234 3.398969679 0.368745893 0.945707023 0.326424807 0.581843853 0.252756029 0.455220014 0.540061235
Syria 152 3.461999893 3.663668557 3.26033123 0.777153134 0.396102607 0.500533342 0.081539445 0.493663728 0.151347131 1.061573505
Tanzania 153 3.348999977 3.461429755 3.236570199 0.511135876 1.041989803 0.364509284 0.390017778 0.354256362 0.066035107 0.621130466
Burundi 154 2.904999971 3.074690333 2.73530961 0.091622569 0.629793584 0.151610792 0.059900753 0.204435185 0.084147945 1.683024168
Central African Republic 155 2.693000078 2.864884269 2.521115887 0 0 0.018772686 0.270842046 0.280876487 0.056565076 2.066004753
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { background-color: #ffffff; }
.label { font-size: 18pt; font-family:Helvetica; }
</style>
</head>
<body>
<script>
// variables
let w = 960;
let h = 500;
let margin = { top: 20, right: 20, left: 150, bottom: 120 };
let innerWidth = w - margin.right - margin.left;
let innerHeight = h - margin.top - margin.bottom;
let happy = [];
let fieldStats = [];
let xLinear;
let yLinear = d3.scaleLinear();
let svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h);
// Create dynamic scales based on columns passed
const createScales=(xField,yField)=>{
xLinear = d3.scaleLinear()
.domain([d3.min(happy,(d,i)=>{ return d[xField]; }), d3.max(happy,(d,i)=>{ return d[xField]; })])
.range([0 + margin.left, w - margin.right])
.nice();
yLinear = d3.scaleLinear()
.domain([0,d3.max(happy,(d,i)=>{ return d[yField]; })])
.range([innerHeight,0 + margin.bottom])
.nice();
}
// Create dynamic axis based on columns passed
const createAxis=(xField,yField)=>{
let xAxis = d3.svg.axis();
}
// Create Scatter plot based on columns passed (happy)
const createScatterPlot=(xField,yField,rField)=>{
svg.selectAll("circle")
.data(happy)
.enter()
.append("circle")
.attr("cx", (d,i)=>{ return xLinear(d[xField]); })
.attr("cy", (d,i)=>{ return yLinear(d[yField]); })
.attr("r", (d,i)=>{ return d[rField] * 10; });
svg.append("g")
.attr("transform", "translate(0," + innerHeight + ")")
.call(d3.axisBottom(xLinear));
svg.append("text")
.attr("class","label")
.attr("y", 425)
.attr("x", w /2)
.style("text-anchor", "middle")
.text("Happiness Ranking");
svg.append("g")
.attr("transform", "translate(" + 150 + "," + 0 + ")")
.call(d3.axisLeft(yLinear));
svg.append("text")
.attr("class","label")
.attr("transform", "rotate(-90)")
.attr("y", 100)
.attr("x", -(h / 2))
.style("text-anchor", "middle")
.text("GDP Per Capita");
svg.append("text")
.attr("class","label")
.attr("y", 50)
.attr("x", w /2)
.style("text-anchor", "middle")
.text("World Happiness Ranking");
}
// Convert header to JSON friendly header (Columns)
const processHappy=(d)=>{
return {
country: d["Country"],
happinessRank: +d["Happiness_Rank"],
happinessScore: +d["Happiness_Score"],
whiskerHigh: +d["Whisker_high"],
whiskerLow: +d["Whisker_low"],
gdpPerCapita: +d["Economy__GDP_per_Capita_"],
family: +d["Family"],
lifeExpectancy: +d["Health__Life_Expectancy_"],
freedom: +d["Freedom"],
generosity: +d["Generosity"],
governmentCorruption: +d["Trust__Government_Corruption_"],
dystopiaResidual: +d["Dystopia_Residual"]
};
}
// Load world happiness data
const happyFile=()=>{
d3.csv("data1.csv",processHappy,data=>{
happy = data;
// For each column calculate basic statistics
fieldStats.push(fieldProfile("happinessRank"));
fieldStats.push(fieldProfile("happinessScore"));
fieldStats.push(fieldProfile("whiskerHigh"));
fieldStats.push(fieldProfile("whiskerLow"));
fieldStats.push(fieldProfile("gdpPerCapita"));
fieldStats.push(fieldProfile("family"));
fieldStats.push(fieldProfile("lifeExpectancy"));
fieldStats.push(fieldProfile("freedom"));
fieldStats.push(fieldProfile("generosity"));
fieldStats.push(fieldProfile("governmentCorruption"));
fieldStats.push(fieldProfile("dystopiaResidual"));
// Write out all the statistics
fieldStats.forEach((row,index)=>{
console.log(JSON.stringify(row));
});
// World Happyiness Rank
createScales("happinessScore","gdpPerCapita");
createScatterPlot("happinessScore","gdpPerCapita","lifeExpectancy");
});
};
// Calculate basic statistics on a field
const fieldProfile=(fieldname)=>{
const avg = d3.nest().rollup((v)=>{ return d3.mean(v, (d)=>{ return d[fieldname]; }); }).entries(happy);
const med = d3.nest().rollup((v)=>{ return d3.median(v, (d)=>{ return d[fieldname]; }); }).entries(happy);
const min = d3.nest().rollup((v)=>{ return d3.min(v, (d)=>{ return d[fieldname]; }); }).entries(happy);
const max = d3.nest().rollup((v)=>{ return d3.max(v, (d)=>{ return d[fieldname]; }); }).entries(happy);
happy.sort((a,b)=>{ return a[fieldname] - b[fieldname]; });
const p25 = Math.round(happy.length * .25);
const v25 = happy[p25][fieldname];
const v75 = happy[happy.length - p25];
const stats = {};
stats["fieldname"] = fieldname;
stats["average"] = avg;
stats["median"] = med;
stats["min"] = min;
stats["max"] = max;
stats["p25"] = v25;
stats["p75"] = v75[fieldname];
return stats;
}
document.addEventListener("DOMContentLoaded", (event)=>{
// World Happyiness Rank
happyFile();
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment