Skip to content

Commit

Permalink
Added a button that exports the trip table to a CSV file. (#1153)
Browse files Browse the repository at this point in the history
* Added a button that exports the trip table to a CSV file.

* Fixed minor type issue in function
  • Loading branch information
dJonaitis authored Jan 9, 2025
1 parent d30c36a commit 08ba763
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
51 changes: 51 additions & 0 deletions apps/game/src/sandbox/dashboards/trip_table.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::collections::{BTreeSet, HashMap};
use std::io::Write;

use abstutil::prettyprint_usize;
use geom::{Duration, Polygon, Time};
use map_gui::tools::{checkbox_per_mode, color_for_mode};
use sim::TripID;
use synthpop::{TripEndpoint, TripMode};
use widgetry::table::{Col, Filter, Table};
use widgetry::tools::PopupMsg;
use widgetry::{
Color, EventCtx, Filler, GeomBatch, GfxCtx, Line, Outcome, Panel, Stash, State, TabController,
Text, Toggle, Widget,
Expand Down Expand Up @@ -62,6 +64,11 @@ impl TripTable {
let finished_trips_table = make_table_finished_trips(app);
let finished_trips_content = Widget::col(vec![
finished_trips_table.render(ctx, app),
ctx.style()
.btn_plain
.text("Export to CSV")
.build_def(ctx)
.align_bottom(),
Filler::square_width(ctx, 0.15)
.named("preview")
.centered_horiz(),
Expand Down Expand Up @@ -123,6 +130,18 @@ impl State<App> for TripTable {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
match self.panel.event(ctx) {
Outcome::Clicked(x) => {
if x == "Export to CSV" {
return Transition::Push(match export_trip_table(app) {
Ok(path) => PopupMsg::new_state(
ctx,
"Data exported",
vec![format!("Data exported to {}", path)],
),
Err(err) => {
PopupMsg::new_state(ctx, "Export failed", vec![err.to_string()])
}
});
}
if self.table_tabs.active_tab_idx() == 0 && self.finished_trips_table.clicked(&x) {
self.finished_trips_table
.replace_render(ctx, app, &mut self.panel);
Expand Down Expand Up @@ -698,3 +717,35 @@ fn make_table_unfinished_trips(app: &App) -> Table<App, UnfinishedTrip, Filters>

table
}

fn export_trip_table(app: &App) -> anyhow::Result<String> {
let (finished, _) = produce_raw_data(app);
let path = format!(
"trip_table_{}_{}.csv",
app.primary.map.get_name().as_filename(),
app.primary.sim.time().as_filename()
);

let mut out = std::io::Cursor::new(Vec::new());
writeln!(
out,
"id,mode,modified,departure,duration,waiting_time,percent_waiting,duration_before"
)?;

for trip in finished {
writeln!(
out,
"{},{:?},{},{},{},{},{},{}",
trip.id.0,
trip.mode,
trip.modified,
trip.departure,
trip.duration_after.inner_seconds(),
trip.waiting.inner_seconds(),
trip.percent_waiting,
trip.duration_before.inner_seconds()
)?;
}

abstio::write_file(path, String::from_utf8(out.into_inner())?).map_err(anyhow::Error::from)
}
3 changes: 1 addition & 2 deletions widgetry_demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub fn main() {
run(settings);
}


fn run(mut settings: Settings) {
abstutil::logger::setup();
settings = settings.read_svg(Box::new(abstio::slurp_bytes));
Expand Down Expand Up @@ -60,7 +59,7 @@ impl Demo {
where
F: Fn(usize) -> usize,
{
Series{
Series {
label: label.to_string(),
color,
pts: (0..(self.elapsed.inner_seconds() as usize))
Expand Down

0 comments on commit 08ba763

Please sign in to comment.