Commit 4f4e017d authored by anneb's avatar anneb

working on graph colors

parent e228b024
...@@ -24,5 +24,5 @@ If you don't have git, you can donwload [a zip file](https://github.com/anneb/pg ...@@ -24,5 +24,5 @@ If you don't have git, you can donwload [a zip file](https://github.com/anneb/pg
For interactive data browsing, preview, administration and api documentation, head to [http://localhost:8090](http://localhost:8090). For interactive data browsing, preview, administration and api documentation, head to [http://localhost:8090](http://localhost:8090).
### Due credit ### Attributions
API based on [Dirt Simple PostGIS http API](https://github.com/tobinbradley/dirt-simple-postgis-http-api) API based on [Dirt Simple PostGIS http API](https://github.com/tobinbradley/dirt-simple-postgis-http-api)
\ No newline at end of file
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
display: inline-block; display: inline-block;
max-width: 400px; max-width: 400px;
} }
#attrinfo { #graphs {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
} }
#attrinfo div { #graphs div {
margin: 5px; margin: 5px;
} }
#colorschemes { #colorschemes {
...@@ -85,13 +85,13 @@ ...@@ -85,13 +85,13 @@
initMap(); initMap();
fetch(`data/${fullTableName}/colstats/${attrName}?geom_column=${geomColumn}`).then(response=>{ fetch(`data/${fullTableName}/colstats/${attrName}?geom_column=${geomColumn}`).then(response=>{
const attrInfoElement = document.querySelector('#attrinfo'); const loadingElement = document.querySelector('#loader');
attrInfoElement.innerHTML = ""; loadingElement.innerHTML = "";
if (!response.ok) { if (!response.ok) {
try { try {
response.json(json=>attrInfoElement.innerHtml = json.error); response.json(json=>loadingElement.innerHtml = json.error);
} catch(err) { } catch(err) {
attrInfoElement.innerHTML = err; loadingElement.innerHTML = err;
} }
return; return;
} }
...@@ -112,7 +112,7 @@ ...@@ -112,7 +112,7 @@
globalStats = json; globalStats = json;
}) })
.catch(err=> .catch(err=>
attrInfoElement.innerHTML = `Failed to parse response, message: ${err.message}` loadingElement.innerHTML = `Failed to parse response, message: ${err.message}`
); );
}) })
} }
...@@ -223,20 +223,36 @@ ...@@ -223,20 +223,36 @@
document.querySelector("#layerjson").innerHTML = `<pre>${JSON.stringify(layerjson, null, 2)}</pre>`; document.querySelector("#layerjson").innerHTML = `<pre>${JSON.stringify(layerjson, null, 2)}</pre>`;
} }
function addCanvas() { function prepareGraphColors(classType) {
const container = document.createElement('div'); globalStats.graphColors = [];
container.classList.add('canvascontainer'); globalStats.classType = classType;
const canvas = document.createElement('canvas'); }
container.classList.add('medium');
container.appendChild(canvas); function addGraphColors(color, upperValue) {
document.querySelector('#attrinfo').appendChild(container); globalStats.graphColors.push({color: color, upperValue: upperValue});
return canvas; }
let graphNoData = null;
let graphValues = null;
let graphMostFrequent = null;
function destroyGraphs() {
if (graphNoData) {
graphNoData.destroy();
}
if (graphValues) {
graphValues.destroy();
}
if (graphMostFrequent) {
graphMostFrequent.destroy();
}
} }
function graphStats(stats) { function graphStats(stats) {
destroyGraphs();
const nullValues = stats.values.filter(value=>value.value === null).reduce((result, value)=>result+value.count,0); const nullValues = stats.values.filter(value=>value.value === null).reduce((result, value)=>result+value.count,0);
const rowCount = stats.percentiles.reduce((result, percentile)=>result + percentile.count, 0); const rowCount = stats.percentiles.reduce((result, percentile)=>result + percentile.count, 0);
new Chart(addCanvas(), { graphNoData = new Chart(document.querySelector('#graphnodata canvas'), {
type: 'doughnut', type: 'doughnut',
data: { data: {
labels: ['no data','data',], labels: ['no data','data',],
...@@ -250,13 +266,21 @@ ...@@ -250,13 +266,21 @@
} }
}); });
if (stats.percentiles.length && typeof(stats.percentiles[0].from) !== 'string') { if (stats.percentiles.length && typeof(stats.percentiles[0].from) !== 'string') {
new Chart(addCanvas(), { graphValues = new Chart(document.querySelector('#graphvalues canvas'), {
type: 'line', type: 'line',
data: { data: {
labels: stats.percentiles.map((percentile, index, arr)=>Math.round((index/(arr.length - 1)) * 100)), labels: stats.percentiles.map((percentile, index, arr)=>Math.round((index/(arr.length - 1)) * 100)),
datasets: [{ datasets: [{
backgroundColor: 'red', backgroundColor: stats.graphColors? stats.percentiles.map((percentile)=>{
borderColor: 'lightred', let color = stats.classType === 'mostfrequent' ?
stats.graphColors.find(graphColor=>percentile.to == graphColor.upperValue) :
stats.graphColors.find(graphColor=>percentile.to < graphColor.upperValue);
if (!color) {
color = stats.graphColors[stats.graphColors.length - 1];
}
return color.color;
}):'red',
//borderColor: 'lighgray',
data: stats.percentiles.map((percentile,index,arr)=>(index===arr.length-1)?percentile.to:percentile.from), data: stats.percentiles.map((percentile,index,arr)=>(index===arr.length-1)?percentile.to:percentile.from),
fill: false fill: false
}] }]
...@@ -299,13 +323,19 @@ ...@@ -299,13 +323,19 @@
count: rowCount - valuesSummeryRowCount count: rowCount - valuesSummeryRowCount
}) })
} }
new Chart(addCanvas(), { graphMostFrequent = new Chart(document.querySelector('#graphmostfrequent canvas'), {
type: "horizontalBar", type: "horizontalBar",
data: { data: {
labels: valuesSummary.map(value=>value.value), labels: valuesSummary.map(value=>value.value),
datasets: [ datasets: [
{ {
backgroundColor: "red", backgroundColor: stats.graphColors? valuesSummary.map((value)=>{
let color = stats.graphColors.find(graphColor=>value.value === graphColor.upperValue)
if (!color) {
color = stats.graphColors[stats.graphColors.length - 1];
}
return color.color;
}):'red',
data: valuesSummary.map(value=>value.count) data: valuesSummary.map(value=>value.count)
} }
] ]
...@@ -447,6 +477,7 @@ ...@@ -447,6 +477,7 @@
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; let reversed = document.querySelector('input[name="colorsreversed"]').checked;
if (prepareLegend()) { if (prepareLegend()) {
prepareGraphColors(classType);
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
...@@ -483,6 +514,7 @@ ...@@ -483,6 +514,7 @@
} }
classValues.forEach((value, index) => { classValues.forEach((value, index) => {
addLegendLine(schemes[selectedColorScheme].colors[index], value.value, layerType); addLegendLine(schemes[selectedColorScheme].colors[index], value.value, layerType);
addGraphColors(schemes[selectedColorScheme].colors[index], value.value)
if (index < classValues.length - 1 || !needsSlice) { if (index < classValues.length - 1 || !needsSlice) {
mapboxPaint.push(value.value); mapboxPaint.push(value.value);
mapboxPaint.push(schemes[selectedColorScheme].colors[index]); mapboxPaint.push(schemes[selectedColorScheme].colors[index]);
...@@ -529,6 +561,7 @@ ...@@ -529,6 +561,7 @@
mapboxPaint = ["case"] mapboxPaint = ["case"]
percentileBreaks.forEach((brk, index, arr)=>{ percentileBreaks.forEach((brk, index, arr)=>{
addLegendLine(seqSchemes[selectedColorScheme].colors[index], `${brk.from} - ${brk.to}`, layerType); addLegendLine(seqSchemes[selectedColorScheme].colors[index], `${brk.from} - ${brk.to}`, layerType);
addGraphColors(seqSchemes[selectedColorScheme].colors[index], brk.to);
if (globalStats.datatype === 'numeric') { if (globalStats.datatype === 'numeric') {
mapboxPaint.push(["<",["to-number", ["get", `${globalStats.column}`], 0],brk.to],seqSchemes[selectedColorScheme].colors[index]); mapboxPaint.push(["<",["to-number", ["get", `${globalStats.column}`], 0],brk.to],seqSchemes[selectedColorScheme].colors[index]);
} else { } else {
...@@ -553,6 +586,7 @@ ...@@ -553,6 +586,7 @@
legendTo = Math.floor(legendTo); legendTo = Math.floor(legendTo);
} }
addLegendLine(intervalSchemes[selectedColorScheme].colors[index], `${(legendFrom)} - ${legendTo}`, layerType); addLegendLine(intervalSchemes[selectedColorScheme].colors[index], `${(legendFrom)} - ${legendTo}`, layerType);
addGraphColors(intervalSchemes[selectedColorScheme].colors[index], legendTo)
if (globalStats.datatype === 'numeric') { if (globalStats.datatype === 'numeric') {
// convert javscript string to number // convert javscript string to number
mapboxPaint.push(["<", ["to-number",["get", globalStats.column], 0],legendTo], intervalSchemes[selectedColorScheme].colors[index]); mapboxPaint.push(["<", ["to-number",["get", globalStats.column], 0],legendTo], intervalSchemes[selectedColorScheme].colors[index]);
...@@ -568,6 +602,7 @@ ...@@ -568,6 +602,7 @@
let colorprop = `${layerType}-color`; let colorprop = `${layerType}-color`;
map.setPaintProperty('attrlayer', colorprop, mapboxPaint); map.setPaintProperty('attrlayer', colorprop, mapboxPaint);
updateLayerJsonDisplay(); updateLayerJsonDisplay();
graphStats(globalStats);
} }
} }
...@@ -589,13 +624,18 @@ ...@@ -589,13 +624,18 @@
<h2 id="tablename"></h2> <h2 id="tablename"></h2>
<h2 id="columnname"></h2> <h2 id="columnname"></h2>
<div id="attrinfo"> <div id="loader">
<h2>Loading statistics...</h2> <h2>Loading statistics...</h2>
<div class="loader"> <div class="loader">
<div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div> <div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div>
<div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div> <div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div>
</div> </div>
</div> </div>
<div id="graphs">
<div id="graphnodata" class="canvascontainer medium"><canvas></canvas></div>
<div id="graphvalues" class="canvascontainer medium"><canvas></canvas></div>
<div id="graphmostfrequent" class="canvascontainer medium"><canvas></canvas></div>
</div>
<div id="mapcontainer"> <div id="mapcontainer">
<div id="map"></div> <div id="map"></div>
<div id="featurecontainer"> <div id="featurecontainer">
......
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