Commit 007c75f9 authored by Krystyna Milian's avatar Krystyna Milian
Browse files

Added search functionality

parent d5086440
...@@ -20,13 +20,16 @@ $(document).ready(function () { ...@@ -20,13 +20,16 @@ $(document).ready(function () {
'soilFilter': undefined, 'shelterTypeFilter': undefined, 'countryFilter': countryChart 'soilFilter': undefined, 'shelterTypeFilter': undefined, 'countryFilter': countryChart
} }
d3.csv('/static/data/shelters-sample.csv', function (data) { var dimensions = []
// d3.json("api/v0.1/shelters", function(dataObject) { // d3.csv('/static/data/shelters-sample.csv', function (data) {
// d3.json("api/v0.1/shelters", function(dataObject) {
// var data = []
// for (var key in dataObject) { var data = []
// data.push(dataObject[key]) for (var key in dataObject) {
// } var shelter = dataObject[key]
shelter["db_id"] = key
data.push(shelter)
}
var dateFormat = d3.time.format('%Y'); var dateFormat = d3.time.format('%Y');
var ndx = crossfilter(data); var ndx = crossfilter(data);
...@@ -36,6 +39,13 @@ $(document).ready(function () { ...@@ -36,6 +39,13 @@ $(document).ready(function () {
}); });
var all = ndx.groupAll(); var all = ndx.groupAll();
var dataCount = dc.dataCount('#data-count') var dataCount = dc.dataCount('#data-count')
var db_idDimension = ndx.dimension(function(d) {
return d.db_id
});
dimensions.push(db_idDimension)
var zoneDimension = ndx.dimension(function (d) { var zoneDimension = ndx.dimension(function (d) {
if (d.zone) { if (d.zone) {
return d.zone; return d.zone;
...@@ -43,19 +53,19 @@ $(document).ready(function () { ...@@ -43,19 +53,19 @@ $(document).ready(function () {
return "No data" return "No data"
} }
}); });
dimensions.push(zoneDimension)
var zoneCount = zoneDimension.group().reduceCount() var zoneCount = zoneDimension.group().reduceCount()
var crisisDimension = ndx.dimension(function (d) { var crisisDimension = ndx.dimension(function (d) {
if (d['associateddisasterimmediatecause']) { if (d['associateddisasterimmediatecause']) {
return d['associateddisasterimmediatecause']; return d['associateddisasterimmediatecause'];
} else { } else {
return 'No data' return 'No data'
} }
}); });
dimensions.push(crisisDimension);
var crisisCount = crisisDimension.group().reduceCount() var crisisCount = crisisDimension.group().reduceCount()
var climateDimension = ndx.dimension(function (d) { var climateDimension = ndx.dimension(function (d) {
if (d['climatezone']) { if (d['climatezone']) {
return d['climatezone']; return d['climatezone'];
...@@ -64,9 +74,9 @@ $(document).ready(function () { ...@@ -64,9 +74,9 @@ $(document).ready(function () {
} }
}); });
dimensions.push(climateDimension);
var climateCount = climateDimension.group().reduceCount() var climateCount = climateDimension.group().reduceCount()
var timeDimension = ndx.dimension(function (d) { var timeDimension = ndx.dimension(function (d) {
if (d['yearofconstructionfirstcompletedshelters']) { if (d['yearofconstructionfirstcompletedshelters']) {
return d3.time.year(dateFormat.parse(d['yearofconstructionfirstcompletedshelters'])); return d3.time.year(dateFormat.parse(d['yearofconstructionfirstcompletedshelters']));
...@@ -74,8 +84,8 @@ $(document).ready(function () { ...@@ -74,8 +84,8 @@ $(document).ready(function () {
else { else {
return undefined; return undefined;
} }
}); });
dimensions.push(timeDimension);
var timeCount = timeDimension.group().reduceCount( var timeCount = timeDimension.group().reduceCount(
function (d) { function (d) {
if (d['yearofconstructionfirstcompletedshelters']) { if (d['yearofconstructionfirstcompletedshelters']) {
...@@ -93,8 +103,8 @@ $(document).ready(function () { ...@@ -93,8 +103,8 @@ $(document).ready(function () {
} else { } else {
return undefined; return undefined;
} }
}); });
dimensions.push(shelters);
var countryDimension = ndx.dimension(function (d) { var countryDimension = ndx.dimension(function (d) {
if (d.country) { if (d.country) {
...@@ -104,6 +114,7 @@ $(document).ready(function () { ...@@ -104,6 +114,7 @@ $(document).ready(function () {
} }
}) })
dimensions.push(countryDimension);
var countryCount = countryDimension.group().reduceCount() var countryCount = countryDimension.group().reduceCount()
var topographyDimension = ndx.dimension(function (d) { var topographyDimension = ndx.dimension(function (d) {
...@@ -112,11 +123,10 @@ $(document).ready(function () { ...@@ -112,11 +123,10 @@ $(document).ready(function () {
} else { } else {
return 'No data' return 'No data'
} }
}) })
dimensions.push(topographyDimension);
var topographyCount = topographyDimension.group().reduceCount() var topographyCount = topographyDimension.group().reduceCount()
var sheltersGroup = shelters.group().reduceCount(); var sheltersGroup = shelters.group().reduceCount();
mapChart.dimension(shelters) mapChart.dimension(shelters)
...@@ -287,6 +297,34 @@ $(document).ready(function () { ...@@ -287,6 +297,34 @@ $(document).ready(function () {
} }
d3.select('#query').on('keydown', function() {
if (d3.event.keyCode == 13) {
var query = this.value
console.log("Searching for " + query);
if (query!="") {
d3.json("api/v0.1/shelters/search/" + query, function(results) {
if (results != null) {
db_idDimension.filterFunction(function(id) {
return id in results;
});
}
dc.renderAll();
generateShelterList(allDimensions.top(Infinity));
})
} else {
db_idDimension.filterAll();
dc.renderAll();
generateShelterList(allDimensions.top(Infinity));
}
}
});
d3.select('#download') d3.select('#download')
.on('click', function () { .on('click', function () {
...@@ -296,6 +334,16 @@ $(document).ready(function () { ...@@ -296,6 +334,16 @@ $(document).ready(function () {
}); });
d3.select('#all').on('click', function () {
for (var i=0; i< dimensions.length; i++ ) {
dimensions[i].filterAll();
}
// dc.filterAll();
dc.renderAll();
$("select").val("");
$("#query").val("");
});
function onFiltered(chart) { function onFiltered(chart) {
...@@ -322,12 +370,7 @@ $(document).ready(function () { ...@@ -322,12 +370,7 @@ $(document).ready(function () {
}) })
d3.selectAll('#all').on('click', function () {
dc.filterAll();
dc.renderAll();
$("select").val("");
});
// Serializing filters values in URL // Serializing filters values in URL
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
<section class="search"> <section class="search">
<div class="content"> <div class="content">
<div class="search-wrapper"> <div class="search-wrapper">
<input type="" name="" placeholder="Search Shelters.."/> <input type="" name="" id="query" placeholder="Search Shelters.."/>
</div> </div>
</div> </div>
</section> </section>
...@@ -155,9 +155,6 @@ ...@@ -155,9 +155,6 @@
<span class="filter-count"></span> selected out of <span <span class="filter-count"></span> selected out of <span
class="total-count"></span> class="total-count"></span>
records records
|
</small> </small>
</h4> </h4>
</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