Skip to content

Instantly share code, notes, and snippets.

@kobben
Last active January 3, 2016 11:29
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 kobben/8456187 to your computer and use it in GitHub Desktop.
Save kobben/8456187 to your computer and use it in GitHub Desktop.
Small test of D3 API used to map track data from a geojson.
<!DOCTYPE html>
<html>
<head>
<title>Time &amp; Distance mapped of tracks...</title>
<meta charset="utf-8">
<style>
#theMapSVG {
fill : none;
stroke : gray;
stroke-width : .3pt;
}
.timecircle {
fill : red;
stroke : none;
}
.distancecircle {
fill : blue;
stroke : none;
}
.connectors {
fill : none;
stroke : black;
stroke-width : .1pt;
}
text {
fill : gray;
stroke : none;
}
</style>
</head>
<body>
<H2>Time &amp; Distance mapped of tracks...</H2>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var SVGwidth = 750, SVGheight = 750;
var xMargin = 10, yT = 200, yD = 100;
var totaldistance = 70, totaltime = 70, distancescale=10, timescale = 10;
//create SVG placeholder
var svg = d3.select("body").append("svg")
.attr("id", "theMapSVG")
.attr("width", SVGwidth)
.attr("height", SVGheight)
;
//draw baseline + text for distance
svg.append("line")
.attr("id","distanceLine")
.attr("y1", yD)
.attr("y2", yD)
.attr("x1", xMargin)
.attr("x2", xMargin + (totaldistance * distancescale))
;
svg.append("text")
.attr("x", xMargin+10)
.attr("y", yT+20)
.text("TIME")
;
//draw baseline + text for time
svg.append("line")
.attr("id","timeLine")
.attr("y1", yT)
.attr("y2", yT)
.attr("x1", xMargin)
.attr("x2", xMargin + (totaltime * timescale))
;
svg.append("text")
.attr("x", xMargin+10)
.attr("y", yD+20)
.text("DISTANCE")
;
//read trackdata (asynchronously)
d3.json("trackdata.geojson", function(json) { //when read, do function
track=json;
//create distance events
svg.selectAll(".distancecircle")
.data(track.features)
.enter().append("circle")
.attr("class", "distancecircle")
.attr("cx", function(d) {
return xMargin + (d.properties.dist * distancescale); })
.attr("cy", yD)
.attr("r", "5")
//create time events
svg.selectAll(".timecircle")
.data(track.features)
.enter().append("circle")
.attr("class", "timecircle")
.attr("cx", function(d) {
return xMargin + (d.properties.timestamp * timescale); })
.attr("cy", yT)
.attr("r", "5")
//create connectors
svg.selectAll(".connectors")
.data(track.features)
.enter().append("line")
.attr("class", "connectors")
.attr("x1", function(d) {
return xMargin + (d.properties.timestamp * timescale); })
.attr("y1", yT)
.attr("x2", function(d) {
return xMargin + (d.properties.dist * distancescale); })
.attr("y2", yD)
});
//set SVG placeholder height (and thus location in HTML)
d3.select(self.frameElement).style("height", SVGheight + "px");
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 8, "dist": 60, "timestamp": 70 }, "geometry": { "type": "Point", "coordinates": [ 6.953566471640581, 52.186217896449541 ] } },
{ "type": "Feature", "properties": { "id": 7, "dist": 31, "timestamp": 60 }, "geometry": { "type": "Point", "coordinates": [ 6.952695969115893, 52.191113767910039 ] } },
{ "type": "Feature", "properties": { "id": 5, "dist": 25, "timestamp": 50 }, "geometry": { "type": "Point", "coordinates": [ 6.950462940900388, 52.190951354279946 ] } },
{ "type": "Feature", "properties": { "id": 4, "dist": 23, "timestamp": 40 }, "geometry": { "type": "Point", "coordinates": [ 6.950462940900388, 52.190928152284357 ] } },
{ "type": "Feature", "properties": { "id": 3, "dist": 21, "timestamp": 30 }, "geometry": { "type": "Point", "coordinates": [ 6.950349397092819, 52.190858546224938 ] } },
{ "type": "Feature", "properties": { "id": 2, "dist": 20, "timestamp": 20 }, "geometry": { "type": "Point", "coordinates": [ 6.950084461541828, 52.190858546224938 ] } },
{ "type": "Feature", "properties": { "id": 1, "dist": 10, "timestamp": 10 }, "geometry": { "type": "Point", "coordinates": [ 6.949365350760564, 52.190696131662463 ] } },
{ "type": "Feature", "properties": { "id": 0, "dist": 0, "timestamp": 0 }, "geometry": { "type": "Point", "coordinates": [ 6.948835479658579, 52.190533716506664 ] } }
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment