Published
Edited
Dec 17, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
timetableSample = loadTimetable(
'https://raw.githubusercontent.com/alexmasselot/swiss-transport-data/master/data/dt-timetable-sample.tsv'
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chartSample = svgSample.node()
Insert cell
Insert cell
{
let x = scales(timetableSample).time2x
let y = scales(timetableSample).distance2y
svgSample.selectAll('circle.stop')
.data(timetableSample)
.enter()
.append('circle')
.classed('stop',true)
.attr('cx', (tt) => x(tt.next_h_arrival_time))
.attr('cy', (tt) => y(tt.next_trip_distance))
.attr('r', 5)
}
Insert cell
Insert cell
Insert cell
addAxes = (svg, scales) => {
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(uniqueStations, 'tripDistance'))
.tickFormat((undefined, i) => uniqueStations[i].stationName)
)
return 'Adding axis ticks and labels'
}
Insert cell
Insert cell
Insert cell
{
//jsut define x & y as shortcut to the scaling function, in order to have shorter syntax below
let x = scales(timetableSample).time2x
let y = scales(timetableSample).distance2y
svgSample.selectAll('path.segment').remove()
svgSample.selectAll('path.segment')
.data(timetableSample)
.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('stroke', 'blue')
return 'Adding segments between stations'
}
Insert cell
Insert cell
timetableFiltered = _.chain(timetable)
.filter((r) => r.h_departure_time > 6*60 && r.next_h_arrival_time < 11*60)
.filter((r) => r.trip_description === selectedDirection)
.filter((r) => r.day_type === selectedDayType)
.sortBy('frequency')
.value()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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