Published
Edited
Dec 19, 2018
2 forks
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
timetable = loadTimetable(
'https://raw.githubusercontent.com/alexmasselot/swiss-transport-data/master/data/dt-timetable.tsv'
)
Insert cell
metadata = d3.json(
'https://raw.githubusercontent.com/alexmasselot/swiss-transport-data/master/data/metadata.json'
)
Insert cell
Insert cell
timetableFiltered = _.chain(timetable)
.filter((r) => r.h_departure_time > 7*60 && r.next_h_arrival_time < 10.5*60)
.filter((r) => r.trip_description === selectedDirection)
.filter((r) => r.day_type === selectedDayType)
.sortBy('frequency')
.value()
Insert cell
Insert cell
scales = (timetable, stations) => {
return {
time2x: d3.scaleLinear()
.range([
dimensions.margins.left,
dimensions.overall.width - dimensions.margins.right
])
.domain([
_.chain(timetable).map('h_departure_time').min().value(),
_.chain(timetable).map('next_h_arrival_time').max().value()
]),
distance2y: d3.scaleLinear()
.range([
dimensions.overall.height-dimensions.margins.bottom,
dimensions.margins.top
])
.domain([
0,
_.chain(stations).map('tripDistance').max().value()
])
}
}
Insert cell
Insert cell
uniqueStations = _.chain(timetable)
.map((tt)=>[
{stationName: tt.station_name, tripDistance: tt.trip_distance},
{stationName: tt.next_station_name, tripDistance: tt.next_trip_distance},
])
.flatMap()
.uniqBy('stationName')
.value()
Insert cell
addAxes = (svg, scales, stations) => {
svg.selectAll('g.axis').remove()
// x axis for time
svg.append("g")
.classed('axis', true)
.attr("transform", 'translate(0, ' + (dimensions.overall.height - dimensions.margins.bottom) + ')')
.call(d3.axisBottom(scales.time2x)
.tickFormat((v) => {
// format tim ein minutes into a readable form (8h, 8:05, 8:40)
let m = v%60;
let h = Math.floor(v/60);
if(m === 0){
return h+'h';
}else{
return h+':'+d3.format('02')(m);
}
})
);
//y axis (distance), where labels actually are the city names
svg.append("g")
.classed('axis', true)
.attr("transform", 'translate(' + dimensions.margins.left + ', 0)')
.call(d3.axisLeft(scales.distance2y)
.tickValues(_.map(stations, 'tripDistance'))
.tickFormat((undefined, i) => stations[i].stationName)
)
return 'Adding axis ticks and labels'
}
Insert cell
Insert cell
addAxes(svg, scales(timetableFiltered, uniqueStations), uniqueStations)
Insert cell
{
//just define x & y as shortcut to the scaling function, in order to have shorter syntax below
let x = scales(timetableFiltered, uniqueStations).time2x
let y = scales(timetableFiltered, uniqueStations).distance2y
svg.selectAll('path.segment').remove()
svg.selectAll('path.segment')
.data(timetableFiltered)
.enter()
.append('path')
.classed('segment',true)
.attr('d', (tt) =>
'M' + x(tt.h_departure_time) + ', ' + y(tt.trip_distance) +
' L' + x(tt.next_h_arrival_time) + ', ' + y(tt.next_trip_distance)
)
.style('opacity', (tt) => tt.frequency)
.style('stroke', 'black')
.style('stroke-width', 2)
.exit()
.remove();
return 'Adding segment between stations'
}

Insert cell
{
const color=(delay)=>{
if(delay <= -1){
return '#756bb1'
}
if(delay <= 1){
return '#3182bd'
}
if(delay <= 3){
return '#fec44f'
}
return '#d95f0e'
}
//just define x & y as shortcut to the scaling function, in order to have shorter syntax below
let x = scales(timetableFiltered, uniqueStations).time2x
let y = scales(timetableFiltered, uniqueStations).distance2y
svg.selectAll('circle.station').remove()
svg.selectAll('circle.station')
.data(timetableFiltered)
.enter()
.append('circle')
.classed('station',true)
.attr('cx', (tt) => x(tt.next_h_arrival_time))
.attr('cy', (tt) => y(tt.next_trip_distance))
.attr('r', 6)
.style('opacity', (tt) => tt.frequency)
.style('fill', (tt) => color(tt.q80_next_delta_arrival_min))
return 'Adding circles on stations, colored by the median delay on the 80% quantile'
}
Insert cell
Insert cell
import {d3, moment, _, loadTimetable, newSVG, dimensions} from "@alexmasselot/marey-like-timetable-commons"
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more