Skip to content

Instantly share code, notes, and snippets.

@hanbzu
Created January 27, 2014 11:18
Show Gist options
  • Save hanbzu/8646864 to your computer and use it in GitHub Desktop.
Save hanbzu/8646864 to your computer and use it in GitHub Desktop.
{
"libraries": [
"d3"
],
"mode": "javascript",
"layout": "fullscreen mode (vertical)",
"resolution": "reset"
}
svg {
font: 10px sans-serif;
}
.axis line {
stroke: #000;
shape-rendering: crispEdges;
}
.trip path {
fill: none;
stroke-width: 1.5px;
}
.trip path { stroke: rgb(192,62,29); }
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
<!-- html goes here -->
<svg></svg>
var margin = {top: 20, right: 30, bottom: 20, left: 100},
width = 600 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom
var x = d3.scale.linear()
.domain([0, 100])
.range([0, width])
var y = d3.scale.linear()
.domain([0, 100])
.range([0, height])
var xAxis = d3.svg.axis().scale(x)
var yAxis = d3.svg.axis().scale(y)
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("y", - margin.top)
.attr("width", width)
.attr("height", height + margin.top + margin.bottom)
d3.json("water.json", function(timetable) {
function findValue(value, where, old) {
function linearInterpolation(from, to, value) {
var x1 = from[0], x2 = to[0], y1 = from[1], y2 = to[1]
var m = (y2 - y1) / (x2 - x1), b = y1 - m * x1
return m * value + b
}
if (where.length == 0) return undefined
var head = where[0]
if (head[0] == value) return { found: head[1], rest: where }
else if (head[0] > value) return { found: linearInterpolation(old, head, value), rest: where }
return findValue(value, where.slice(1), head)
}
function normalise(trip, refTrip, newTrip) {
newTrip = newTrip || []
if (trip.length == 0) return newTrip
var head = trip[0]
var lookup = findValue(head[0], refTrip)
var result = [ head[0], head[1] - lookup.found ] // distance, difference
return normalise(trip.slice(1), lookup.rest, newTrip.concat( [result] ))
}
var ref = [ [0, 0], [20, 5], [35, 25], [50, 30] ]
var trip = [ [0, 0], [20, 10], [35, 27], [50, 30] ]
var normalised = normalise(trip, ref)
console.log("normalised", normalise(trip, ref))
var trips = normalised.map(function(step) {
return { x: step[0], y: step[1] }
})
var trip = svg.append("g")
.attr("class", "trip")
.attr("clip-path", "url(#clip)")
.selectAll("g")
.data(trips)
.enter().append("g")
.attr("class", function(d) { return "line" })
var line = d3.svg.line()
.x(function(d) { return x(d.x) })
.y(function(d) { return y(d.y) })
trip.append("path")
.attr("class", "trip")
.attr("d", function(d) { return line(d) })
trip.selectAll("circle")
.data(function(d) { return d })
.enter().append("circle")
.attr("transform", function(d) {
return "translate(" + x(d.x) + "," + y(d.y) + ")" })
.attr("r", 2);
/*
svg.append("g")
.attr("class", "x top axis")
.call(xAxis.orient("top"))
svg.append("g")
.attr("class", "y left axis")
.call(yAxis.orient("left")) */
})
{
"runTypes": {
"RT1": [ [0, 0], [20, 5], [35, 25], [50, 30] ],
"RT2": [ [50, 0], [65, 5], [80, 25], [100, 30] ],
"RT1X": [ [20, 0], [35, 20], [50, 25] ],
"RT2X": [ [50, 0], [65, 5], [80, 25] ]
},
"services": {
"S1": [ ["01", "10:05", "RT1X"], ["02", "10:30", "RT2"], ["03", "11:00", "RT1"], ["04", "11:30", "RT2X"] ],
"S2": [ ["05", "10:15", "RT1X"], ["06", "10:40", "RT2"], ["07", "11:10", "RT1"], ["08", "11:40", "RT2X"] ],
"S3": [ ["09", "10:25", "RT1X"], ["10", "10:50", "RT2"], ["11", "11:20", "RT1"], ["12", "11:50", "RT2X"] ],
"S4": [ ["13", "10:35", "RT1X"], ["14", "11:00", "RT2"], ["15", "11:30", "RT1"], ["16", "12:00", "RT2X"] ],
"S5": [ ["17", "10:45", "RT1X"], ["18", "11:10", "RT2"], ["19", "11:40", "RT1"], ["20", "12:10", "RT2X"] ],
"S6": [ ["21", "10:55", "RT1X"], ["22", "11:20", "RT2"], ["23", "11:50", "RT1"], ["24", "12:20", "RT2X"] ]
},
"updates": [
{
"trip": [ "S1", "01" ],
"report": [ [20, 2], [35, 25] ]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment