Skip to content

Instantly share code, notes, and snippets.

@HarryStevens
Last active September 13, 2020 07:01
Show Gist options
  • Save HarryStevens/69da410602d8ca6b9f41f72024dba791 to your computer and use it in GitHub Desktop.
Save HarryStevens/69da410602d8ca6b9f41f72024dba791 to your computer and use it in GitHub Desktop.
Choropleth Sandbox
license: gpl-3.0
height: 700

There's a lot of human judgment involved in creating choropleth maps. You can significantly change what you communicate to your readers by altering the number of classes, the type of breaks, and the color scheme.

Number of classes

There's some disagreement here, but in general, you should use no fewer than three classes and no more than eight. Any fewer will fail to show enough detail, and any more will no longer be discriminable. Here's a paper that argues you should use seven or eight.

Type of breaks

  • Equidistant Also called equal interval, this type ensures each break is numerically equal. It may make the most immediate sense to your readers, but it often fails to correspond to the distribution of your data.
  • Quantile This type ensures each break contains the same number of data points. It corresponds to the distribution of your data, but it fails to highlight outliers and it might not make much sense to your readers.
  • Logarithmic These are just like equidistant breaks, but on a logarithmic scale.
  • K-means This uses the k-means clustering algorithm, which creates more "natural" breaks. Axismaps describes natural breaks as those that "minimize within-class variance and maximize between-class differences."

I used Gregor Aisch's awesome chroma.js library to implement these breaks.

Color schemes

This block lets you choose from 12 multi-hue schemes created by Cynthia Brewer. They can all be found on ColorBrewer2. Don't use rainbow color schemes, as people have trouble making pre-cognitive interpretations of the different colors.

About this map

Sex ration Sex ratio among people aged 0-4 by district. Lower sex ratios mean fewer females. Source: Census of India, 2011

Muslim pop. (%) Percentage of a district's population that is Muslim. Source: Census of India, 2011

Houseless / lakh pop. Number of homeless people per lakh people. No data for Dibang Valley, Arunanchal Pradesh; Lakshadweep, Lakshadweep; Kiphire, Nagaland; Upper Siang, Arunanchal Pradesh; Kolasib, Mizoram. Source: Census of India, 2011

state district sex_ratio muslim_pct houseless_per_lakh
Himachal Pradesh Lahul & Spiti 1.05 0.234444304 41.18616145
Jammu & Kashmir Leh (ladakh) 0.97 14.27629657 34.46028452
Andaman & Nicobar Island Nicobar 0.96 3.626296075 5.428586939
Jammu & Kashmir Kargil 0.99 76.87319782 17.04521243
Mizoram Lunglei 0.96 0.799117873 6.814183413
Arunanchal Pradesh Anjaw 1.02 3.094439458 89.76236595
Himachal Pradesh Kinnaur 0.96 0.532566184 26.1528037
Andaman & Nicobar Island North & Middle Andaman 0.96 2.05024764 12.3109558
Arunanchal Pradesh Lower Dibang Valley 0.95 4.154955621 36.98224852
Mizoram Mamit 0.98 2.063359733 18.52623778
Nagaland Tuensang 0.94 0.53510753 7.121202873
Mizoram Saiha 0.92 0.908544561 14.14077138
Jammu & Kashmir Doda 0.93 53.81669334 12.92884743
Nagaland Zunheboto 0.96 0.548462954 6.393998167
Mizoram Aizawl 0.98 1.314984175 7.494210722
Arunanchal Pradesh Kurung Kumey 0.98 0.51153395 57.56114514
Mizoram Champhai 0.98 0.555886914 23.06254722
Sikkim North 0.95 1.864604544 96.0900501
Mizoram Lawangtlai 0.96 0.442770624 22.05370926
Arunanchal Pradesh West Siang 0.96 1.975524164 95.30256337
Arunanchal Pradesh East Kameng 1 0.828567798 68.62371331
Assam Dima Hasao 0.97 2.035478417 29.89229433
Arunanchal Pradesh West Kameng 0.96 2.346718763 151.2859304
Arunanchal Pradesh Upper Subansiri 0.97 0.624340907 147.3971815
Nagaland Peren 0.92 1.942889549 32.55652758
Arunanchal Pradesh Changlang 0.99 1.439693441 61.39273812
Rajasthan Jaisalmer 0.87 25.0969147 119.4174221
Himachal Pradesh Kullu 0.97 0.679145838 26.48988475
Chhattisgarh Narayanpur 0.99 0.554999285 70.80532113
Mizoram Serchhip 0.94 0.515884627 49.2785315
Manipur Ukhrul 0.91 0.605984848 57.60932184
Meghalaya West Khasi Hills 0.97 0.399779899 35.466449
Meghalaya Ri Bhoi 0.96 0.710091176 27.04373358
Uttarakhand Uttarkashi 0.92 1.076689105 69.67881098
Meghalaya Jaintia Hills 0.97 0.416578087 28.34553204
Andaman & Nicobar Island South Andaman 0.97 12.14065558 33.59340226
Arunanchal Pradesh Lower Subansiri 0.96 1.784897025 134.8910033
Jammu & Kashmir Kishtwar 0.91 57.74915907 22.97395707
Assam Dhemaji 0.95 1.963904957 15.59464419
Nagaland Mokokchung 0.96 1.056920595 28.77372548
Arunanchal Pradesh Tirap 0.97 0.985041304 73.23063184
Himachal Pradesh Chamba 0.96 6.252408107 44.69445943
Nagaland Wokha 0.95 1.228185136 34.8677131
Arunanchal Pradesh Lohit 0.96 2.639199594 130.3816752
Arunanchal Pradesh East Siang 0.99 2.307134074 135.0615841
Sikkim West 0.96 0.707296515 35.91453806
Odisha Kandhamal 0.98 0.29163427 46.92338121
Meghalaya East Garo Hills 0.98 1.056565078 39.63298597
Chhattisgarh Dakshin Bastar Dantewada 1.02 1.206248431 78.51764679
Manipur Chandel 0.92 2.14035039 117.9065348
Tripura Dhalai 0.98 2.182798826 32.78428469
Nagaland Phek 0.91 0.58133131 64.25240794
Nagaland Longleng 0.88 1.257824261 59.42476824
Manipur Tamenglong 0.92 0.418767019 173.4790368
Odisha Gajapati 0.97 0.269289412 42.74709813
Uttarakhand Bageshwar 0.89 0.554063517 50.01962308
Uttarakhand Pithoragarh 0.81 1.244210748 87.70496381
Gujarat Kachchh 0.93 21.14132723 132.1467369
Assam Karbi Anglong 0.96 2.121690283 66.71455894
Nagaland Kohima 0.99 1.635894145 37.31510366
Jammu & Kashmir Udhampur 0.89 10.76984063 32.61349406
Assam Chirang 0.97 22.65794484 27.58408999
Manipur Senapati 0.9 0.344152537 47.37575864
Odisha Debagarh 0.93 0.225265583 66.87571995
Madhya Pradesh Seoni 0.95 5.781829282 45.2458831
Chhattisgarh Bijapur 1 0.633546213 241.3509384
Assam Kokrajhar 0.96 28.43637208 26.94044471
Madhya Pradesh Dindori 0.97 0.956106534 77.49913417
Chhattisgarh Uttar Bastar Kanker 0.98 0.806472072 70.49954536
Sikkim South 0.94 1.286346612 38.13415049
Assam Lakhimpur 0.95 18.56531339 16.40859119
Odisha Malkangiri 0.98 0.305124659 71.59258438
Odisha Rayagada 0.97 0.475973514 55.68693816
Jammu & Kashmir Reasi 0.92 49.66361264 41.9491081
Jharkhand Pashchimi Singhbhum 0.99 2.536246837 37.27523367
Kerala Idukki 0.97 7.412797775 30.56879602
Arunanchal Pradesh Papum Pare 0.96 3.481846035 153.4775985
Arunanchal Pradesh Tawang 0.99 1.574724373 346.1592332
Odisha Koraput 0.98 0.641468434 52.54967394
Uttarakhand Rudraprayag 0.9 0.61332728 68.51435293
Meghalaya East Khasi Hills 0.97 1.717474531 28.08981962
Odisha Nabarangapur 1 0.553095714 36.69285947
Odisha Nayagarh 0.87 0.548199034 34.37928767
Uttarakhand Champawat 0.87 3.347994208 58.15565689
Nagaland Mon 0.93 1.376168784 61.53600256
Himachal Pradesh Shimla 0.92 1.450842127 54.42193585
Assam Udalguri 0.98 12.66358691 21.28253101
Rajasthan Bikaner 0.91 9.972389281 112.9894748
Assam Golaghat 0.97 8.464993514 30.5561596
Tamil Nadu Dindigul 0.94 4.862173143 26.5768425
Madhya Pradesh Chhindwara 0.96 4.815674616 53.89966723
Karnataka Chamrajnagar 0.96 4.624844851 53.19404266
Meghalaya West Garo Hills 0.98 16.60026333 56.273133
Chhattisgarh Bastar 1 0.878361788 73.09657026
Jammu & Kashmir Baramula 0.85 95.15356053 42.06186467
Chhattisgarh Koriya 0.97 3.927353521 101.0749457
Himachal Pradesh Mandi 0.91 0.946211005 39.90889968
Jharkhand Khunti 0.97 2.46707465 48.50672608
Uttarakhand Almora 0.92 1.245289202 51.72640906
Rajasthan Barmer 0.91 12.33574178 111.8770574
Assam Baksa 0.97 14.28834566 26.62947662
Jharkhand Simdega 0.98 2.51893832 65.21253281
Chhattisgarh Korba 0.98 2.502403368 56.76921037
Manipur Churachandpur 0.94 1.052735251 173.9967827
Jharkhand Gumla 0.96 5.016420978 55.3055804
Meghalaya South Garo Hills 0.98 0.737701463 142.6222828
Uttar Pradesh Lalitpur 0.92 2.76065986 44.61391365
Assam Tinsukia 0.96 3.642739936 31.55289176
Jharkhand Kodarma 0.95 14.94124332 39.51084733
Uttar Pradesh Hamirpur 0.89 8.264985941 41.6559131
Assam Kamrup 0.97 39.65517923 24.0520526
Jammu & Kashmir Ramban 0.92 70.67564757 55.33761231
Madhya Pradesh Panna 0.93 3.464171881 83.12674615
Tamil Nadu Pudukkottai 0.96 7.056221016 34.23250296
Tripura South Tripura 0.95 5.623052942 41.78077422
Jharkhand Lohardaga 0.98 20.57472011 38.97875658
Maharashtra Garhchiroli 0.97 1.963107046 164.0349618
Tamil Nadu Dharmapuri 0.92 2.542003381 37.49561169
Assam Nalbari 0.97 35.96085734 17.36563341
Tamil Nadu Perambalur 0.9 5.78568105 39.63037598
Uttarakhand Chamoli 0.88 1.122304363 262.5094164
Tamil Nadu The Nilgiris 0.98 10.66802286 44.7379228
Odisha Kendujhar 0.97 1.540294816 59.27626346
Karnataka Uttara Kannada 0.96 13.07946386 92.19514198
Bihar Jamui 0.96 12.36198488 22.72204408
Chhattisgarh Jashpur 0.99 1.800699568 88.64946358
Assam Darrang 0.97 64.33947227 22.29402262
Himachal Pradesh Kangra 0.88 1.310994487 49.79885105
Uttar Pradesh Sonbhadra 0.93 5.560468152 49.34071887
Madhya Pradesh Shahdol 0.97 3.979689756 77.4813496
Sikkim East 0.95 2.185603509 45.84195809
Jharkhand Chatra 0.97 11.19106019 48.61509312
Karnataka Bijapur 0.94 16.97435989 66.87086162
Madhya Pradesh Sidhi 0.93 3.053947844 61.04524002
Odisha Kalahandi 0.97 0.315498624 72.1049117
Manipur Imphal East 0.93 17.58314277 22.36287937
Jharkhand Hazaribagh 0.93 16.21492135 30.09521503
Madhya Pradesh Balaghat 0.97 2.264914221 79.92017385
Karnataka Chitradurga 0.95 7.758626924 75.92849705
Odisha Subarnapur 0.96 0.256644318 57.35984123
Tamil Nadu Virudunagar 0.96 2.462971506 32.79637211
Kerala Wayanad 0.97 28.64928678 39.14756184
Jharkhand Latehar 0.97 9.60249141 89.13612241
Assam Cachar 0.96 37.70641425 33.22551835
Tamil Nadu Thoothukkudi 0.97 4.613250325 41.99577642
Odisha Nuapada 0.98 0.811295222 97.97143428
Tamil Nadu Sivaganga 0.97 5.549245352 49.1374437
Odisha Bauda 0.98 0.22826082 109.256917
Tamil Nadu Theni 0.94 4.555024123 35.95797091
Karnataka Koppal 0.96 11.63879936 63.81662254
Kerala Pathanamthitta 0.98 4.599419415 35.57672714
Madhya Pradesh Sheopur 0.91 6.018076326 155.1185487
Tamil Nadu Ramanathapuram 0.96 15.37099771 49.87273218
Jammu & Kashmir Shupiyan 0.85 98.5154856 19.53308416
Jharkhand Giridih 0.94 20.79703158 34.51273659
Assam Goalpara 0.96 57.52219587 31.34351601
Odisha Mayurbhanj 0.97 1.337678759 72.98377847
Chhattisgarh Kabeerdham 0.99 1.483965249 91.18252797
Odisha Sambalpur 0.95 1.932573175 112.7654527
Chhattisgarh Surguja 0.97 3.451098909 118.2684248
Chhattisgarh Rajnandgaon 0.99 1.538448527 94.33145993
Odisha Kendrapara 0.93 3.488500452 33.04727079
Maharashtra Hingoli 0.88 10.83386773 74.06495123
Maharashtra Parbhani 0.88 16.6857108 61.7618129
Jharkhand Dumka 0.97 8.0869989 52.51838522
Odisha Baleshwar 0.95 4.061746266 30.2948164
Uttar Pradesh Chitrakoot 0.92 3.484718623 60.09700221
Madhya Pradesh Chhatarpur 0.91 3.991829208 91.41073835
Tamil Nadu Karur 0.94 5.006326956 51.10414066
Uttar Pradesh Mahoba 0.89 6.558990271 67.24066679
Maharashtra Yavatmal 0.92 8.62936399 91.97979474
Chhattisgarh Dhamtari 0.97 1.303481828 96.40138988
Bihar Pashchim Champaran 0.96 21.98426345 25.1077371
Tamil Nadu Viluppuram 0.94 3.87108749 39.40589897
Odisha Balangir 0.96 0.473681881 75.92494104
Himachal Pradesh Hamirpur 0.9 0.816020476 46.83706857
Jammu & Kashmir Punch 0.89 90.44617111 66.89945159
Assam Barpeta 0.96 70.73809858 25.80268797
Uttarakhand Tehri Garhwal 0.89 1.191409059 112.9366601
Assam Marigaon 0.96 52.56370486 31.22966547
Kerala Palakkad 0.97 28.93078627 31.03275735
Gujarat The Dangs 0.97 1.57386844 151.5609463
Odisha Bargarh 0.96 0.508150183 77.29931713
Andhra Pradesh Prakasam 0.94 6.494698374 102.4886915
Himachal Pradesh Sirmaur 0.93 6.268696153 105.5005615
Tamil Nadu Ariyalur 0.88 1.052068237 50.86806889
Assam Dibrugarh 0.96 4.86498509 50.51514135
Tamil Nadu Krishnagiri 0.94 6.133761462 54.10124114
Karnataka Davanagere 0.95 13.66257568 60.39587828
Madhya Pradesh Mandla 0.98 1.569620013 109.7729179
Tamil Nadu Namakkal 0.92 1.881326375 40.25249609
Bihar Banka 0.95 12.33190303 30.17550447
Tamil Nadu Erode 0.96 3.379513835 52.0929555
Madhya Pradesh Tikamgarh 0.9 3.054527992 71.27208916
Bihar Buxar 0.95 6.178268024 20.39438521
Karnataka Raichur 0.96 14.10308522 89.48513385
Odisha Anugul 0.89 0.58972179 102.761691
Madhya Pradesh Guna 0.91 3.963129038 105.9991833
Tamil Nadu Tiruvannamalai 0.93 3.71767331 51.84847102
West Bengal Koch Bihar 0.95 25.54136341 24.83074301
Jammu & Kashmir Kupwara 0.87 94.59208552 56.87341013
Bihar Kishanganj 0.97 67.97769759 23.36725035
Jammu & Kashmir Anantnag 0.82 97.98950952 69.52865137
Bihar Katihar 0.96 44.46864553 20.93760756
Punjab Barnala 0.85 2.199732338 52.39057171
Madhya Pradesh Betul 0.96 2.386118238 135.5244065
Kerala Kasaragod 0.96 37.24356057 32.50788794
Gujarat Bhavnagar 0.9 7.390139791 74.74747124
Rajasthan Churu 0.91 12.24467982 146.3560291
Madhya Pradesh Anuppur 0.97 2.865982326 107.9765148
Tamil Nadu Tirunelveli 0.96 9.858467006 46.99026691
Odisha Jagatsinghapur 0.93 4.127545909 31.75102971
Andhra Pradesh Vizianagaram 0.97 0.700498278 60.39734286
Uttarakhand Garhwal 0.9 3.336529549 168.0559779
Maharashtra Washim 0.86 11.91753817 89.04407097
Madhya Pradesh Singrauli 0.94 3.952734214 106.8512985
Assam Sonitpur 0.97 18.21808524 60.49550182
Tamil Nadu Tiruppur 0.96 5.001226275 47.8005302
Himachal Pradesh Una 0.88 2.739013725 67.92370288
Madhya Pradesh Umaria 0.96 2.476277921 145.6360371
Chhattisgarh Mahasamund 0.97 1.217133993 107.1891273
Bihar Munger 0.93 8.072731792 24.12695163
Bihar Madhepura 0.93 12.07735985 20.88160331
Kerala Malappuram 0.97 70.23839511 20.22893711
Odisha Bhadrak 0.95 6.917575549 39.16786217
Tamil Nadu Salem 0.92 2.742259171 35.69730068
Karnataka Kolar 0.97 13.00916883 61.63755426
Andhra Pradesh Khammam 0.96 5.679870736 136.628333
Jharkhand Pakur 0.98 35.86795969 48.42174003
Assam Nagaon 0.97 55.35876177 34.24502296
Assam Hailakandi 0.95 60.31479032 49.7500364
Chhattisgarh Raigarh 0.96 1.160119519 117.5380727
Uttar Pradesh Jalaun 0.89 10.15287809 67.27914157
Gujarat Surendranagar 0.9 6.245117488 147.9273095
West Bengal Puruliya 0.96 7.755634165 54.16169673
Tamil Nadu Coimbatore 0.96 6.102725673 34.73060646
Uttar Pradesh Banda 0.92 8.759093258 62.29819774
Odisha Puri 0.94 2.713438863 52.09774361
Bihar Supaul 0.95 18.35967011 27.94880031
Madhya Pradesh Ashoknagar 0.93 4.816991708 142.5915692
Madhya Pradesh Damoh 0.94 3.794121114 149.6576147
Uttar Pradesh Pilibhit 0.93 24.11050282 47.9565063
Maharashtra Sangli 0.87 8.49025014 80.68336721
Karnataka Udupi 0.96 8.216681205 80.94373773
Bihar Saharsa 0.94 14.0277514 23.67597378
Karnataka Bellary 0.97 13.08140969 92.35116275
Karnataka Shimoga 0.97 13.3883097 129.9099189
Jharkhand Saraikela-kharsawan 0.95 5.970390289 67.69597092
Odisha Jajapur 0.93 5.124912981 43.18101218
Bihar Rohtas 0.94 10.15186907 35.71044874
Odisha Dhenkanal 0.88 0.402829954 102.4470767
Uttarakhand Nainital 0.9 12.64837289 122.6685383
Madhya Pradesh Rewa 0.9 3.611423759 73.99245531
West Bengal Bankura 0.95 8.075516435 53.5216703
Puducherry Karaikal 0.97 14.40151432 21.97560708
Andhra Pradesh Sri Potti Sriramulu Nellore 0.94 9.730806595 123.7701856
Karnataka Chikmagalur 0.98 8.896174825 177.7741065
Maharashtra Sindhudurg 0.92 3.091151543 173.247604
Himachal Pradesh Solan 0.89 2.529294183 94.77529639
Karnataka Gulbarga 0.96 19.99453694 121.4966454
Jharkhand Deoghar 0.95 20.28225161 47.38374061
Madhya Pradesh Shivpuri 0.9 2.966310362 167.1446366
Odisha Sundargarh 0.95 3.410229207 133.1781181
Maharashtra Gondiya 0.95 1.977834522 113.9502475
Karnataka Hassan 0.98 6.755774673 111.1223072
Bihar Kaimur (bhabua) 0.95 9.547745182 59.51853929
Madhya Pradesh East Nimar 0.94 8.875693575 164.3434924
Assam Sivasagar 0.96 8.301377004 68.28547848
Karnataka Yadgir 0.95 13.22863291 132.4225839
Madhya Pradesh Narsimhapur 0.92 3.576302326 139.2127519
Andhra Pradesh Anantapur 0.93 10.86596222 139.0785142
Chhattisgarh Durg 0.97 2.497075247 76.79719798
Gujarat Patan 0.91 10.62688002 129.7875919
Maharashtra Aurangabad 0.85 21.25417626 82.34984527
Andhra Pradesh Srikakulam 0.96 0.333874191 65.36905214
Gujarat Panch Mahals 0.94 6.630190365 66.25463866
Bihar Araria 0.97 42.94548702 30.5167684
Maharashtra Chandrapur 0.95 4.187120941 157.9634779
Tamil Nadu Cuddalore 0.89 4.748775286 43.28615603
Maharashtra Jalna 0.86 13.9976805 120.3136629
Maharashtra Buldana 0.85 13.69685468 114.4124059
Karnataka Ramanagara 0.97 10.55858109 100.3107231
West Bengal Jalpaiguri 0.96 11.51135367 49.67922814
Gujarat Dohad 0.95 3.119431936 53.03029591
Madhya Pradesh Barwani 0.95 4.2010822 123.603686
Bihar Bhagalpur 0.94 17.68069035 26.79600733
Bihar Nawada 0.95 11.01297526 35.91471674
Andhra Pradesh Adilabad 0.94 10.06734546 188.2360495
Tamil Nadu Thiruvarur 0.96 7.600549563 57.66141439
Punjab Faridkot 0.86 0.506066318 76.92208036
Chhattisgarh Bilaspur 0.97 2.176466768 101.8159811
Jharkhand Palamu 0.94 12.28407691 74.28336656
Karnataka Gadag 0.95 13.4951201 143.5321303
Rajasthan Dhaulpur 0.86 5.988979839 82.63462731
Chhattisgarh Raipur 0.97 2.535463715 100.446077
Madhya Pradesh Katni 0.95 2.881640071 126.6212708
Gujarat Banas Kantha 0.9 6.841999342 113.9238316
Jharkhand Purbi Singhbhum 0.93 8.893034148 51.61472572
Madhya Pradesh Rajgarh 0.93 6.917261715 132.3574505
Maharashtra Nanded 0.91 14.04076171 104.5728845
Andhra Pradesh Chittoor 0.93 9.531957344 122.6622304
Jharkhand Sahibganj 0.96 34.61276049 61.36105068
Maharashtra Bid 0.8 12.39415578 141.6994417
Uttar Pradesh Bahraich 0.95 33.52695492 51.43745318
Maharashtra Solapur 0.88 10.21952144 118.4179931
Nagaland Dimapur 0.97 8.239465063 84.21085977
Madhya Pradesh Satna 0.92 2.668135231 116.0643985
Madhya Pradesh Alirajpur 0.98 2.422499894 150.8918394
Tamil Nadu Tiruchirappalli 0.94 7.014829427 57.34142946
Andhra Pradesh Y.s.r. 0.92 15.754133 184.7721519
Andhra Pradesh Warangal 0.92 5.617899798 127.4278478
Maharashtra Bhandara 0.95 2.207885472 118.8835774
Kerala Kollam 0.98 19.29516672 33.12621543
Kerala Thiruvananthapuram 0.97 13.71876464 23.47469746
Andhra Pradesh Nalgonda 0.93 5.407174769 144.5192328
Karnataka Mysore 0.97 9.681329714 74.57198579
Daman & Diu Diu 0.92 6.125897761 26.88481776
Madhya Pradesh Hoshangabad 0.93 4.210657752 196.1574093
West Bengal Purba Medinipur 0.95 14.58897638 33.75279025
Chhattisgarh Janjgir-champa 0.96 0.875034806 88.04061475
Haryana Rohtak 0.82 0.771293738 60.96848485
Karnataka Bagalkot 0.94 11.64126298 129.2233055
Bihar Khagaria 0.93 10.53389374 33.23562619
Goa South Goa 0.95 9.932759544 114.4352317
Maharashtra Amravati 0.94 14.58951097 157.7319284
Assam Karimganj 0.97 56.36012781 55.09951281
Assam Dhubri 0.97 79.67252154 41.81078133
Kerala Kottayam 0.96 6.406469116 42.03487274
Gujarat Jamnagar 0.92 14.85126514 247.7641278
Maharashtra Latur 0.89 14.98103656 110.3823818
Madhya Pradesh Shajapur 0.93 11.55061774 155.8160643
Punjab Tarn Taran 0.83 0.344311096 82.0809073
Himachal Pradesh Bilaspur 0.9 1.828482862 116.5055661
Rajasthan Pratapgarh 0.93 2.949479632 195.5411547
Karnataka Tumkur 0.97 9.179725119 151.1769405
Andhra Pradesh Kurnool 0.95 16.54725848 168.3005371
Bihar Samastipur 0.93 10.61511191 26.51607414
Gujarat Mahesana 0.86 6.704015205 84.22339543
Andhra Pradesh East Godavari 0.97 1.508974261 81.75704306
West Bengal Dakshin Dinajpur 0.97 24.62530037 51.78144888
Jammu & Kashmir Bandipore 0.88 97.39286953 34.92830774
Karnataka Kodagu 0.98 15.73868524 293.7681125
Jammu & Kashmir Kulgam 0.86 98.49063449 38.39965323
Rajasthan Ganganagar 0.86 2.574082049 221.9719191
Tamil Nadu Thanjavur 0.96 7.931119045 56.48637303
Tamil Nadu Vellore 0.95 10.53671554 61.5293785
Gujarat Amreli 0.89 6.54508351 195.616138
Karnataka Haveri 0.95 18.6476164 120.92625
West Bengal Pashchim Medinipur 0.97 10.49392935 63.71907329
Uttar Pradesh Kheri 0.94 20.08334239 77.76202532
Bihar Saran (chhapra) 0.93 10.28499983 27.25297594
Jammu & Kashmir Rajouri 0.87 62.71319941 167.4929757
Assam Jorhat 0.96 5.006518618 107.3924062
Bihar Aurangabad 0.95 9.344337741 53.54176829
Madhya Pradesh Vidisha 0.93 10.25091252 208.9281124
Bihar Purba Champaran 0.94 19.4210031 32.29810108
Uttar Pradesh Sant Kabir Nagar 0.95 23.57824209 40.11233787
Kerala Kannur 0.97 29.4285421 49.14778143
Maharashtra Osmanabad 0.87 10.79437685 191.9067361
Bihar Sheikhpura 0.94 5.917101181 46.35871905
Tamil Nadu Madurai 0.94 5.558492186 52.7606005
Karnataka Chikkaballapura 0.96 11.77671332 146.3623732
Uttar Pradesh Mirzapur 0.91 7.840102204 76.77304894
Rajasthan Jalor 0.9 4.319391053 253.4545832
Uttar Pradesh Hardoi 0.91 13.59003334 63.74538982
Kerala Kozhikode 0.97 39.242256 33.27616659
Maharashtra Kolhapur 0.86 7.393135347 88.15787199
Bihar Jehanabad 0.92 6.730749578 36.78976427
Uttarakhand Udham Singh Nagar 0.9 22.5766601 68.89433089
Punjab Kapurthala 0.88 1.25004907 89.67476643
Rajasthan Bharatpur 0.87 14.56902241 89.26952805
Maharashtra Ahmadnagar 0.86 7.059911396 168.5391156
Uttar Pradesh Saharanpur 0.9 41.94725221 48.11933595
Uttar Pradesh Jhansi 0.87 7.397266991 113.6794051
Punjab Gurdaspur 0.83 1.203790764 70.70372615
Rajasthan Hanumangarh 0.89 6.686963146 249.2826924
Karnataka Dharwad 0.95 20.94364824 106.3332725
Assam Bongaigaon 0.98 50.2207622 68.21836373
Jammu & Kashmir Samba 0.79 7.196658493 131.0763943
Puducherry Yanam 0.9 2.141085104 25.16808687
Haryana Rewari 0.79 0.634543702 82.6361831
Madhya Pradesh Morena 0.85 3.873863792 120.1442545
Madhya Pradesh Harda 0.93 6.773421682 278.3694004
Bihar Bhojpur 0.93 7.250347914 41.96587972
Bihar Sitamarhi 0.94 21.6177889 32.50988587
Bihar Madhubani 0.94 18.24922744 37.86174513
Kerala Alappuzha 0.96 10.55297306 32.33403312
Jharkhand Ramgarh 0.93 13.59081061 68.77716724
Andhra Pradesh Mahbubnagar 0.93 8.244996087 221.5380698
Rajasthan Pali 0.91 7.041514586 296.3820192
Kerala Thrissur 0.95 17.07160707 47.28950404
Tripura North Tripura 0.97 15.2370426 143.0945015
Bihar Nalanda 0.94 6.881753985 40.06737435
Maharashtra Akola 0.92 19.69523228 154.8040527
West Bengal Darjiling 0.96 5.690095911 84.41523633
Madhya Pradesh Datia 0.87 3.620318422 182.7763189
Madhya Pradesh Dewas 0.92 11.1439105 223.1864502
Madhya Pradesh Mandsaur 0.94 9.366380909 207.0260539
Tamil Nadu Kanniyakumari 0.96 4.20183343 45.33852588
Uttar Pradesh Ambedkar Nagar 0.94 16.7513245 49.3767849
Uttar Pradesh Siddharth Nagar 0.94 29.22962829 57.63301407
Karnataka Bangalore Rural 0.96 9.309704185 118.1726532
Bihar Sheohar 0.95 15.13792084 27.27635673
Andhra Pradesh Visakhapatnam 0.96 2.012078062 133.7811662
Jammu & Kashmir Kathua 0.84 10.42023895 209.267806
Haryana Yamunanagar 0.83 11.41232329 75.19323343
Jharkhand Garhwa 0.96 14.71744442 159.8144519
Andhra Pradesh Nizamabad 0.95 15.3486704 161.2097196
Rajasthan Karauli 0.86 5.592532957 196.5372145
Tamil Nadu Nagappattinam 0.96 7.956757091 82.52652417
Bihar Arwal 0.95 9.16881527 47.37152258
Karnataka Mandya 0.94 4.308469134 144.0937351
Andhra Pradesh Karimnagar 0.94 6.480549982 165.7456076
Maharashtra Ratnagiri 0.93 11.59065031 269.4002547
Rajasthan Nagaur 0.9 13.74009408 286.2072416
Uttar Pradesh Budaun 0.91 21.47032399 75.66753651
Jharkhand Godda 0.96 22.01528528 93.56317341
Madhya Pradesh Bhind 0.86 3.921068934 142.1604752
West Bengal South 24 Parganas 0.97 35.56835177 66.60164144
Uttar Pradesh Jyotiba Phule Nagar 0.91 40.77597202 66.78545675
Uttar Pradesh Bijnor 0.88 43.04462498 68.15627501
Madhya Pradesh Burhanpur 0.93 23.86233633 248.9948499
Punjab Bathinda 0.87 1.173835545 133.9550962
Karnataka Bidar 0.96 19.67850643 177.4790113
Kerala Ernakulam 0.96 15.6714258 52.70553024
Jharkhand Bokaro 0.93 11.70768015 79.42472834
Madhya Pradesh West Nimar 0.95 7.21204925 243.7740451
Bihar Purnia 0.96 38.46209925 56.48438608
Uttar Pradesh Balrampur 0.97 37.51050071 89.07856739
Rajasthan Udaipur 0.93 3.399371664 219.6244321
Karnataka Dakshina Kannada 0.95 24.01819636 134.6159092
Rajasthan Sawai Madhopur 0.87 11.65668702 195.1254576
Uttar Pradesh Bulandshahr 0.85 22.2168908 74.78914291
Andhra Pradesh West Godavari 0.96 2.188030072 114.1996146
Tripura West Tripura 0.95 8.853656318 100.942263
Uttar Pradesh Fatehpur 0.91 13.32075831 92.22355628
Madhya Pradesh Jhabua 0.95 1.534854953 205.9415754
Uttar Pradesh Shahjahanpur 0.92 17.54779085 85.71320236
Maharashtra Dhule 0.9 9.162049909 208.741495
Rajasthan Banswara 0.94 2.716795968 150.8218427
Haryana Jind 0.84 1.725140764 121.9501226
Uttar Pradesh Sitapur 0.95 19.9314584 77.16338477
Gujarat Bharuch 0.93 22.14743984 253.0594403
Bihar Gopalganj 0.96 17.02009983 47.85301552
Andhra Pradesh Guntur 0.95 11.45236121 141.8221196
Rajasthan Tonk 0.89 10.77486798 310.0625754
Bihar Darbhanga 0.94 22.38734592 35.55659403
Bihar Siwan 0.95 18.26418181 41.01530598
Rajasthan Sirohi 0.91 2.941006189 305.7859055
Gujarat Porbandar 0.91 5.733206479 245.6234446
Maharashtra Satara 0.9 4.892898556 217.9948271
Rajasthan Chittaurgarh 0.92 6.33637196 318.6478608
Odisha Khordha 0.92 3.73322414 78.87468562
Uttar Pradesh Rampur 0.93 50.5748519 64.17449297
West Bengal Birbhum 0.96 37.06180098 83.2856518
Maharashtra Wardha 0.92 4.140150403 312.9675101
West Bengal Murshidabad 0.97 66.26831219 48.7062782
Maharashtra Jalgaon 0.84 13.24520079 181.1619471
Haryana Sirsa 0.88 0.735336696 217.5744235
Manipur Bishnupur 0.93 7.873242937 138.1640192
Madhya Pradesh Raisen 0.95 9.036592903 422.4251031
Punjab Firozpur 0.86 0.337296718 174.6116702
Rajasthan Jhalawar 0.91 6.814685263 295.0120081
Bihar Gaya 0.97 11.11533905 76.26238267
Uttar Pradesh Chandauli 0.93 11.0142281 89.77056017
Tamil Nadu Thiruvallur 0.95 3.83822447 63.16883864
Uttarakhand Dehradun 0.9 11.90886512 127.0706444
Madhya Pradesh Dhar 0.93 5.316239918 264.0689214
Uttar Pradesh Ballia 0.92 6.588113862 65.28233142
Gujarat Junagadh 0.92 12.20736383 228.5750116
Rajasthan Jodhpur 0.89 11.16190895 440.0670976
Karnataka Belgaum 0.94 11.05542841 200.9138305
West Bengal Barddhaman 0.95 20.72887516 65.52586613
Odisha Jharsuguda 0.95 2.205848094 262.8105021
Bihar Muzaffarpur 0.92 15.52877259 47.6144653
Rajasthan Bhilwara 0.93 5.913458165 313.5116418
Haryana Fatehabad 0.86 0.887463098 197.3437677
Jammu & Kashmir Jammu 0.8 7.025617697 113.7939734
Jharkhand Dhanbad 0.92 16.08359437 56.80787428
Uttar Pradesh Kansiram Nagar 0.91 14.88265973 101.9684434
Maharashtra Pune 0.89 7.144711524 124.3874483
Punjab Amritsar 0.83 0.501956111 80.9425308
West Bengal Nadia 0.96 26.75675362 57.2219212
Uttar Pradesh Basti 0.94 14.79064007 82.16796837
Andhra Pradesh Krishna 0.93 6.796899454 145.7254818
Uttar Pradesh Unnao 0.93 11.69273126 111.3124673
Maharashtra Nashik 0.89 11.34813786 193.3950934
Odisha Ganjam 0.91 0.377299038 176.904085
Rajasthan Dungarpur 0.93 2.064164684 207.2662745
Uttar Pradesh Shrawasti 0.95 30.78512674 112.1392281
Uttar Pradesh Auraiya 0.91 7.391060096 112.6458361
Uttar Pradesh Azamgarh 0.93 15.57662661 67.79494975
Odisha Cuttack 0.92 5.382534378 116.937896
Jharkhand Ranchi 0.94 14.09482979 136.7417311
Rajasthan Rajsamand 0.91 2.911731571 314.7163619
Jammu & Kashmir Pulwama 0.81 95.48908001 151.8449789
Punjab Muktsar 0.85 0.480432334 226.3010369
Jammu & Kashmir Badgam 0.83 97.65291975 143.2845326
Bihar Vaishali 0.91 9.555879636 46.40887709
Uttar Pradesh Baghpat 0.84 27.97924558 81.11750296
Uttar Pradesh Kushinagar 0.94 17.40037435 65.39405882
Punjab Moga 0.87 0.942810717 181.4719818
Madhya Pradesh Sehore 0.93 10.52212559 404.4742293
Bihar Lakhisarai 0.92 4.084874594 99.40933868
Uttar Pradesh Mahamaya Nagar 0.87 10.19027192 95.4171641
Uttar Pradesh Farrukhabad 0.9 14.68520118 93.88904331
Bihar Begusarai 0.93 13.712923 53.62659529
Andhra Pradesh Medak 0.96 11.2896962 267.3006981
Uttar Pradesh Gonda 0.94 19.76211437 97.61441665
Madhya Pradesh Ratlam 0.95 10.38239424 280.6052496
Maharashtra Nagpur 0.93 8.401592756 178.8519352
Madhya Pradesh Ujjain 0.95 11.73371705 259.2024416
Rajasthan Bundi 0.9 5.995916846 444.2320052
Gujarat Rajkot 0.88 9.498816945 253.3802875
Rajasthan Baran 0.92 6.541294045 496.3381871
Punjab Sangrur 0.85 10.82161399 190.3128925
Haryana Bhiwani 0.85 0.949251887 254.0923677
Jharkhand Jamtara 0.95 20.78347294 202.0120297
Tamil Nadu Kancheepuram 0.96 4.346524431 99.54350051
West Bengal Maldah 0.96 51.27175912 83.3073233
Maharashtra Nandurbar 0.95 5.835241871 325.5485214
Madhya Pradesh Sagar 0.94 4.350717986 388.7813028
Rajasthan Ajmer 0.9 12.16231806 297.7098409
Haryana Karnal 0.83 2.102537394 153.5881976
Uttar Pradesh Moradabad 0.92 47.11628611 71.91944017
Uttar Pradesh Kanpur Dehat 0.9 9.816755967 156.9995056
Uttar Pradesh Kaushambi 0.93 13.77991693 105.651677
Rajasthan Jhunjhunun 0.84 10.67726697 265.7875712
Gujarat Sabar Kantha 0.92 6.11725574 292.1861212
Uttar Pradesh Muzaffarnagar 0.87 41.30440554 92.98874964
Rajasthan Sikar 0.85 12.23542234 277.776429
Uttar Pradesh Ghazipur 0.92 10.16921952 89.77235939
Haryana Mewat 0.91 79.19547437 137.6159844
Uttar Pradesh Maharajganj 0.94 17.0838264 111.1854831
Assam Kamrup Metropolitan 0.95 12.04772485 77.595543
Gujarat Narmada 0.95 3.828581206 495.3438693
Haryana Kaithal 0.83 0.766263553 224.4243715
Madhya Pradesh Jabalpur 0.92 8.267483028 221.2895036
Punjab Mansa 0.84 1.347838457 299.9671322
Maharashtra Raigarh 0.94 8.635069471 285.9312125
Uttar Pradesh Faizabad 0.94 14.80398997 100.8500216
Gujarat Valsad 0.93 5.51299835 188.253586
Punjab Jalandhar 0.88 1.378242972 128.829909
Gujarat Anand 0.9 11.98994622 164.9986023
Uttar Pradesh Mau 0.94 19.42707238 83.99940525
Uttar Pradesh Rae Bareli 0.94 12.13436619 146.4076823
Punjab Fatehgarh Sahib 0.85 2.800572511 213.1087721
Punjab Shahid Bhagat Singh Nagar 0.9 1.115284741 227.6624586
Haryana Palwal 0.87 20.00234006 142.0340114
Haryana Kurukshetra 0.82 1.655514148 174.7775111
Uttar Pradesh Bareilly 0.91 34.54159163 102.622113
Uttar Pradesh Sant Ravi Das Nagar(bhadohi) 0.92 12.91885189 71.59996781
Haryana Ambala 0.82 1.962423007 157.1320955
Gujarat Kheda 0.91 11.49979238 194.8358287
Punjab Rupnagar 0.88 2.116773075 225.9624584
Madhya Pradesh Gwalior 0.85 6.975024065 259.0997404
Haryana Hisar 0.86 1.241448199 267.4991155
Punjab Patiala 0.84 2.112322399 216.1750416
Uttar Pradesh Sultanpur 0.94 17.12512414 145.2154358
Rajasthan Alwar 0.87 14.89679735 283.6552057
Uttar Pradesh Kannauj 0.91 16.53774924 157.7915461
Rajasthan Dausa 0.86 2.783146691 262.6025677
West Bengal Hugli 0.95 15.76700739 71.91331266
Uttar Pradesh Etawah 0.88 7.204468299 184.3457811
Uttar Pradesh Gorakhpur 0.92 9.093820052 94.89078215
Uttar Pradesh Mainpuri 0.9 5.390497017 189.0792169
Uttarakhand Hardwar 0.88 34.28435556 161.6570268
Manipur Thoubal 0.93 25.42186997 157.993974
Uttar Pradesh Bara Banki 0.95 22.60576643 176.0972111
Haryana Sonipat 0.8 3.110342683 191.2412474
Goa North Goa 0.94 7.083182561 283.3713118
Punjab Ludhiana 0.87 2.221171685 139.0500978
Uttar Pradesh Pratapgarh 0.93 14.09704341 165.7452882
Uttar Pradesh Etah 0.89 8.247261169 196.1137911
Gujarat Vadodara 0.9 9.232201835 260.0569518
Madhya Pradesh Neemuch 0.94 8.149944254 743.5232251
Gujarat Navsari 0.93 5.916421493 248.3319195
Uttar Pradesh Firozabad 0.89 12.60177507 143.0655251
Haryana Jhajjar 0.79 0.860492172 288.3958243
Gujarat Tapi 0.96 2.764360823 589.5750054
Punjab Hoshiarpur 0.87 1.455227291 324.3362483
Uttar Pradesh Deoria 0.93 11.56224584 127.6707173
Haryana Panchkula 0.87 4.178031794 257.6194608
Uttar Pradesh Meerut 0.86 34.4294447 123.9949368
Gujarat Ahmadabad 0.87 12.24300601 192.2451823
Manipur Imphal West 0.95 4.690806036 172.0103785
Uttar Pradesh Jaunpur 0.93 10.76386386 157.4694874
Rajasthan Jaipur 0.87 10.37478921 306.3304366
Haryana Mahendragarh 0.78 0.613824277 376.7536287
Uttar Pradesh Mathura 0.88 8.516581448 239.9119969
Rajasthan Kota 0.91 12.50595844 490.5141634
Gujarat Gandhinagar 0.87 4.115169861 288.4850976
Haryana Gurgaon 0.84 4.677793391 156.4282847
Maharashtra Thane 0.93 12.25688843 174.3828383
Dadara & Nagar Havelli Dadra & Nagar Haveli 0.93 3.759575688 292.1075677
West Bengal North 24 Parganas 0.96 25.82158391 91.1308649
Uttar Pradesh Aligarh 0.89 19.85043642 233.812181
Bihar Patna 0.92 7.535405282 138.7693512
West Bengal Uttar Dinajpur 0.95 49.92028955 274.3808557
Andhra Pradesh Rangareddy 0.93 11.65845187 379.6107833
Uttar Pradesh Gautam Buddha Nagar 0.85 13.07554388 217.4605534
Uttar Pradesh Agra 0.87 9.308257428 284.3534111
Madhya Pradesh Bhopal 0.92 22.16029027 391.4281412
Jammu & Kashmir Ganderbal 0.84 97.69201805 322.4114629
Uttar Pradesh Varanasi 0.9 14.87654756 154.7252111
West Bengal Haora 0.97 26.19862685 117.504452
Uttar Pradesh Allahabad 0.9 13.3809822 383.2465822
Madhya Pradesh Indore 0.91 12.66952666 513.016614
Puducherry Mahe 0.98 30.74421274 93.2657356
Punjab Sahibzada Ajit Singh Nagar 0.85 2.964726511 479.6768239
Jammu & Kashmir Srinagar 0.85 95.1903618 699.0457048
Haryana Faridabad 0.85 8.933914561 194.6696004
Puducherry Puducherry 0.97 3.438322447 157.1101002
Haryana Panipat 0.84 7.185941696 575.2270753
Karnataka Bangalore 0.95 12.97393736 159.3610012
Uttar Pradesh Lucknow 0.92 21.46197753 394.7633882
Gujarat Surat 0.84 10.86559797 644.4322468
NCT of Delhi South West 0.85 4.914656091 163.7622669
Daman & Diu Daman 0.91 8.414891224 378.191481
Uttar Pradesh Ghaziabad 0.86 25.34955128 263.3262454
NCT of Delhi North West 0.87 7.967370237 134.0885466
Uttar Pradesh Kanpur Nagar 0.88 15.73057939 1816.091091
NCT of Delhi South 0.9 16.32231292 311.3184859
Chandigarh Chandigarh 0.89 4.874413757 392.155005
NCT of Delhi North East 0.89 29.33520519 108.8496554
NCT of Delhi West 0.88 5.890392699 202.8905614
Maharashtra Mumbai Suburban 0.92 19.19199843 203.8802765
NCT of Delhi East 0.88 10.46055041 187.6741163
NCT of Delhi New Delhi 0.89 5.97166277 1439.396073
Andhra Pradesh Hyderabad 0.91 43.45079011 454.0079522
Tamil Nadu Chennai 0.95 9.453310413 359.0049953
NCT of Delhi North 0.88 13.49042431 912.6352229
Maharashtra Mumbai 0.92 25.05899538 1242.589723
West Bengal Kolkata 0.93 20.60211346 1552.207021
NCT of Delhi Central 0.92 33.36275587 1538.157714
Arunanchal Pradesh Dibang Valley 0.83 1.386806597
Lakshadweep Lakshadweep 0.91 96.57996371
Nagaland Kiphire 0.95 0.762120966
Arunanchal Pradesh Upper Siang 0.98 1.163646659
Mizoram Kolasib 0.99 5.271871836
<html>
<head>
<style>
body {
margin: 0;
font-family: "Helvetica Neue", sans-serif;
}
#select, #legend {
margin: 0 auto;
display: table;
}
#select select {
margin-right: 15px;
}
#legend {
margin: 5px auto;
}
#legend text {
font-size: .7em;
}
.subunit.district {
fill: #ddd;
stroke: #fff;
stroke-width: .5px;
}
.subunit.state {
fill: none;
stroke: #000;
stroke-width: .5px;
}
.subunit-boundary {
fill: none;
stroke: #3a403d;
}
</style>
</head>
<body>
<div id="select">
<select id="column"></select>
<select id="breaks"></select>
<select id="count"></select>
<select id="colors"></select>
<button id="states" data="showing">Hide states</button>
</div>
<div id="legend"></div>
<div id="map"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/3.0.0/topojson.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/1.3.4/chroma.min.js"></script>
<script src="http://www.hindustantimes.com/static/common/js/d3.moveto.js"></script>
<script>
var width = window.innerWidth, height = window.innerHeight * .9;
var projection = d3.geoMercator();
var path = d3.geoPath()
.projection(projection);
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
var legendHeight = 22,
legendBarHeight = 10,
legendWidth = width / 2;
var legend = d3.select("#legend").append("svg")
.attr("width", legendWidth)
.attr("height", legendHeight);
var legendX = d3.scaleLinear()
.range([0, legendWidth]);
var t = d3.transition()
.duration(750);
var columns = [{
name: "Sex ratio",
column: "sex_ratio"
},{
name: "Muslim pop. (%)",
column: "muslim_pct"
},{
name: "Houseless / lakh pop.",
column: "houseless_per_lakh"
}];
columns.forEach(function(d){
$("#select #column").append("<option value='" + d.column + "'>" + d.name + "</option>")
});
var breaks = [
{
type: "Equidistant",
value: "e"
},
{
type: "Quantile",
value: "q"
},
{
type: "Logarithmic",
value: "l"
},
{
type: "K-means",
value: "k"
}
];
breaks.forEach(function(d){
$("#select #breaks").append("<option value='" + d.value + "'>" + d.type + "</option>");
});
[3,4,5,6,7,8].forEach(function(d){
$("#select #count").append("<option value='" + d + "'>" + d + "</option>");
});
$("#select #count").val(4);
var colors = {
"BuGn": {
3:["#e5f5f9", "#99d8c9", "#2ca25f"],
4:["#edf8fb", "#b2e2e2", "#66c2a4", "#238b45"],
5:["#edf8fb", "#b2e2e2", "#66c2a4", "#2ca25f", "#006d2c"],
6:["#edf8fb", "#ccece6", "#99d8c9", "#66c2a4", "#2ca25f", "#006d2c"],
7:["#edf8fb", "#ccece6", "#99d8c9", "#66c2a4", "#41ae76", "#238b45", "#005824"],
8:["#f7fcfd", "#e5f5f9", "#ccece6", "#99d8c9", "#66c2a4", "#41ae76", "#238b45", "#005824"]
},
"BuPu": {
3:["#e0ecf4", "#9ebcda", "#8856a7"],
4:["#edf8fb", "#b3cde3", "#8c96c6", "#88419d"],
5:["#edf8fb", "#b3cde3", "#8c96c6", "#8856a7", "#810f7c"],
6:["#edf8fb", "#bfd3e6", "#9ebcda", "#8c96c6", "#8856a7", "#810f7c"],
7:["#edf8fb", "#bfd3e6", "#9ebcda", "#8c96c6", "#8c6bb1", "#88419d", "#6e016b"],
8:["#f7fcfd", "#e0ecf4", "#bfd3e6", "#9ebcda", "#8c96c6", "#8c6bb1", "#88419d", "#6e016b"]
},
"GnBu": {
3:["#e0f3db", "#a8ddb5", "#43a2ca"],
4:["#f0f9e8", "#bae4bc", "#7bccc4", "#2b8cbe"],
5:["#f0f9e8", "#bae4bc", "#7bccc4", "#43a2ca", "#0868ac"],
6:["#f0f9e8", "#ccebc5", "#a8ddb5", "#7bccc4", "#43a2ca", "#0868ac"],
7:["#f0f9e8", "#ccebc5", "#a8ddb5", "#7bccc4", "#4eb3d3", "#2b8cbe", "#08589e"],
8:["#f7fcf0", "#e0f3db", "#ccebc5", "#a8ddb5", "#7bccc4", "#4eb3d3", "#2b8cbe", "#08589e"]
},
"OrRd": {
3:["#fee8c8", "#fdbb84", "#e34a33"],
4:["#fef0d9", "#fdcc8a", "#fc8d59", "#d7301f"],
5:["#fef0d9", "#fdcc8a", "#fc8d59", "#e34a33", "#b30000"],
6:["#fef0d9", "#fdd49e", "#fdbb84", "#fc8d59", "#e34a33", "#b30000"],
7:["#fef0d9", "#fdd49e", "#fdbb84", "#fc8d59", "#ef6548", "#d7301f", "#990000"],
8:["#fff7ec", "#fee8c8", "#fdd49e", "#fdbb84", "#fc8d59", "#ef6548", "#d7301f", "#990000"]
},
"PuBu": {
3:["#ece7f2", "#a6bddb", "#2b8cbe"],
4:["#f1eef6", "#bdc9e1", "#74a9cf", "#0570b0"],
5:["#f1eef6", "#bdc9e1", "#74a9cf", "#2b8cbe", "#045a8d"],
6:["#f1eef6", "#d0d1e6", "#a6bddb", "#74a9cf", "#2b8cbe", "#045a8d"],
7:["#f1eef6", "#d0d1e6", "#a6bddb", "#74a9cf", "#3690c0", "#0570b0", "#034e7b"],
8:["#fff7fb", "#ece7f2", "#d0d1e6", "#a6bddb", "#74a9cf", "#3690c0", "#0570b0", "#034e7b"]
},
"PuBuGn": {
3:["#ece2f0", "#a6bddb", "#1c9099"],
4:["#f6eff7", "#bdc9e1", "#67a9cf", "#02818a"],
5:["#f6eff7", "#bdc9e1", "#67a9cf", "#1c9099", "#016c59"],
6:["#f6eff7", "#d0d1e6", "#a6bddb", "#67a9cf", "#1c9099", "#016c59"],
7:["#f6eff7", "#d0d1e6", "#a6bddb", "#67a9cf", "#3690c0", "#02818a", "#016450"],
8:["#fff7fb", "#ece2f0", "#d0d1e6", "#a6bddb", "#67a9cf", "#3690c0", "#02818a", "#016450"]
},
"PuRd": {
3:["#e7e1ef", "#c994c7", "#dd1c77"],
4:["#f1eef6", "#d7b5d8", "#df65b0", "#ce1256"],
5:["#f1eef6", "#d7b5d8", "#df65b0", "#dd1c77", "#980043"],
6:["#f1eef6", "#d4b9da", "#c994c7", "#df65b0", "#dd1c77", "#980043"],
7:["#f1eef6", "#d4b9da", "#c994c7", "#df65b0", "#e7298a", "#ce1256", "#91003f"],
8:["#f7f4f9", "#e7e1ef", "#d4b9da", "#c994c7", "#df65b0", "#e7298a", "#ce1256", "#91003f"]
},
"RdPu": {
3:["#fde0dd", "#fa9fb5", "#c51b8a"],
4:["#feebe2", "#fbb4b9", "#f768a1", "#ae017e"],
5:["#feebe2", "#fbb4b9", "#f768a1", "#c51b8a", "#7a0177"],
6:["#feebe2", "#fcc5c0", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177"],
7:["#feebe2", "#fcc5c0", "#fa9fb5", "#f768a1", "#dd3497", "#ae017e", "#7a0177"],
8:["#fff7f3", "#fde0dd", "#fcc5c0", "#fa9fb5", "#f768a1", "#dd3497", "#ae017e", "#7a0177"]
},
"YlGn": {
3:["#f7fcb9", "#addd8e", "#31a354"],
4:["#ffffcc", "#c2e699", "#78c679", "#238443"],
5:["#ffffcc", "#c2e699", "#78c679", "#31a354", "#006837"],
6:["#ffffcc", "#d9f0a3", "#addd8e", "#78c679", "#31a354", "#006837"],
7:["#ffffcc", "#d9f0a3", "#addd8e", "#78c679", "#41ab5d", "#238443", "#005a32"],
8:["#ffffe5", "#f7fcb9", "#d9f0a3", "#addd8e", "#78c679", "#41ab5d", "#238443", "#005a32"]
},
"YlGnBu": {
3:["#edf8b1", "#7fcdbb", "#2c7fb8"],
4:["#ffffcc", "#a1dab4", "#41b6c4", "#225ea8"],
5:["#ffffcc", "#a1dab4", "#41b6c4", "#2c7fb8", "#253494"],
6:["#ffffcc", "#c7e9b4", "#7fcdbb", "#41b6c4", "#2c7fb8", "#253494"],
7:["#ffffcc", "#c7e9b4", "#7fcdbb", "#41b6c4", "#1d91c0", "#225ea8", "#0c2c84"],
8:["#ffffd9", "#edf8b1", "#c7e9b4", "#7fcdbb", "#41b6c4", "#1d91c0", "#225ea8", "#0c2c84"]
},
"YlOrBu": {
3:["#fff7bc", "#fec44f", "#d95f0e"],
4:["#ffffd4", "#fed98e", "#fe9929", "#cc4c02"],
5:["#ffffd4", "#fed98e", "#fe9929", "#d95f0e", "#993404"],
6:["#ffffd4", "#fee391", "#fec44f", "#fe9929", "#d95f0e", "#993404"],
7:["#ffffd4", "#fee391", "#fec44f", "#fe9929", "#ec7014", "#cc4c02", "#8c2d04"],
8:["#ffffe5", "#fff7bc", "#fee391", "#fec44f", "#fe9929", "#ec7014", "#cc4c02", "#8c2d04"]
},
"YlOrRd": {
3:["#ffeda0", "#feb24c", "#f03b20"],
4:["#ffffb2", "#fecc5c", "#fd8d3c", "#e31a1c"],
5:["#ffffb2", "#fecc5c", "#fd8d3c", "#f03b20", "#bd0026"],
6:["#ffffb2", "#fed976", "#feb24c", "#fd8d3c", "#f03b20", "#bd0026"],
7:["#ffffb2", "#fed976", "#feb24c", "#fd8d3c", "#fc4e2a", "#e31a1c", "#b10026"],
8:["#ffffcc", "#ffeda0", "#fed976", "#feb24c", "#fd8d3c", "#fc4e2a", "#e31a1c", "#b10026"]
},
};
Object.keys(colors).forEach(function(d){
$("#select #colors").append("<option value='" + d + "'>" + d + "</option>");
});
d3.queue()
.defer(d3.json, "map.json")
.defer(d3.csv, "data.csv")
.defer(d3.json, "states.json")
.await(ready);
function ready(error, map, data, states) {
if (error) throw error;
var boundary = centerZoom(map);
drawSubUnits(map, "district");
drawOuterBoundary(map, boundary);
var buckets = colorSubUnits(data, $("#select #breaks").val(), $("#select #count").val(), $("#select #colors").val(), $("#select #column").val());
drawLegend(buckets, $("#select #colors").val());
$("#select select").change(function(){
buckets = colorSubUnits(data, $("#select #breaks").val(), $("#select #count").val(), $("#select #colors").val(), $("#select #column").val());
drawLegend(buckets, $("#select #colors").val());
});
drawSubUnits(states, "state");
$("#select #states").click(function(){
if ($(this).attr("data") == "showing"){
$(".subunit.state").fadeOut();
$(this).text("Show states");
$(this).attr("data", "hiding");
} else {
$(".subunit.state").fadeIn();
$(this).text("Hide states");
$(this).attr("data", "showing");
}
});
};
function drawLegend(buckets, colorScheme){
// update legendX domain
legendX.domain([buckets[0].x, buckets[buckets.length - 1].x + buckets[buckets.length - 1].width]);
// JOIN
var legendRect = legend.selectAll(".legend-rect")
.data(buckets, function(d){ return d.bucket; });
var legendNumber = legend.selectAll(".legend-number")
.data(buckets, function(d){ return d.bucket; });
var legendMax = legend.selectAll(".legend-max")
.data([buckets[buckets.length - 1].x + buckets[buckets.length - 1].width]);
// EXIT
legendRect.exit()
.transition(t)
.attr("opacity", 1e-6)
.remove();
legendNumber.exit()
.transition(t)
.attr("opacity", 1e-6)
.remove();
// UPDATE
legendRect
.transition(t)
.attr("width", function(d){ return legendX(d.x + d.width); })
.attr("x", function(d){ return legendX(d.x); })
.attr("fill", function(d){ return d.color });
legendNumber
.transition(t)
.attr("x", function(d){ return legendX(d.x); })
.text(function(d){ return d.x.toFixed(2); });
legendMax
.attr("x", function(d){ return legendX(d); })
.text(function(d){ return d.toFixed(2); })
// ENTER
legendRect.enter().append("rect")
.attr("class", "legend-rect")
.attr("y", 0)
.attr("height", legendBarHeight)
.attr("x", function(d){ return legendX(d.x); })
.attr("fill", function(d){ return d.color })
.attr("width", function(d){ return legendX(d.x + d.width); });
legendNumber.enter().append("text")
.attr("class", "legend-number")
.attr("y", legendHeight)
.attr("x", function(d){ return legendX(d.x); })
.text(function(d){ return d.x.toFixed(2); });
legendMax.enter().append("text")
.attr("class", "legend-max")
.attr("y", legendHeight)
.attr("x", function(d){ return legendX(d); })
.style("text-anchor", "end")
.text(function(d){ return d.toFixed(2); });
}
function colorSubUnits(data, breakType, breakCount, colorScheme, value){
// string to number
var nums = data.filter(function(d){ return d[value] != ""; }).map(function(d){ return +d[value]; });
var buckets = chroma.limits(nums, breakType, breakCount);
svg.selectAll(".subunit.district")
.transition(t)
.style("fill", function(d){
// lookup district
var district = data.filter(function(e){
return e.district == d.properties.district && e.state == d.properties.st_nm;
});
if (district.length == 0){
return "black";
} else {
district = district[0];
if (district[value] == "") return "black";
var bucketNumber = d3.min(buckets.map(function(bucket, i){
if (district[value] <= bucket){
return i;
}
}));
return colors[colorScheme][breakCount][bucketNumber - 1];
}
});
// an array to return for drawing the legend
var arr = [];
buckets.forEach(function(d, i){
if (i != 0){
var obj = {};
obj.bucket = i;
obj.x = buckets[i - 1];
obj.width = d - obj.x;
obj.color = colors[colorScheme][breakCount][i - 1];
arr.push(obj);
}
});
return arr;
}
// This function "centers" and "zooms" a map by setting its projection's scale and translate according to its outer boundary
// It also returns the boundary itself in case you want to draw it to the map
function centerZoom(data){
var o = topojson.mesh(data, data.objects.polygons, function(a, b) { return a === b; });
projection
.scale(1)
.translate([0, 0]);
var b = path.bounds(o),
s = 1 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];
projection
.scale(s)
.translate(t);
return o;
}
function drawOuterBoundary(data, boundary){
svg.append("path")
.datum(boundary)
.attr("d", path)
.attr("class", "subunit-boundary");
}
function drawSubUnits(data, cl){
svg.selectAll(".subunit." + cl)
.data(topojson.feature(data, data.objects.polygons).features)
.enter().append("path")
.attr("class", "subunit " + cl)
.attr("d", path);
}
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment