Skip to content

Instantly share code, notes, and snippets.

@ptvans
Created February 12, 2013 18:16
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 ptvans/4771981 to your computer and use it in GitHub Desktop.
Save ptvans/4771981 to your computer and use it in GitHub Desktop.
Weather stations pseudo heatmap (using topoJSON)
{"description":"Weather stations pseudo heatmap (using topoJSON)","endpoint":"","display":"canvas","public":true,"require":[{"name":"TopoJSON","url":"http://d3js.org/topojson.v0.min.js"},{"name":"TopoJSON","url":"http://d3js.org/topojson.v0.min.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"stations.json":{"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},"countries.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"stations_1910.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
var r = 10,
month = Math.floor(10); // 1 = January, 12 = December
var projection = d3.geo.equirectangular()
.scale(143)
var c = tb.ctx;
c.fillStyle = '#5C5C5C';
var path = d3.geo.path()
.projection(projection)
.context(c);
var world = tb['countries'];
var countries = topojson.object(world, world.objects.land);
// clipping path
c.beginPath();
path(countries);
c.closePath();
c.clip();
// background
path(countries);
c.fill();
//var values = _.map(stations.features
var colorScale = d3.scale.linear()
.domain([-2000, 1500, 4000])
.range(['#21C4F7', '#F0F8B3', '#D52323'])
var drawGradient = function(p) {
var coords = d3.geo.centroid(p);
var cs = projection(coords);
var grd = c.createRadialGradient(cs[0], cs[1], 0, cs[0], cs[1], r);
var color = colorScale(p.properties["val" + month]);
var rgbColor = d3.rgb(color);
var colorString = [rgbColor.r, rgbColor.g, rgbColor.b].join();
grd.addColorStop(0, 'rgba(' + colorString +', 0.5)');
grd.addColorStop(1, 'rgba(' + colorString +', 0)');
path.pointRadius(r);
c.fillStyle = grd;
c.beginPath();
path(p)
c.closePath();
c.fill();
};
var drawPoint = function(p) {
path.pointRadius(1);
var color = colorScale(p.properties["val" + month]);
c.fillStyle = color;
c.beginPath();
path(p)
c.closePath();
c.fill();
};
var stations = tb['stations_1910'];
c.save();
var delta = 9500;
var lowerLimit = -4000;
var upperLimit = lowerLimit + delta;
_.chain(stations.features)
.filter(function(d, i) { return d.properties["val" + month] > lowerLimit && d.properties["val" + month] < upperLimit}) // filter out unknown results
.sortBy(function(d, i) { return d.properties["val" + month]} )
.each(drawGradient)
.values();
_.chain(stations.features)
.filter(function(d, i) { return d.properties["val" + month] > lowerLimit && d.properties["val" + month] < upperLimit}) // filter out unknown results
.sortBy(function(d, i) { return d.properties["val" + month]} )
.each(drawPoint)
.values();
c.restore();
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment