latest v of ss.js

This commit is contained in:
jlord 2013-08-03 22:24:50 -07:00
parent 9beda801cf
commit 1ca16208e5
1 changed files with 47 additions and 154 deletions

View File

@ -7,8 +7,7 @@ function exportFunctions(exports) {
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
function initiateTableFilter(data, filterDiv, tableDiv) { function initiateTableFilter(data, filterDiv, tableDiv) {
$('.clear').on("click", function() { $('.clear').on("click", function() {
console.log(this)
$(this.id + ".noMatches").css("visibility", "hidden") $(this.id + ".noMatches").css("visibility", "hidden")
$(this.id + filterDiv).val("") $(this.id + filterDiv).val("")
makeTable(data, tableDiv) makeTable(data, tableDiv)
@ -23,16 +22,15 @@ function searchTable(data, searchTerm, tableDiv) {
var filteredList = [] var filteredList = []
data.forEach(function(object) { data.forEach(function(object) {
var stringObject = JSON.stringify(object).toLowerCase() var stringObject = JSON.stringify(object).toLowerCase()
if (stringObject.match(searchTerm)) filteredList.push(object) if (stringObject.match(searchTerm.toLowerCase())) filteredList.push(object)
}) })
// if ($('#tableFilter').val("")) makeTable(data, tableDiv)
if (filteredList.length === 0) { if (filteredList.length === 0) {
console.log("no matchie") console.log("no matchie")
$(".noMatches").css("visibility", "inherit") $(".noMatches").css("visibility", "inherit")
makeTable("no matches", tableDiv) makeTable("no matches", tableDiv)
} }
else $(".noMatches").css("visibility", "hidden") else $(".noMatches").css("visibility", "hidden")
makeTable(filteredList, tableDiv) makeTable(filteredList, tableDiv)
return filteredList return filteredList
} }
@ -44,7 +42,7 @@ function sortThings(data, sorter, sorted, tableDiv) {
}) })
if (sorted === "descending") data.reverse() if (sorted === "descending") data.reverse()
makeTable(data, tableDiv) makeTable(data, tableDiv)
var header var header
$(tableDiv + " .tHeader").each(function(i, el){ $(tableDiv + " .tHeader").each(function(i, el){
var contents = resolveDataTitle($(el).text()) var contents = resolveDataTitle($(el).text())
if (contents === sorter) header = el if (contents === sorter) header = el
@ -60,8 +58,6 @@ function resolveDataTitle(string) {
function sendToSort(event) { function sendToSort(event) {
var tableDiv = "#" + $(event.target).closest("div").attr("id") var tableDiv = "#" + $(event.target).closest("div").attr("id")
console.log("came from this table",tableDiv) console.log("came from this table",tableDiv)
// var dataset = $(tableDiv).attr('dataset')
// console.log("made with this data", dataset, typeof dataset)
var sorted = $(event.target).attr("data-sorted") var sorted = $(event.target).attr("data-sorted")
if (sorted) { if (sorted) {
if (sorted === "descending") sorted = "ascending" if (sorted === "descending") sorted = "ascending"
@ -89,36 +85,34 @@ function makeTable(data, targetDiv) {
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
function getKeywordCount(data, keyword) { function getKeywordCount(data, keyword) {
console.log(data, keyword)
var group = [] var group = []
data.forEach(function (d) { data.forEach(function (d) {
for(var key in d) { for(var key in d) {
var value = d[key].toString().toLowerCase() var value = d[key].toString().toLowerCase()
if (value.match(keyword.toLowerCase())) group.push(d) if (value.match(keyword.toLowerCase())) group.push(d)
} }
}) })
return group.length return group.length
if (group = []) return "0" if (group = []) return "0"
} }
function getKeyword(data, keyword) { function getKeyword(data, keyword) {
console.log(data, keyword)
var group = [] var group = []
data.forEach(function (d) { data.forEach(function (d) {
for(var key in d) { for(var key in d) {
var value = d[key].toString().toLowerCase() var value = d[key].toString().toLowerCase()
if (value.match(keyword.toLowerCase())) group.push(d) if (value.match(keyword.toLowerCase())) group.push(d)
} }
}) })
return group return group
if (group = []) return "no matches" if (group = []) return "no matches"
} }
function getColumnTotal(data, column){ function getColumnTotal(data, column){
var total = [] var total = []
data.forEach(function (d) { data.forEach(function (d) {
if (d[column] === "") return if (d[column] === "") return
total.push(+d[column]) total.push(+d[column])
}) })
return total.reduce(function(a,b) { return total.reduce(function(a,b) {
return a + b return a + b
@ -139,7 +133,7 @@ function getMax(data, column){
if (element[column].valueOf() > result[0][column].valueOf()) { if (element[column].valueOf() > result[0][column].valueOf()) {
result.length = 0 result.length = 0
return result.push(element) return result.push(element)
} }
if (element[column].valueOf() === result[0][column].valueOf()) { if (element[column].valueOf() === result[0][column].valueOf()) {
return result.push(element) return result.push(element)
} }
@ -156,7 +150,7 @@ function getMin(data, column){
if (element[column].valueOf() < result[0][column].valueOf()) { if (element[column].valueOf() < result[0][column].valueOf()) {
result.length = 0 result.length = 0
return result.push(element) return result.push(element)
} }
if (element[column].valueOf() === result[0][column].valueOf()) { if (element[column].valueOf() === result[0][column].valueOf()) {
return result.push(element) return result.push(element)
} }
@ -169,14 +163,8 @@ function getMin(data, column){
function getMatches(data, filter, category) { function getMatches(data, filter, category) {
var matches = [] var matches = []
data.forEach(function (element) { data.forEach(function (element) {
if (category === 'rowNumber') { var projectType = element[category].toLowerCase()
var projectType = element[category].toString() if (projectType === filter.toLowerCase()) matches.push(element)
if (projectType === filter) matches.push(element)
}
else {
var projectType = element[category].toLowerCase()
if (projectType === filter.toLowerCase()) matches.push(element)
}
}) })
return matches return matches
} }
@ -244,32 +232,31 @@ function makeColorArrayOfObject(data, colors) {
var keys = Object.keys(data) var keys = Object.keys(data)
var counter = 1 var counter = 1
var colorIndex var colorIndex
return keys.map(function(key){ return keys.map(function(key){
if (keys.length > colors.length || keys.length <= colors.length ) { if (keys.length > colors.length || keys.length <= colors.length ) {
colorIndex = counter % colors.length colorIndex = counter % colors.length
} }
var h = {label: key, units: data[key], hexcolor: colors[colorIndex]} var h = {label: key, units: data[key], hexcolor: colors[colorIndex]}
counter++ counter++
colorIndex = counter colorIndex = counter
return h return h
}) })
} }
function makeArrayOfObject(data) { function makeArrayOfObject(data) {
var keys = Object.keys(data) var keys = Object.keys(data)
return keys.map(function(key){ return keys.map(function(key){
// var h = {label: key, units: data[key], hexcolor: "#FDBDBD"} // var h = {label: key, units: data[key], hexcolor: "#FDBDBD"}
var h = {label: key, units: data[key]} var h = {label: key, units: data[key]}
return h return h
}) })
} }
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
// //
// // // // Mapbox + Leaflet Map // // // // Mapbox + Leaflet Map
// //
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
function buildOptionObject(optionsJSON, lineItem) { function buildOptionObject(optionsJSON, lineItem) {
var newObj = {} var newObj = {}
@ -317,7 +304,7 @@ function addTileLayer(map, tileLayer) {
layer.addTo(map) layer.addTo(map)
} }
function addMarkerLayer(geoJSON, map, zoomLevel) { function addMarkerLayer(geoJSON, map, zoomLevel) {
var viewCoords = [geoJSON[0].geometry.coordinates[1], geoJSON[0].geometry.coordinates[0]] var viewCoords = [geoJSON[0].geometry.coordinates[1], geoJSON[0].geometry.coordinates[0]]
var markerLayer = L.mapbox.markerLayer(geoJSON) var markerLayer = L.mapbox.markerLayer(geoJSON)
markerLayer.setGeoJSON(geoJSON) markerLayer.setGeoJSON(geoJSON)
@ -340,10 +327,10 @@ function addMarkerLayer(geoJSON, map, zoomLevel) {
// } // }
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
// //
// // // // // D3 Charts // // // // // D3 Charts
// //
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
// Bar Chart // Bar Chart
// Adapted mostly from http://bl.ocks.org/mbostock/3885705 // Adapted mostly from http://bl.ocks.org/mbostock/3885705
@ -457,24 +444,6 @@ function d3BarChart(data, options) {
if (options.xaxis) return options.xaxis if (options.xaxis) return options.xaxis
return return
}) })
d3.select("input").on("change", change)
function change() {
// Copy-on-write since in betweens are evaluated after a delay.
var y0 = y.domain(data.sort(this.checked
? function(a, b) { return b.units - a.units }
: function(a, b) { return d3.ascending(a.label, b.label) })
.map(function(d) { return d.label }))
.copy()
var transition = svg.transition().duration(750),
delay = function(d, i) { return i * 50 }
transition.selectAll(".bar")
.delay(delay)
.attr("transform", function(d) { return "translate(0," + y(d.label) + ")" })
}
} }
// Pie Chart // Pie Chart
@ -557,54 +526,13 @@ function mouseOut(d) {
.style("fill", function(d) { return d.data.hexcolor}) .style("fill", function(d) { return d.data.hexcolor})
.attr("fill", function(d) { return d.data.hexcolor}) .attr("fill", function(d) { return d.data.hexcolor})
// g.append("text")
// .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")" })
// .attr("dy", ".35em")
// .attr("dx", ".35em")
// .attr("class", "pieTip")
// .style("text-anchor", "middle")
// .text(function(d) { return d.data.units })
// var labelr = radius + 8 // radius for label anchor
// g.append("text")
// .attr("transform", function(d) {
// var c = arc.centroid(d),
// x = c[0],
// y = c[1],
// // pythagorean theorem for hypotenuse
// h = Math.sqrt(x*x + y*y)
// return "translate(" + (x/h * labelr) + ',' +
// (y/h * labelr) + ")"
// })
// .attr("dy", ".35em")
// .attr("fill", "#333")
// .attr("class", "pieTip")
// .attr("text-anchor", function(d) {
// // are we past the center?
// return (d.endAngle + d.startAngle)/2 > Math.PI ?
// "end" : "start"
// })
// .text(function(d) { return d.data.units })
// svg.selectAll("rect")
// .data(data)
// .enter().append("g")
// .append("rect")
// .attr("width", 100)
// .attr("height", 26)
// .attr("fill", function(d) { return d.hexcolor })
// .attr("x", 0)
// .attr("y", "-140px") // Controls padding to place text above bars
svg.selectAll("g.labels") svg.selectAll("g.labels")
.data(data) .data(data)
.enter().append("g") // Append legend elements .enter().append("g") // Append legend elements
.append("text") .append("text")
.attr("text-anchor", "start") .attr("text-anchor", "start")
.attr("x", width / 2.5) .attr("x", width / 2.5)
.attr("y", function(d, i) { return data.length + i*(data.length * 2)}) .attr("y", function(d, i) { return (height / 2) - i*(data.length * 20)})
.attr("dx", 0) .attr("dx", 0)
.attr("dy", "-140px") // Controls padding to place text above bars .attr("dy", "-140px") // Controls padding to place text above bars
.text(function(d) { return d.label + ", " + d.units}) .text(function(d) { return d.label + ", " + d.units})
@ -613,49 +541,14 @@ svg.selectAll("g.labels")
.attr("class", function(d, i) { return "labels-" + "index-" + i + " aLabel "}) .attr("class", function(d, i) { return "labels-" + "index-" + i + " aLabel "})
.on('mouseover', mouseOver) .on('mouseover', mouseOver)
.on("mouseout", mouseOut) .on("mouseout", mouseOut)
d3.select("input").on("change", change)
function change() {
console.log("checked/unchecked")
// Copy-on-write since in betweens are evaluated after a delay.
// pie.sort(function(a, b) { return b.units - a.units })
path = path.data(pie(data).sort(function(a, b) { return b.units - a.units; })); // update the data
path.attr("d", arc)
// path.transition().duration(750).attrTween("d", arcTween)
// var pie = d3.layout.pie()
// .sort(null)
// .value(function(d) { return d.units })
// function change() {
// clearTimeout(timeout);
// path = path.data(pie(dataset[this.value])); // update the data
// path.attr("d", arc); // redraw the arcs
// }
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
} }
var transition = svg.transition().duration(750),
delay = function(d, i) { return i * 50 }
transition.selectAll(".path")
.delay(delay)
}
}
// Line Chart // Line Chart
function d3LineChart(data, options){ function d3LineChart(data, options){
// Adapted from http://bl.ocks.org/1166403 and // Adapted from http://bl.ocks.org/1166403 and
// http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html // http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
var m = options.m var m = options.m
var w = options.w - m[1] - m[3] var w = options.w - m[1] - m[3]
var h = options.h - m[0] - m[2] var h = options.h - m[0] - m[2]
@ -676,8 +569,8 @@ function d3LineChart(data, options){
.append("svg:g") .append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")") .attr("transform", "translate(" + m[3] + "," + m[0] + ")")
var div = d3.select(options.div).append("div") var div = d3.select(options.div).append("div")
.attr("class", "tooltip") .attr("class", "tooltip")
.style("opacity", 0) .style("opacity", 0)
// create yAxis // create yAxis
@ -711,7 +604,7 @@ function d3LineChart(data, options){
if (options.yaxis) return options.yaxis if (options.yaxis) return options.yaxis
return return
}) })
var lineData = data.map(function(d) { return d.units }) var lineData = data.map(function(d) { return d.units })
graph.append("svg:path") graph.append("svg:path")
.attr("d", line(lineData)) .attr("d", line(lineData))
@ -719,21 +612,21 @@ function d3LineChart(data, options){
.attr("index_value", function(d, i) { return i }) .attr("index_value", function(d, i) { return i })
// .attr("stroke", options.hiColor).attr("fill", "none") // .attr("stroke", options.hiColor).attr("fill", "none")
graph.selectAll("dot") graph.selectAll("dot")
.data(data) .data(data)
.enter().append("circle") .enter().append("circle")
.attr("r", 3.5) .attr("r", 3.5)
.attr("fill", options.hiColor) .attr("fill", options.hiColor)
.attr("cx", function(d) { return x(d.label); }) .attr("cx", function(d) { return x(d.label); })
.attr("cy", function(d) { return y(d.units); }) .attr("cy", function(d) { return y(d.units); })
.on("mouseover", function(d) { .on("mouseover", function(d) {
div.transition().duration(200).style("opacity", .9) div.transition().duration(200).style("opacity", .9)
div .html(d.label + ", " + d.units) div .html(d.label + ", " + d.units)
.style("left", (d3.event.pageX) + "px") .style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px") .style("top", (d3.event.pageY - 28) + "px")
}) })
.on("mouseout", function(d) { .on("mouseout", function(d) {
div.transition().duration(500).style("opacity", 0) div.transition().duration(500).style("opacity", 0)
}) })
} }
// tables // tables
@ -768,4 +661,4 @@ exports.getMin = getMin
exports.getColumnAverage = getColumnAverage exports.getColumnAverage = getColumnAverage
} }
var Sheetsee = {} var Sheetsee = {}
exportFunctions(Sheetsee) exportFunctions(Sheetsee)