Skip to content

Commit 8efdb27

Browse files
authored
Merge pull request #5 from balladaniel/dev_normalization
Data normalization feature: normalizeByField, updates for Examples
2 parents 76caf01 + 890ab36 commit 8efdb27

13 files changed

+249
-88
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ Aims to simplify data visualization and creation of elegant thematic web maps wi
2020
- Supports ColorBrewer2 color ramps and custom color ramps (thanks to [chroma.js](https://github.com/gka/chroma.js))
2121
- Various SVG shapes/symbols for Point features
2222
- For size/width based symbology, min and max values can be adjusted to create a telling visualization with distinguishable classes
23+
- Normalization by another attribute field
24+
- Rounding of class boundary values to *n* decimals or up/down to the nearest 10, 100, 1000 etc. numbers
2325
- Handling of null/nodata feature attributes
2426
- Legend generation with options for:
2527
- class order (ascending/descending)
2628
- legend header (title)
2729
- custom HTML templating of legend rows, including the display of feature counts in classes
28-
- rounding of class boundary values to n decimals or up/down to the nearest 10, 100, 1000 etc. numbers
2930
- modifying class boundary values in legend by dividing/multiplying by a number (to easily change unit of measurement from m to km for example)
3031
- positioning (L.control options)
3132
- row gap adjustments
@@ -59,7 +60,7 @@ const layer = L.dataClassification(data, {
5960
// required:
6061
mode: 'quantile',
6162
classes: 4,
62-
field: 'density',
63+
field: 'population',
6364
// optional:
6465
pointMode: 'size',
6566
pointSize: {min: 2, max: 10},
@@ -81,9 +82,10 @@ const layer = L.dataClassification(data, {
8182
reverseColorRamp: false,
8283
middlePointValue: 0,
8384
classRounding: 2,
85+
normalizeByField: 'areakm2',
8486
legendTitle: 'Density (pop/km²)',
8587
legendPosition: 'bottomleft',
86-
legendRowGap: 5,
88+
legendRowGap: 5,
8789
legendAscending: false,
8890
legendTemplate: {
8991
highest: '{low} and above [{count}]',
@@ -148,6 +150,7 @@ const layer = L.dataClassification(data, {
148150
- `reverseColorRamp <boolean>`: if true, reverses the chosen color ramp, both in symbology on map and legend colors. Useful when you found a great looking colorramp (green to red), but would prefer reversed colors to match visual implications about colors: green implies positive, red implies negative phenomena. (default: false)
149151
- `middlePointValue <number>`: adjust boundary value of middle classes (only when classifying into even classes). Useful for symmetric classification of diverging data around 0. Only use a value within the range of the two middle classes.
150152
- `classRounding <integer>`: class boundary value rounding. When positive numbers are used for this option, class boundary values are rounded to x decimals, zero will round to whole numbers, while negative numbers will round values to the nearest 10, 100, 1000, etc. Example: with a setting of "1", a value of 254777.253 will get rounded up to 254777.3, with "0" it will be 254777, with "-2" it will become 254800. (default: null - no rounding happens, values are used as-is)
153+
- `normalizeByField <string>`: attribute field name to normalize values of `field` by. Useful for choropleth maps showing population density. Case-sensitive!
151154
- `legendTitle <string>`: legend header (usually a description of visualized data, with a unit of measurement). HTML-markdown and styling allowed. To hide header, set this as ''. (by default it inherits target attribute field name, on which the classification is based on)
152155
- `legendPosition <string>`: ['topleft'|'topright'|'bottomleft'|'bottomright'] legend position, L.control option. (default: 'bottomleft')
153156
- `legendRowGap <number>`: legend symbology row gap in pixels. You can also alter this in the attached CSS file. (default: 3)

examples/banner.png

680 KB
Loading

examples/combined.html

+35-16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#map {
2828
position: absolute;
29-
margin: auto;
29+
margin: auto;
3030
top: 0;
3131
bottom: 0;
3232
left: 0;
@@ -131,6 +131,7 @@
131131
mode: 'quantile',
132132
classes: 5,
133133
field: 'density',
134+
polygonMode: 'color',
134135
colorRamp: 'YlGnBu',
135136
noDataColor: '#505050',
136137
legendAscending: false,
@@ -149,56 +150,74 @@
149150
// Line features example. Attribute to test with: 'length'
150151
fetch('data/rivers_yukon.geojson').then(r => r.json()).then(d => {
151152
function tooltip(feature, layer) {
152-
layer.bindTooltip('<b>' + feature.properties.name + '</b><br>' + String(feature.properties.length)+' m');
153+
layer.bindTooltip('<b>' + feature.properties.name + '</b><br>' + String(feature.properties.discharge)+' m³/s<br>'+ String(Math.round(feature.properties.discharge*35.3147))+' cuft/s');
153154
layer.on('mousemove',e=>{
154155
e.target.getTooltip().setLatLng(e.latlng);
155156
});
157+
158+
// line highlighting
159+
var origstyle;
160+
function highlight(e) {
161+
origstyle = {
162+
weight: e.target.options.weight,
163+
color: e.target.options.color
164+
}
165+
e.target.setStyle({
166+
color: '#f22',
167+
});
168+
}
169+
function resetStyle(e) {
170+
e.target.setStyle(origstyle);
171+
}
172+
layer.on({
173+
mouseover: highlight,
174+
mouseout: resetStyle
175+
});
176+
// line highlighting end
156177
}
157178
window.testdata3 = L.dataClassification(d, {
158179
mode: 'quantile',
159180
classes: 4,
160-
field: 'length',
181+
field: 'discharge',
161182
lineMode: 'width',
162183
lineWidth: {
163-
min: 3,
164-
max: 10
165-
},
166-
classRounding: -4,
167-
legendTitle: 'River length (km)',
168-
unitModifier: {
169-
action: 'divide',
170-
by: 1000
184+
min: 2,
185+
max: 11
171186
},
187+
classRounding: -2,
188+
legendTitle: 'Mean river discharge (m³/s)',
172189
legendTemplate: {
173190
highest: '{low} and above',
174191
lowest: 'under {high}'
175192
},
176-
attribution: "Rivers (10m scale): <a href='https://www.naturalearthdata.com/' target='_blank'>Natural Earth</a>",
193+
attribution: "Rivers (10m scale): <a href='https://www.naturalearthdata.com/' target='_blank'>Natural Earth</a>, Discharge data: <a href='https://www.usgs.gov/' target='_blank'>USGS</a>",
177194
pane: 'front',
178195
onEachFeature: tooltip
179196
}).addTo(map);
180197
});
181-
//testdata3.bringToFront();
182198

183199
map.fitBounds([[17, -150],[68, -65]]);
184200

185201
var infopanel = L.control({position: 'bottomright'});
186202
infopanel.onAdd = function (map) {
187203
var div = L.DomUtil.create('div', 'info');
188204
div.innerHTML +=
189-
'<div style="display: flex; flex-direction: column; max-width: 500px; text-align: center;">' +
205+
'<div style="display: flex; flex-direction: column; max-width: 500px; text-align: center; row-gap: 8px">' +
190206
'<div style="font-weight: bold; margin-bottom: 5px;">' +
191207
'Leaflet-dataclassification plugin demo page: "combined"' +
192208
'</div>'+
193-
'<div style="justify-content: center; ">' +
209+
'<div style="text-align: justify">' +
194210
'This is an example page showcasing some of the features of Leaflet plugin <i>leaflet-dataclassification</i> for three layers simultaneously. '+
195211
'Feature tooltips on hover (native feature of Leaflet) were added to provide an easy check of attribute values used. '+
196212
'<br><i>Note: population density for North Dakota has been manually removed to showcase handling of Null data (nodata) in feature attributes.</i>'+
197213
'<br><br>'+
214+
'</div>'+
215+
'<div>'+
198216
'Single-step data classification, symbology and legend creation for GeoJSON data powered thematic maps.'+
199217
'<br><br>'+
200-
'Project page: <a href="https://github.com/balladaniel/leaflet-dataclassification">https://github.com/balladaniel/leaflet-dataclassification</a>'
218+
'Project page: <a href="https://github.com/balladaniel/leaflet-dataclassification">https://github.com/balladaniel/leaflet-dataclassification</a>'+
201219
'</div>'+
220+
'<a href="https://github.com/balladaniel/leaflet-dataclassification" target="_blank"><img src="banner.png" style="max-width: 100%;"></img></a>'+
202221
'</div>';
203222
return div;
204223
}

0 commit comments

Comments
 (0)