Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<nav id="menu"></nav>
<div id="map"></div>

<div class='map-overlay' id='features'><h2>Toronto Neighbourhood Profile at 2016</h2><div id='pd'><p>Hover over a neighbourhood!</p></div></div> 

<div id='console'>

  <div class='session'>
  <h5>Population Percentage (%)</h5>
  <div class='row colors'>
  </div>
  <div class='row labels'>
    <div class='label'>0</div>
    <div class='label'>7</div>
    <div class='label'>13</div>
    <div class='label'>19</div>
    <div class='label'>25</div>
    <div class='label'>32</div>
  </div>
</div>
    
</div>


<div id='console2'>
  
  <h4> Toronto Homicide</h4>
  
<div class='session'>
  <h5>Homicide Type</h5>
  <div class='row' id='filters hmc'>
    <input id='all' type='radio' name='toggle' value='all' checked='checked'> 
    <label for='all'>All</label>
    <input id='Shooting' type='radio' name='toggle' value='Shooting'>
    <label for='Shooting'>Shooting</label>
    <input id='Stabbing' type='radio' name='toggle' value='Stabbing'>
    <label for='Stabbing'>Stabbing</label>
    <input id='Other' type='radio' name='toggle' value='Other'>
    <label for='Other'>Other</label>
  </div>
</div>  
  
  
<div class='session' id='sliderbar'>
  <h5><br>Occurrence Year: <label id='occurrence-year'>2016</label></h5>
  <input id='slider' class='row' type='range' min='2004' max='2020' step='1' value='2016' />
</div>
  
<p><small><em>Source: <a href='https://data.torontopolice.on.ca/datasets/homicide-asr-rc-tbl-002/'>Homicide 2004-2020</a></p> 
  
</div> 




              
            
!

CSS

              
                
	body { margin: 0; padding: 0; }
h2,
h3 {
  margin: 10px;
  font-size: 1.2em;
}

h3 {
  font-size: 1em;
}

p {
  font-size: 0.85em;
  margin: 10px;
  text-align: left;
}

/**
* Create a position for the map
* on the page */
#map {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}

    .mapboxgl-popup {
        max-width: 400px;
        font: 12px/15px 'Helvetica Neue', Arial, Helvetica, sans-serif;
    }

/**
* Set rules for how the map overlays
* (information box and legend) will be displayed
* on the page. */
.map-overlay {
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 10px 10px;
  background: rgba(255, 255, 255, 0.8);
  margin-right: 20px;
  font-family: Arial, sans-serif;
  overflow: auto;
  border-radius: 3px;
}

#features {
  top: 0;
  height: 270px;
  margin-top: 20px;
  margin-right: 20px;
  width: 300px;
}


    #menu {
        background: #fff;
        position: absolute;
        z-index: 1;
        bottom: 35px;
        left: 20px;
        border-radius: 3px;
        width: 120px;
        border: 1px solid rgba(0, 0, 0, 0.4);
        font-family: 'Open Sans', sans-serif;
    }

    #menu a {
        font-size: 13px;
        color: #404040;
        display: block;
        margin: 0;
        padding: 0;
        padding: 10px;
        text-decoration: none;
        border-bottom: 1px solid rgba(0, 0, 0, 0.25);
        text-align: center;
    }

    #menu a:last-child {
        border: none;
    }

    #menu a:hover {
        background-color: #f8f8f8;
        color: #404040;
    }

    #menu a.active {
        background-color: #3887be;
        color: #ffffff;
    }

    #menu a.active:hover {
        background: #3074a4;
    }



h4 {
  font-size: 20px;
  line-height: 30px;
}

h5 {
  font-size: 14px;
  line-height: 20px;
  margin-bottom: 10px;
}

a {
  text-decoration: none;
  color: #2dc4b2;
}

#console {
  position: absolute;
  bottom: 10px;
  right: 10px;
  height: 110px;
  width: 240px;
  margin: 25px;
  padding: 1px 10px;
  background-color: white;
  background: rgba(255, 255, 255, 0.8);
  margin-right: 10px;
  font-family: Arial, sans-serif;
  overflow: auto;
  border-radius: 3px;
}

.session {
  margin-right: 5px;
}

.row {
  height: 12px;
  width: 100%;
}

.colors {
  background: linear-gradient(to right, #fef5da, #ffdb78, #ffc216, #b38400, #513c00, #201800);
  margin-bottom: 5px;
}

.label {
  width: 15%;
  display: inline-block;
  text-align: center;
}

#console2 {
  position: absolute;
  top: 20px;
  left: 20px;
  width: 305px;
  height: 275px;
  padding: 0px 20px;
  background-color: white;
  background: rgba(255, 255, 255, 0.8);
  margin-right: 10px;
  font-family: Arial, sans-serif;
  overflow: auto;
  border-radius: 3px;
}



              
            
!

JS

              
                
	  mapboxgl.accessToken = 'pk.eyJ1IjoibHVjaWZlcnllbGxvdyIsImEiOiJja2V1bWJkd2MxcHozMnltcmVzajE0NWs3In0.LjpvVCLIKvcCpSLqE4P-5Q';
    var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/luciferyellow/ckf1ke60u5o3418p7n03byd0u',
        center: [-79.37, 43.73], // starting position //[-79.37, 43.73][-79.3832, 43.653239]
        //pitch: 16, // pitch in degrees
        //bearing: -17.2, // bearing in degrees
        zoom: 10
    });

    map.on('load', function () {
      
        // combine time and type filter
        var filterHmcYear = ['==', ['number', ['get', 'Occurrence_year']], 2016];
        var filterHmc = ['!=', ['string', ['get', 'Homicide_Type']], 'placeholder'];
      
        map.getCanvas().style.cursor = 'default';  
        map.fitBounds([[-79.11319221830604, 43.857310847120004], [-79.64111539940615, 43.580518219783386]]);
        map.setBearing(-17);
		    map.setZoom(10.6);
      
               
        // add source and layer for neighbourhoods
        map.addSource('nbdata-8ov2bh', {
            type: 'vector',
            url: 'mapbox://luciferyellow.7pmak70e'
        });
      // Black population layer
     
        map.addLayer({
            'id': 'Black',
            'type': 'fill',
            'source': 'nbdata-8ov2bh',
            'layout': {
                // make layer visible by default
                'visibility': 'none' 
            },
            'paint': {
                'fill-color': ["interpolate",
                  ["linear"],
                  ["get", "BLK"],
                  0, '#fef5da', 7, '#ffdb78', 13, '#ffc216', 19, '#b38400', 25, '#513c00', 32, '#201800'],
                'fill-opacity': 0.8,
                'fill-outline-color': '#000000'
            },
            'source-layer': 'nbdata-8ov2bh'
        },

                    );
      
      
        // add source and layer for homicides
        map.addSource('Homicide_ASR_RC_TBL_002-6kw4zp', {
            type: 'vector',
            url: 'mapbox://luciferyellow.4kh08v7s'
        });
      // homicide layer
        map.addLayer({
            'id': 'Homicides',
            'type': 'circle',
            'source': 'Homicide_ASR_RC_TBL_002-6kw4zp', //updated April 19, 2021
            'layout': {
                // make layer visible by default
                'visibility': 'none'
            },
            'paint': {
                'circle-radius': 4,
                'circle-color': '#e72323'
            },
            'source-layer': 'Homicide_ASR_RC_TBL_002-6kw4zp',
          'filter': ['all', filterHmcYear, filterHmc]
        });
      
      // interactive time slider for homicide
      document.getElementById('slider').addEventListener('input', function(e) {
  var year = parseInt(e.target.value);
  // update the map
  filterHmcYear = ['==', ['number', ['get', 'Occurrence_year']], year];
  map.setFilter('Homicides', ['all', filterHmcYear, filterHmc]);

  // 
  // var ampm = year !== 2016;
  var year2016 = year - 2016;

  // update text in the UI
  document.getElementById('occurrence-year').innerText = 2016 + year2016;
});
      
      //radio button crime type filter
      // Homicide type
      document.getElementById('filters hmc').addEventListener('change', function(e) {
  var hmc = e.target.value;
  // update the map filter
  if (hmc === 'all') {
    filterHmc = ['!=', ['string', ['get', 'Homicide_Type']], 'placeholder'];
  } else if (hmc === 'Shooting') {
    filterHmc = ['match', ['get', 'Homicide_Type'], ['Shooting'], true, false];
  } else if (hmc === 'Stabbing') {
    filterHmc = ['match', ['get', 'Homicide_Type'], ['Stabbing'], true, false];
  } else if (hmc === 'Other') {
    filterHmc = ['match', ['get', 'Homicide_Type'], ['Other'], true, false];
  } else {
    console.log('error');
  }
  map.setFilter('Homicides', ['all', filterHmcYear,filterHmc]);
});
      
      
      // homicide survivor support source and layer, symbol layer
      map.loadImage(
            'https://i.ibb.co/fCkd76k/blackhand.png',
            function (error, image) {
                if (error) throw error;
                map.addImage('blackhand', image);
                map.addSource('Resource_Assest_Map_-_Toronto', {
                    type: 'vector',
                    url: 'mapbox://luciferyellow.ckevyqoo002p12bpc75c37wed-2cjy7'
                });
              map.addLayer({
                'id': 'Survivor Support',
                'type': 'symbol',
                'source': 'Resource_Assest_Map_-_Toronto',
                'layout': {
                  // make layer visible by default
                  'visibility': 'none',
                  'icon-image': 'blackhand',
                  'icon-size': 0.05,
                  'text-field': ['get', 'Name_of_Organization'],
                  'text-font': [
                  'Open Sans ExtraBold',
                  'Arial Unicode MS Bold'
                  ],
                  'text-size': 10,
                  'text-offset': [0, 0],
                  'text-anchor': 'top'
                          },
                'paint': {
                  'text-color': '#fce8d0',
                  'text-halo-color': '#212b2c',
                  'text-halo-width': 1,
                },
                'source-layer': 'Resource_Assest_Map_-_Toronto'
               });
            }
          )
      
      // When a click event occurs on a feature in the survivor support layer, open a popup at the
        // location of the feature, with description HTML from its properties.
        map.on('click', 'Survivor Support', function (e) {
            var coordinates = e.features[0].geometry.coordinates.slice();
            var description = '<strong>' + e.features[0].properties.Name_of_Organization + '</strong><p>' + e.features[0].properties.Supports;

            // Ensure that if the map is zoomed out such that multiple
            // copies of the feature are visible, the popup appears
            // over the copy being pointed to.
            while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
                coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
            }

            new mapboxgl.Popup()
                .setLngLat(coordinates)
                .setHTML(description)
                .addTo(map);
        });

        // Change the cursor to a pointer when the mouse is over the places layer.
        map.on('mouseenter', 'Survivor Support', function () {
            map.getCanvas().style.cursor = 'pointer';
        });

        // Change it back to a pointer when it leaves.
        map.on('mouseleave', 'Survivor Support', function () {
            map.getCanvas().style.cursor = 'default';
        });
      

      // hover information window
      map.on('mousemove', function(e) {
        var nbhs = map.queryRenderedFeatures(e.point, {
          layers: ['nbdata']
        });
      
      
        if (nbhs.length > 0) {
          document.getElementById('pd').innerHTML = '<h3><strong>' + nbhs[0].properties.AREA_NAME + '</strong></h3><p><strong><em>' + nbhs[0].properties.BLK + '</strong>% Black population. Median: 5.69%</em></p>' + '</strong></h3><p><strong><em>' + nbhs[0].properties.UNEMP + '</strong>% Unemployment rate. Median: 8.2%</em></p>' + '</strong></h3><p><strong><em>' + nbhs[0].properties.LOW_INCOME + '</strong>% Low income prevalence (LIM-AT). Median: 18.6%</em></p>' + '</strong></h3><p><strong><em>' + nbhs[0].properties.NO_CERT + '</strong>% No certificate, diploma or degree. Median: 16.42%</em></p>' + '</strong></h3><p><strong><em>' + '<small>Source: <a href="https://open.toronto.ca/dataset/neighbourhood-profiles/">2016 Census Neighbourhood Profiles</a></small></em>'; 
        } else {
          document.getElementById('pd').innerHTML = '<p>Hover over a neighbourhood!</p>';
        }       
      });
      
      
    }); //map.on end here

    // enumerate ids of the layers
    var toggleableLayerIds = ['Black', 'Homicides','Survivor Support'];

    // set up the corresponding toggle button for each layer
    for (var i = 0; i < toggleableLayerIds.length; i++) {
        var id = toggleableLayerIds[i];

        var link = document.createElement('a');
        link.href = '#';
        link.className = '';
        link.textContent = id;

        link.onclick = function (e) {
            var clickedLayer = this.textContent;
            e.preventDefault();
            e.stopPropagation();

            var visibility = map.getLayoutProperty(clickedLayer, 'visibility');

            // toggle layer visibility by changing the layout object's visibility property
            if (visibility === 'visible') {
                map.setLayoutProperty(clickedLayer, 'visibility', 'none');
                this.className = '';
            } else {
                this.className = 'active';
                map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
            }
        };

        var layers = document.getElementById('menu');
        layers.appendChild(link);
    }

              
            
!
999px

Console