Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bi provider stuart changes #235

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions examples/bi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

<html>
<link rel="stylesheet" href="vendor/leaflet.css" />
<style>
#map, html, body {
width: 100%; height: 100%; padding: 0; margin: 0;
}
#title {
position: absolute;
top: 100px;
left: 50px;
color: white;
font-size: 27px;
font-family: Helvetica, sans-serif;
}
</style>
<body>
<div id="map"></div>
<div id='graphs'></div>

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="../dist/torque.full.uncompressed.js"></script>


<script>
// define the torque layer style using cartocss
// this creates a kind of density map
//color scale from http://colorbrewer2.org/
var CARTOCSS = [
'Map {',
'-torque-time-attribute: "transaction_date";',
'-torque-aggregation-function: "count(1);avg(confidence_score);merchant_name";',
'-torque-frame-count: 1;',
'-torque-animation-duration: 15;',
'-torque-resolution: 1',
'}',
'#layer {',
' marker-width: 1;',
' marker-fill-opacity: 1.0;',
' marker-fill: #fff5eb; ',
' marker-type: ellipse;',
' [value > 1] { marker-fill: #fee6ce; }',
' [value > 2] { marker-fill: #fdd0a2; }',
' [value > 4] { marker-fill: #fdae6b; }',
' [value > 10] { marker-fill: #fd8d3c; }',
' [value > 15] { marker-fill: #f16913; }',
' [value > 20] { marker-fill: #d94801; }',
' [value > 25] { marker-fill: #8c2d04; }',
'}'
].join('\n');


var map = new L.Map('map', {
zoomControl: true,
center: [40.76045572900912, -73.97601127624512],
zoom: 13
});

L.tileLayer('http://{s}.api.cartocdn.com/base-dark/{z}/{x}/{y}.png', {
attribution: 'CartoDB'
}).addTo(map);

var torqueLayer = new L.TorqueLayer({
user : 'stuartlynn',
table : 'yodlee_512',
cartocss: CARTOCSS,
provider: "filterable_sql_api"
});
torqueLayer.addTo(map);

var graphs=[]
// var ndx = crossfilter({});
//
// torqueLayer.onNewData(function(){
// data = torqueLayer.getValues()
// ndx = crossfilter(data)
// })

function addGraph(variable, type){
var dataDim = ndx.dimension(function(d){return d[variable]})
var graphID = "variable_graph"
$("graphs").append("<div id='"+graphID+"'></div>")
}

</script>
</body>
</html>
171 changes: 171 additions & 0 deletions lib/torque/leaflet/torque.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ L.TorqueLayer = L.CanvasLayer.extend({

providers: {
'sql_api': torque.providers.json,
'filterable_sql_api': torque.providers.filterableJson,
'url_template': torque.providers.JsonArray,
'windshaft': torque.providers.windshaft,
'tileJSON': torque.providers.tileJSON
Expand All @@ -28,10 +29,16 @@ L.TorqueLayer = L.CanvasLayer.extend({
options.tileLoader = true;
this.key = 0;
this.prevRenderedKey = 0;
this._filters = {}

if (options.cartocss) {
torque.extend(options, torque.common.TorqueLayer.optionsFromCartoCSS(options.cartocss));
}


options.columns = options.countby.split(";")


options.resolution = options.resolution || 2;
options.steps = options.steps || 100;
options.visible = options.visible === undefined ? true: options.visible;
Expand Down Expand Up @@ -98,11 +105,48 @@ L.TorqueLayer = L.CanvasLayer.extend({
self.redraw();
}
self.fire('tileLoaded');
self.fire('dataUpdate')
});
}, this);

},

setFilters: function() {
this.provider.setFilters(this._filters);
this._reloadTiles();
return this;
},

filterByRange: function(variableName, start, end) {
this._filters[variableName] = {type: 'range', range: {start: start, end: end} }
this._filtersChanged()
this.fire('dataUpdate')
return this
},

filterByCat: function(variableName, categories) {
this._filters[variableName] = {type: 'cat', categories: categories}
this._filtersChanged()
return this
},

clearFilter: function(name){
if(name) {
delete this._filters[name]
}
else {
this._filters = {}
}
this._filtersChanged()
return this
},

_filtersChanged:function(){
this.provider._filters = this._filters;
this._clearTileCaches()
this._render()
},

_clearTileCaches: function() {
var t, tile;
for(t in this._tiles) {
Expand All @@ -118,6 +162,96 @@ L.TorqueLayer = L.CanvasLayer.extend({
this._clearTileCaches();
},

valuesForRangeVariable:function(variable){
var t, tile;

var variable_id = this.provider.idForRange(variable)
var values = [ ]


for(t in this._tiles){
tile = this._tiles[t]
var noPoints = tile.x.length;
for(var i=0; i < tile.x.length; i++){
if(tile.renderFlags[i] ){
value = tile.renderData[variable_id*noPoints + i]
values.push(value)
}
}
}
return values;
},

valuesForCatVariable:function(variable){
var t, tile;

var categories = this.provider.idsForCategory(variable)
var result = [ ]


for(t in this._tiles){
tile = this._tiles[t]
var noPoints = tile.x.length;
for(var i=0; i < tile.x.length; i++){
if(tile.renderFlags[i] ){
var vals={}
Object.keys(categories).forEach(function(categoryName){
var variable_id = categories[categoryName]
value = tile.renderData[variable_id*noPoints + i]
vals[categoryName] = value
}.bind(this))
result.push(vals)
}
}
}
return result;

},
getValues:function(variable,callback){
var type= this.provider._mapping[variable].type
if(type=='float'){
callback(this.valuesForRangeVariable(variable))
}
else{
callback(this.valuesForCatVariable(variable))
}
},
getHistogram:function(variable,callback,noBins){
var type= this.provider._mapping[variable].type
if(type=='float'){
callback(this.histogramForRangeVariable(variable,noBins))
}
else if(type='cat'){
callback(this.histogramForCatVariable(variable))
}
return this
},
histogramForCatVariable:function(variable){
var result = {}
this.valuesForCatVariable(variable).forEach(function(point){
Object.keys(point).forEach(function(key){
result[key] = result[key] || 0
result[key] += point[key]
})
})
return result
},
histogramForRangeVariable:function(variable,noBins){
noBins = noBins || 10

var vals = this.valuesForRangeVariable(variable)

var min = Math.min.apply(null, vals)
var max = Math.max.apply(null, vals)
var binSize = (max-min)/noBins
var result = []
vals.forEach(function(val){
var bin = (val -min)/binSize
result[bin]= result[bin] || 0
result[bin] += val
})
return result
},
onAdd: function (map) {
map.on({
'zoomend': this._clearCaches,
Expand Down Expand Up @@ -208,6 +342,40 @@ L.TorqueLayer = L.CanvasLayer.extend({
canvas.width = canvas.width;
},

/*
_filterTile:function(tile){
var noPoints = tile.x.length

var renderFlags = []
for(var i =0; i < noPoints; i++){
var includePoint = true
Object.keys(this._filters).forEach(function(key){
var filter = this._filters[key]
var variableId = this.provider.idForRange(key)

var value = tile.renderData[variableId*noPoints+i]
if(filter.type=='range'){
if(value < filter.range.start || value > filter.range.end){
includePoint = false;
}
}
else if (filter.type=='cat'){
var ids = this.provider.idsForCategory(key);
filter.categories.forEach(function(key){
var catId = ids[key]
var value = tile.renderData[catId*noPoints + i]
if(value==0){
includePoint= false
}
}.bind(this))
}
}.bind(this))
renderFlags[i] = includePoint
}
return renderFlags
},
*/

/**
* render the selectef key
* don't call this function directly, it's called by
Expand Down Expand Up @@ -236,10 +404,13 @@ L.TorqueLayer = L.CanvasLayer.extend({
// all the points
this.renderer._ctx.drawImage(tile._tileCache, 0, 0);
} else {

//tile.renderFlags = this._filterTile(tile)
this.renderer.renderTile(tile, this.key);
}
}
}

this.renderer.applyFilters();

// prepare caches if the animation is not running
Expand Down
Loading