Skip to content

Instantly share code, notes, and snippets.

@enjalot
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/bf29e0083a106ef58b86 to your computer and use it in GitHub Desktop.
Save enjalot/bf29e0083a106ef58b86 to your computer and use it in GitHub Desktop.
Data Canvas API: by sensor
{"description":"Data Canvas API: by sensor","endpoint":"","display":"div","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"ui.html":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"sources.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"controls":{"resolution":"1h","ci":"5m","city":"San Francisco","field":"light","Sensor":"Exploratorium","Count":1},"thumbnail":"http://i.imgur.com/AMh3uza.png"}
/*
Data Canvas API explorer
http://map.datacanvas.org/#!/data
*/
var apiEntries = "http://sensor-api.localdata.com/api/v1/sources/"
var fields = [
"temperature",
"light",
"airquality_raw",
"sound",
"humidity",
"dust"
];
//var field = tributary.control({name: "field", options: fields});
var cities = [
"San Francisco",
"Bangalore",
"Boston",
"Geneva",
"Rio de Janeiro",
"Shanghai",
"Singapore"
]
//var city = cities[0];
var city = tributary.control({name: "city", options: cities});
window.tributary = tributary;
var names =[];
tributary.sources.forEach(function(d) {
if(d.city == city) names.push(d.name);
});
var name = tributary.control({name: "Sensor", options: names })
var count = Math.round(tributary.control({name: "Count", min: 1, max: 100, step: 10 }));
if(count < 1) count = 1;
console.log("count", count);
var id;
tributary.sources.forEach(function(d) {
if(d.name == name) id = d.id;
})
// BUILD UP THE VISUALIZATION
var date = lastDay();
//var date = last7Days();
var url = apiEntries + id +
"/entries?count=" + count + "&sort=desc";
//console.log(url);
var display = d3.select("#display");
display.select("#url").node().value = url;
// REQUEST THE DATA AND USE IT
d3.json(url, function(err, data) {
if(err) { console.log("err", err) }
//console.log(data);
//data.sort(function(a,b) { return b.timestamp - a.timestamp });
display.select(".loader").style("display", "none");
display.select("#data").node().value = JSON.stringify(data, null, 2);
// SIMPLE VISUALIZATION
var svg = display.select("svg");
})
function lastDay() {
var today = new Date();
var day = today.getUTCDate() - 1;
var month = today.getUTCMonth();
if(day < 1) {
month -= 1;
}
var from = new Date(today.getUTCFullYear(), month, day)
var before = new Date(today.getUTCFullYear(), month, today.getUTCDate());
return { from: from, before: before }
}
//console.log("from", lastDay().from.toISOString());
//console.log("before", lastDay().before.toISOString());
function last7Days() {
var today = new Date();
var day = today.getUTCDate() - 7;
var month = today.getUTCMonth();
if(day < 1) {
month -= 1;
}
var from = new Date(today.getUTCFullYear(), month, day)
var before = today;
return { from: from, before: before }
}
//console.log("7 from", last7Days().from.toISOString());
//console.log("7 before", last7Days().before.toISOString());
[
{
"name": "MODseeed",
"city": "Bangalore",
"location": [
77.6027288,
12.9756103
],
"id": "ci53tm6va0001032pzez19tgz"
},
{
"name": "openbangalore",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/openbangalore.png",
"city": "Bangalore",
"location": [
77.61856,
13.00438
],
"id": "ci4v3czb7000502s7lnln7ztr"
},
{
"name": "WRI",
"city": "Bangalore",
"location": [
77.5885708,
12.9436922
],
"id": "ci4yfgz37000e03zzg1a6o6vy"
},
{
"name": "Nextdrop",
"city": "Bangalore",
"location": [
77.5964831,
12.9619378
],
"id": "ci59onqz2000103z5f90u4k78"
},
{
"name": "DataBrigade",
"city": "Bangalore",
"location": [
77.5789318,
12.8864971
],
"id": "ci4yae86r000903zzsna9zh30"
},
{
"name": "Laddoo",
"city": "Bangalore",
"location": [
77.589195,
12.910474
],
"id": "ci4xvuiqf000503zz1bah2he8"
},
{
"name": "Palantir",
"city": "Bangalore",
"location": [
77.5905334,
12.9127007
],
"id": "ci4xs6uhw000403zz9za2guib"
},
{
"name": "Claptrap",
"city": "Bangalore",
"location": [
77.6383672,
12.956511
],
"id": "ci541fl410002032pbifg5o7a"
},
{
"name": "sycsmlp1",
"city": "Bangalore",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/sycsmlp1.png",
"location": [
77.570326,
13.013893
],
"id": "ci4zftqww0002032zeskt2yuc"
},
{
"name": "Geekstorm",
"city": "Bangalore",
"location": [
77.644259,
12.96145
],
"id": "ci4x8lumr000l02tco6291y1u"
},
{
"name": "soldering defect",
"city": "Boston",
"location": [
-71.096538,
42.36502
],
"id": "ci4x0rtb9000h02tcfa5qov33"
},
{
"name": "Pineapple",
"city": "Boston",
"location": [
-71.096945,
42.3895
],
"id": "ci4ooqbyw0001021o7p4qiedw"
},
{
"name": "Sproutuino",
"city": "Boston",
"location": [
-71.097605,
42.382972
],
"id": "ci4xird28000003zzz1soh9fj"
},
{
"name": "monalisa",
"city": "Boston",
"location": [
-71.106167,
42.372802
],
"id": "ci4ue1845000102w7ni64j7pl"
},
{
"name": "SeeedSensor",
"city": "Boston",
"location": [
-71.119359,
42.390486
],
"id": "ci4w1npi3000p02s7a43zws7q"
},
{
"name": "badgersense",
"city": "Boston",
"location": [
-71.110054,
42.376184
],
"id": "ci4vzm23c000o02s76ezwdgxe"
},
{
"name": "Sparkling",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/swissnex.png",
"city": "Boston",
"location": [
-71.109645,
42.372527
],
"id": "ci4x1uh3q000j02tcnehaazvw"
},
{
"name": "Euclid",
"city": "Boston",
"location": [
-71.11682,
42.311615
],
"id": "ci5a6lluy000303z5d02xla24"
},
{
"name": "firehazard",
"city": "Boston",
"location": [
-71.127625,
42.303363
],
"id": "ci530o426000003v9a6uxvc2l"
},
{
"name": "Home1",
"city": "Boston",
"location": [
-71.1196326,
42.3900055
],
"id": "ci4rb6392000102wddchkqctq"
},
{
"name": "theperch",
"city": "Boston",
"location": [
-71.089624,
42.3443141
],
"id": "ci4qaiat7000002wdidwagmmb"
},
{
"name": "Richard",
"city": "Boston",
"location": [
-71.105266,
42.33115
],
"id": "ci4vv79v9000k02s7n4avp69i"
},
{
"name": "Gabe Sensor",
"city": "Boston",
"location": [
-71.107039,
42.370755
],
"id": "ci4w3emre000002tcnpko08o3"
},
{
"name": "Geneva16",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/cern.png",
"city": "Geneva",
"location": [
6.054929,
46.234563
],
"id": "ci4lr75ve000c02ypnfy0qt8n"
},
{
"name": "Geneva6",
"city": "Geneva",
"location": [
6.1401,
46.174962
],
"id": "ci4lr75ok000302yp9dowz3rm"
},
{
"name": "Geneva7",
"city": "Geneva",
"location": [
6.116039,
46.189268
],
"id": "ci4lr75sm000902ypns4q30xy"
},
{
"name": "Laconn'air",
"city": "Geneva",
"location": [
6.035054,
46.1558904
],
"id": "ci4lr75sf000602ypyfkxnua3"
},
{
"name": "OpiOneKenopi",
"city": "Geneva",
"location": [
6.1668213,
46.1927629
],
"id": "ci4lr75sl000802ypo4qrcjda"
},
{
"name": "Orion",
"city": "Geneva",
"location": [
6.145645,
46.19095
],
"id": "ci4lr75ob000102yphtjj75f5"
},
{
"name": "metabolo",
"city": "Geneva",
"location": [
6.1619939,
46.1972488
],
"id": "ci4lr75o6000002yp6eolj0rm"
},
{
"name": "makegeneve",
"city": "Geneva",
"location": [
6.1414264,
46.2154212
],
"id": "ci4lr75oi000202ypmtgrudhs"
},
{
"name": "Geneva11",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/Geneva11.png",
"city": "Geneva",
"location": [
6.13995,
46.220411
],
"id": "ci4lr75vd000b02ypdlm6qbly"
},
{
"name": "LEClife",
"city": "Geneva",
"location": [
6.211192,
46.246715
],
"id": "ci4lr75v6000a02ypa256zigk"
},
{
"name": "Geneva14",
"city": "Geneva",
"location": [
6.149906,
46.212852
],
"id": "ci4lr75se000502ypjgleg6kj"
},
{
"name": "Geneva15",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/Geneva15.jpg",
"city": "Geneva",
"location": [
6.156988953133464,
46.20343253312591
],
"id": "ci4lr75wi000e02yp5ek72zcr"
},
{
"name": "Crisp Cookie",
"city": "Rio de Janeiro",
"location": [
-43.2401755,
-22.9273149
],
"id": "ci4vxorfp000l02s7nv4mlefu"
},
{
"name": "Rio40",
"city": "Rio de Janeiro",
"location": [
-43.1833012,
-22.9135667
],
"id": "ci4vjer3i000e02s7r2cj23gs"
},
{
"name": "PadulaNiteroiRJ",
"city": "Rio de Janeiro",
"location": [
-43.1281207,
-22.8994585
],
"id": "ci4vye225000n02s7rxjdfxa1"
},
{
"name": "coppentt",
"city": "Rio de Janeiro",
"location": [
-43.230439,
-22.859619
],
"id": "ci5a2vxxf000203z5tenrtp4g"
},
{
"name": "BarraDaTijucaCanvas",
"city": "Rio de Janeiro",
"location": [
-43.337678,
-23.002739
],
"id": "ci4q0adco000002t9qu491siy"
},
{
"name": "Kennyduino",
"city": "Rio de Janeiro",
"location": [
-43.2632354,
-22.8427674
],
"id": "ci4xkmrkk000103zzm94zdsu4"
},
{
"name": "swissnexbrazil",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/swissnex.png",
"city": "Rio de Janeiro",
"location": [
-43.178667,
-22.919665
],
"id": "ci4oethyi000302ymejc2wc2j"
},
{
"name": "OurHomeMakerSpace DataCanvas@Rio",
"city": "Rio de Janeiro",
"location": [
-43.2174938,
-22.964142
],
"id": "ci4sn1zfb000102wetn54eux9"
},
{
"name": "BarmaCanvas",
"city": "Rio de Janeiro",
"location": [
-43.1620437,
-22.9443154
],
"id": "ci4symwpg000402wejp5hagiv"
},
{
"name": "GaveaDataCanvas",
"city": "Rio de Janeiro",
"location": [
-43.234095,
-22.980901
],
"id": "ci4wsj9if000c02tcrquen3bl"
},
{
"name": "valeriab",
"city": "Rio de Janeiro",
"location": [
-43.338334499999995,
-22.9431045
],
"id": "ci4yf41wg000b03zz1sg9c2yb"
},
{
"name": "Cavalo",
"city": "Rio de Janeiro",
"location": [
-43.244374,
-22.927468
],
"id": "ci50huz75000003whywxrn4wx"
},
{
"name": "Olabi",
"city": "Rio de Janeiro",
"location": [
-43.187984,
-22.947361
],
"id": "ci4x6nzle000k02tckylmop2g"
},
{
"name": "nico",
"city": "Rio de Janeiro",
"location": [
-43.198756,
-22.956237
],
"id": "ci4q2f9cr000102t9di6kx27u"
},
{
"name": "Urban Launchpad",
"city": "San Francisco",
"location": [
-122.430467,
37.767358
],
"id": "ci4yfbbdb000d03zzoq8kjdl0"
},
{
"name": "GlenParklifeLogger",
"city": "San Francisco",
"location": [
-122.428851,
37.73914
],
"id": "ci4yhy9yy000f03zznho5nm7c"
},
{
"name": "ClimateNinja9000",
"city": "San Francisco",
"location": [
-122.402767,
37.791326
],
"id": "ci4yyrdqi000j03zz8ylornqd"
},
{
"name": "Exploratorium",
"city": "San Francisco",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/Exploratorium.png",
"location": [
-122.396751,
37.802468
],
"id": "ci4vy1tfy000m02s7v29jkkx4"
},
{
"name": "Datavore",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/swissnex.png",
"city": "San Francisco",
"location": [
-122.40326249999998,
37.7962173
],
"id": "ci4lnqzte000002xpokc9d25v"
},
{
"name": "Grand Theater",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/GrandTheater.png",
"city": "San Francisco",
"location": [
-122.418503,
37.754392
],
"id": "ci4usvy81000302s7whpk8qlp"
},
{
"name": "mapsense",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/mapsense.jpeg",
"city": "San Francisco",
"location": [
-122.41102930000001,
37.7874046
],
"id": "ci4usvryz000202s7llxjafaf"
},
{
"name": "GehlData",
"city": "San Francisco",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/GehlData.png",
"location": [
-122.415348,
37.758871
],
"id": "ci4xcxxgc000n02tci92gpvi6"
},
{
"name": "a-streetcar-named-data-sensor",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/stamen.png",
"city": "San Francisco",
"location": [
-122.42,
37.7648
],
"id": "ci4usss1t000102s7hkg0rpqg"
},
{
"name": "AlleyCat",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/localdata.png",
"city": "San Francisco",
"location": [
-122.413649,
37.775641
],
"id": "ci4tmxpz8000002w7au38un50"
},
{
"name": "DataDonut",
"city": "San Francisco",
"location": [
-122.403045,
37.804427
],
"id": "ci4yf50s5000c03zzt4h2tnsq"
},
{
"name": "grapealope",
"city": "San Francisco",
"location": [
-122.423758,
37.747894
],
"id": "ci4ut5zu5000402s7g6nihdn0"
},
{
"name": "james",
"city": "Shanghai",
"location": [
121.50631,
31.282182
],
"id": "ci5c3vgzv000003v4deetujpe"
},
{
"name": "penguin",
"city": "Shanghai",
"location": [
121.441373,
31.028852
],
"id": "ci4vk908n000h02s7pqy6aud3"
},
{
"name": "WGQ",
"city": "Shanghai",
"location": [
121.6289675,
31.2984302
],
"id": "ci4w9izto000302tcgj9hmy9m"
},
{
"name": "frogsh",
"city": "Shanghai",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/frogsh.jpg",
"location": [
121.443609,
31.233924
],
"id": "ci4wmzegn000702tcc6dn993o"
},
{
"name": "fraserxu",
"city": "Shanghai",
"location": [
121.435432,
31.226463
],
"id": "ci4s0caqw000002wey2s695ph"
},
{
"name": "SASPudong",
"city": "Shanghai",
"location": [
121.7588735,
31.2327278
],
"id": "ci4xdbcwd000o02tcoi729rb7"
},
{
"name": "transistor",
"city": "Shanghai",
"location": [
121.493988,
31.210459
],
"id": "ci4xrkkxy000203zz2qz5x2qt"
},
{
"name": "JiadingSensor",
"city": "Shanghai",
"location": [
121.2513265,
31.3262905
],
"id": "ci4z8a9d10001032zk4mh5qow"
},
{
"name": "xinchejian",
"city": "Shanghai",
"location": [
121.448771,
31.225905
],
"id": "ci521nmr600020347n4s107yh"
},
{
"name": "Simon's All Knowing Box",
"city": "Shanghai",
"location": [
121.435574,
31.203442
],
"id": "ci525hppb00030347n3qo265c"
},
{
"name": "TheMarms",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/swissnex.png",
"city": "Shanghai",
"location": [
121.370579,
31.196056
],
"id": "ci527ripa000403471yii8wim"
},
{
"name": "chinotto",
"city": "Shanghai",
"location": [
121.4580891,
31.2353115
],
"id": "ci4yd6rfl000a03zzycxw5inl"
},
{
"name": "killingjackysbox",
"city": "ShenZhen",
"location": [
-122.230081,
37.790237
],
"id": "ci53h7ry60000032pfrgyjdug"
},
{
"name": "SECSensor",
"city": "Singapore",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/SECSensor.png",
"location": [
103.737863,
1.335005
],
"id": "ci4v5vrcu000602s7g2cur4b2"
},
{
"name": "HackerspaceSGSeeed",
"city": "Singapore",
"location": [
103.862409,
1.31042
],
"id": "ci4y9ght2000803zzrj61j1nl"
},
{
"name": "Durianz",
"city": "Singapore",
"location": [
103.751746,
1.374852
],
"id": "ci599v4yf000003z5b4ovspob"
},
{
"name": "sg_hougang_61",
"city": "Singapore",
"location": [
103.88545,
1.375776
],
"id": "ci4v67rh8000902s7vcxxq6wt"
},
{
"name": "The ThunderBolt",
"city": "Singapore",
"location": [
103.912091,
1.328281
],
"id": "ci4y4ohu3000703zzy0fxkd5n"
},
{
"name": "NorthStarOne",
"city": "Singapore",
"location": [
103.798312,
1.438973
],
"id": "ci4vejnju000b02s70yzzonp9"
},
{
"name": "Aislinnn",
"city": "Singapore",
"location": [
103.814735,
1.283259
],
"id": "ci5lssf4x000003x8j2karv47"
},
{
"name": "ghimmohyandao",
"city": "Singapore",
"location": [
103.784951,
1.308855
],
"id": "ci4wyckhv000f02tcyojovuvr"
},
{
"name": "mm@alexandra",
"city": "Singapore",
"location": [
103.803632,
1.288705
],
"id": "ci4wq5cf6000a02tci6u38edj"
},
{
"name": "Gecko",
"city": "Singapore",
"location": [
103.783868,
1.28145
],
"id": "ci52039km00010347y1k3z1u4"
},
{
"name": "swissnexSG",
"avatar": "http://localdata-public.s3-website-us-east-1.amazonaws.com/datacanvas/avatars/swissnex.png",
"city": "Singapore",
"location": [
103.792581,
1.302858
],
"id": "ci4wg4xti000502tccs34dvk4"
},
{
"name": "HPSG730864",
"city": "Singapore",
"location": [
103.794999,
1.440834
],
"id": "ci4w8gd6f000102tcnp415mz5"
},
{
"name": "reversehaven",
"city": "Singapore",
"location": [
103.749155,
1.396564
],
"id": "ci516u6u8000203wht0ml6o9c"
},
{
"name": "rpbaltazar@bukit-timah",
"city": "Singapore",
"location": [
103.812228,
1.326082
],
"id": "ci4v5wwl4000702s7t1u7rz4a"
}
]
#display {
overflow-y: scroll;
}
.control {
width: 100%;
}
.control .label {
width: 100px;
height: 30px;
display: block;
margin: auto 0;
}
.row {
margin: 10px;
}
#url {
width: 100% !important;
margin: 0 !important;
}
#data {
width: 100%;
height: 800px;
margin: 0px;
}
.canvas {
height: 300px;
}
.canvas svg {
width: 100%;
height: 100%;
}
<div class="control">
<div class="row">
<span class="label">API URL:</span>
<input id="url">
</div>
<div class="row">
<span class="label">DATA:</span>
<textarea id="data"></textarea>
</div>
<div class="canvas">
<div class="loader">
Loading
</div>
<svg></svg>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment