Commit e228b024 authored by anneb's avatar anneb

add reverse color order checkbox

parent e1f783a4
...@@ -79,7 +79,8 @@ ...@@ -79,7 +79,8 @@
document.querySelector('#back').innerHTML = `<a href="tableinfo.html?table=${fullTableName}&geom_column=${geomColumn}">Back to layer info</a>`; document.querySelector('#back').innerHTML = `<a href="tableinfo.html?table=${fullTableName}&geom_column=${geomColumn}">Back to layer info</a>`;
document.querySelectorAll('[name="colorscheme"]').forEach(radio=>radio.onchange=updateLegendColorSchemes); document.querySelectorAll('[name="colorscheme"]').forEach(radio=>radio.onchange=updateLegendColorSchemes);
document.querySelectorAll('[name="classtype"]').forEach(radio=>radio.onchange=applyLegendToMap); document.querySelectorAll('[name="classtype"]').forEach(radio=>radio.onchange=applyLegendToMap);
document.querySelector('#classcount').onchange = classCountChanged; document.querySelector('#classcount').onchange = updateLegendColorSchemes;
document.querySelector('input[name="colorsreversed"]').onchange = updateLegendColorSchemes
initMap(); initMap();
...@@ -372,29 +373,37 @@ ...@@ -372,29 +373,37 @@
// schemeTypes 'div', 'qual', 'seq' // schemeTypes 'div', 'qual', 'seq'
// for diverging, qualitative and sequential legends // for diverging, qualitative and sequential legends
function getColorSchemes(numClasses, schemeType) { function getColorSchemes(numClasses, schemeType, reversed) {
for (; numClasses > 2; numClasses--) { for (; numClasses > 2; numClasses--) {
let result = colorbrewer.filter(scheme=>scheme.type===schemeType && scheme.sets.length > numClasses - 3) let result = colorbrewer.filter(scheme=>scheme.type===schemeType && scheme.sets.length > numClasses - 3)
.map(scheme=>{ .map(scheme=>{
const result = scheme.sets[numClasses - 3]; let result = Object.assign({}, scheme.sets[numClasses - 3]);
result.name = scheme.name; result.name = scheme.name;
result.type = scheme.type; result.type = scheme.type;
return result; return result;
}); });
if (result.length) { if (result.length) {
if (reversed) {
result.forEach(scheme=>scheme.colors = scheme.colors.map(c=>c).reverse())
}
return result; return result;
} }
} }
if (numClasses === 2) { if (numClasses === 2) {
return colorbrewer.filter(scheme=>scheme.type===schemeType) let result = colorbrewer.filter(scheme=>scheme.type===schemeType)
.map(scheme=>{ .map(scheme=>{
const result = {colors: [scheme.sets[0].colors[0],scheme.sets[0].colors[2]]} const result = {colors: [scheme.sets[0].colors[0],scheme.sets[0].colors[2]]}
result.name = scheme.name; result.name = scheme.name;
result.type = scheme.type; result.type = scheme.type;
return result; return result;
}) });
if (reversed) {
result.forEach(scheme=>scheme.colors = scheme.colors.map(c=>c).reverse());
}
return result;
} }
return []; return [{colors:['#ff0000']}];
} }
function selectColorScheme(schemeIndex) function selectColorScheme(schemeIndex)
...@@ -406,17 +415,14 @@ ...@@ -406,17 +415,14 @@
applyLegendToMap(); applyLegendToMap();
} }
function classCountChanged() {
updateLegendColorSchemes();
}
// display the available colorschemes for current scheme type and class number // display the available colorschemes for current scheme type and class number
function updateLegendColorSchemes() { function updateLegendColorSchemes() {
let schemeDiv = document.querySelector('#colorschemes'); let schemeDiv = document.querySelector('#colorschemes');
schemeDiv.innerHTML = ''; schemeDiv.innerHTML = '';
let classCount = Number(document.querySelector('#classcount').value); let classCount = Number(document.querySelector('#classcount').value);
let schemeType = document.querySelector('input[name="colorscheme"]:checked').value; let schemeType = document.querySelector('input[name="colorscheme"]:checked').value;
let schemes = getColorSchemes(classCount, schemeType); let reversed = document.querySelector('input[name="colorsreversed"]').checked;
let schemes = getColorSchemes(classCount, schemeType, reversed);
classCount = schemes[0].colors.length; classCount = schemes[0].colors.length;
if (selectedColorScheme > schemes.length - 1) { if (selectedColorScheme > schemes.length - 1) {
selectedColorScheme = schemes.length - 1; selectedColorScheme = schemes.length - 1;
...@@ -439,25 +445,26 @@ ...@@ -439,25 +445,26 @@
function applyLegendToMap() { function applyLegendToMap() {
let classType = document.querySelector('input[name="classtype"]:checked').value; let classType = document.querySelector('input[name="classtype"]:checked').value;
let reversed = document.querySelector('input[name="colorsreversed"]').checked;
if (prepareLegend()) { if (prepareLegend()) {
let classCount = Number(document.querySelector('#classcount').value); let classCount = Number(document.querySelector('#classcount').value);
if (classCount === 1) { if (classCount === 1) {
// special case, single classification // special case, single classification
} else { } else {
// classCount > 1 // classCount > 1
const layerType = map.getLayer('attrlayer').type; let layerType = map.getLayer('attrlayer').type;
const rowCount = globalStats.percentiles.reduce((result, percentile)=>result + percentile.count, 0); let rowCount = globalStats.percentiles.reduce((result, percentile)=>result + percentile.count, 0);
let mapboxPaint; let mapboxPaint;
let schemeType = document.querySelector('input[name="colorscheme"]:checked').value; let schemeType = document.querySelector('input[name="colorscheme"]:checked').value;
switch(classType) { switch(classType) {
case 'mostfrequent': case 'mostfrequent':
const schemes = getColorSchemes(classCount, schemeType); let schemes = getColorSchemes(classCount, schemeType, reversed);
classCount = schemes[0].colors.length; classCount = schemes[0].colors.length;
let classValues = globalStats.values.filter(value=>value.value !== null); let classValues = globalStats.values.filter(value=>value.value !== null);
const needsSlice = classValues.length > classCount; let needsSlice = classValues.length > classCount;
if (needsSlice) { if (needsSlice) {
classValues = classValues.slice(0, classCount - 1); classValues = classValues.slice(0, classCount - 1);
const classValuesRowCount = classValues.reduce((result, value)=>result+value.count,0); let classValuesRowCount = classValues.reduce((result, value)=>result+value.count,0);
classValues.push({ classValues.push({
value:"other", value:"other",
count: rowCount - classValuesRowCount count: rowCount - classValuesRowCount
...@@ -489,7 +496,7 @@ ...@@ -489,7 +496,7 @@
result.push(Object.assign({}, percentile)); // spread operator not supported by current Edge result.push(Object.assign({}, percentile)); // spread operator not supported by current Edge
return result; return result;
} }
if (result[result.length - 1].from === percentile.from || result[result.length -1].to === percentile.to) { if (result[result.length - 1].from === percentile.from) {
result[result.length - 1].to = percentile.to; result[result.length - 1].to = percentile.to;
result[result.length - 1].count += percentile.count; result[result.length - 1].count += percentile.count;
return result; return result;
...@@ -497,14 +504,13 @@ ...@@ -497,14 +504,13 @@
result.push(Object.assign({}, percentile)); result.push(Object.assign({}, percentile));
return result; return result;
},[]); },[]);
//const schemeType = (typeof percentileBreaks[0].from === "string") ? 'qual' : 'seq'; let seqSchemes = getColorSchemes(classCount, schemeType, reversed);
const seqSchemes = getColorSchemes(classCount, schemeType);
classCount = seqSchemes[selectedColorScheme].colors.length; classCount = seqSchemes[selectedColorScheme].colors.length;
if (classCount > percentileBreaks.length) { if (classCount > percentileBreaks.length) {
classCount = percentileBreaks.length classCount = percentileBreaks.length
} else { } else {
let totalRowCount = percentileBreaks.reduce((result, percentile)=>result+percentile.count, 0); let totalRowCount = percentileBreaks.reduce((result, percentile)=>result+percentile.count, 0);
const rowCountPerClass = totalRowCount / classCount; let rowCountPerClass = totalRowCount / classCount;
let sumRowCount = 0; let sumRowCount = 0;
let sumClassCount = 0 let sumClassCount = 0
percentileBreaks = percentileBreaks.reduce((result, percentile)=>{ percentileBreaks = percentileBreaks.reduce((result, percentile)=>{
...@@ -532,10 +538,10 @@ ...@@ -532,10 +538,10 @@
mapboxPaint.push(seqSchemes[selectedColorScheme].colors[classCount - 1]) mapboxPaint.push(seqSchemes[selectedColorScheme].colors[classCount - 1])
break; break;
case 'interval': case 'interval':
const min = globalStats.percentiles[0].from; let min = globalStats.percentiles[0].from;
const max = globalStats.percentiles[globalStats.percentiles.length - 1].to; let max = globalStats.percentiles[globalStats.percentiles.length - 1].to;
if (typeof min === "number") { if (typeof min === "number") {
const intervalSchemes = getColorSchemes(classCount, schemeType); let intervalSchemes = getColorSchemes(classCount, schemeType, reversed);
classCount = intervalSchemes[0].colors.length; classCount = intervalSchemes[0].colors.length;
let classTicks = getIntervalClassTicks(min, max, classCount); let classTicks = getIntervalClassTicks(min, max, classCount);
mapboxPaint = ["case"]; mapboxPaint = ["case"];
...@@ -559,14 +565,14 @@ ...@@ -559,14 +565,14 @@
break; break;
} }
if (mapboxPaint) { if (mapboxPaint) {
const colorprop = `${layerType}-color`; let colorprop = `${layerType}-color`;
map.setPaintProperty('attrlayer', colorprop, mapboxPaint); map.setPaintProperty('attrlayer', colorprop, mapboxPaint);
updateLayerJsonDisplay(); updateLayerJsonDisplay();
} }
} }
const nullValues = globalStats.values.filter(value=>value.value === null).reduce((result, value)=>result+value.count,0); let nullValues = globalStats.values.filter(value=>value.value === null).reduce((result, value)=>result+value.count,0);
const checkButtonNullValues = document.querySelector('#hidenulls') let checkButtonNullValues = document.querySelector('#hidenulls')
if (nullValues) { if (nullValues) {
checkButtonNullValues.removeAttribute('disabled'); checkButtonNullValues.removeAttribute('disabled');
} else { } else {
...@@ -618,6 +624,7 @@ ...@@ -618,6 +624,7 @@
<input type="radio" name="colorscheme" id="sequential" value="seq"><label for="sequential">sequential</label> <input type="radio" name="colorscheme" id="sequential" value="seq"><label for="sequential">sequential</label>
<input type="radio" name="colorscheme" id="diverting" value="div"><label for="diverting">diverting</label> <input type="radio" name="colorscheme" id="diverting" value="div"><label for="diverting">diverting</label>
<input type="radio" name="colorscheme" id="qualitative" value="qual" checked><label for="qualitative">qualitative</label><br> <input type="radio" name="colorscheme" id="qualitative" value="qual" checked><label for="qualitative">qualitative</label><br>
<input type="checkbox" name="colorsreversed" id="colorsreversed"><label for="colorsreversed">reverse color order</label><br>
<input type="checkbox" id="hidenulls" name="hidenulls" checked><label for="hidenulls">Hide no-data</label><br> <input type="checkbox" id="hidenulls" name="hidenulls" checked><label for="hidenulls">Hide no-data</label><br>
<div id="colorschemes"></div> <div id="colorschemes"></div>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment