Skip to content

Commit 08ba763

Browse files
authored
Added a button that exports the trip table to a CSV file. (#1153)
* Added a button that exports the trip table to a CSV file. * Fixed minor type issue in function
1 parent d30c36a commit 08ba763

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

apps/game/src/sandbox/dashboards/trip_table.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::collections::{BTreeSet, HashMap};
2+
use std::io::Write;
23

34
use abstutil::prettyprint_usize;
45
use geom::{Duration, Polygon, Time};
56
use map_gui::tools::{checkbox_per_mode, color_for_mode};
67
use sim::TripID;
78
use synthpop::{TripEndpoint, TripMode};
89
use widgetry::table::{Col, Filter, Table};
10+
use widgetry::tools::PopupMsg;
911
use widgetry::{
1012
Color, EventCtx, Filler, GeomBatch, GfxCtx, Line, Outcome, Panel, Stash, State, TabController,
1113
Text, Toggle, Widget,
@@ -62,6 +64,11 @@ impl TripTable {
6264
let finished_trips_table = make_table_finished_trips(app);
6365
let finished_trips_content = Widget::col(vec![
6466
finished_trips_table.render(ctx, app),
67+
ctx.style()
68+
.btn_plain
69+
.text("Export to CSV")
70+
.build_def(ctx)
71+
.align_bottom(),
6572
Filler::square_width(ctx, 0.15)
6673
.named("preview")
6774
.centered_horiz(),
@@ -123,6 +130,18 @@ impl State<App> for TripTable {
123130
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
124131
match self.panel.event(ctx) {
125132
Outcome::Clicked(x) => {
133+
if x == "Export to CSV" {
134+
return Transition::Push(match export_trip_table(app) {
135+
Ok(path) => PopupMsg::new_state(
136+
ctx,
137+
"Data exported",
138+
vec![format!("Data exported to {}", path)],
139+
),
140+
Err(err) => {
141+
PopupMsg::new_state(ctx, "Export failed", vec![err.to_string()])
142+
}
143+
});
144+
}
126145
if self.table_tabs.active_tab_idx() == 0 && self.finished_trips_table.clicked(&x) {
127146
self.finished_trips_table
128147
.replace_render(ctx, app, &mut self.panel);
@@ -698,3 +717,35 @@ fn make_table_unfinished_trips(app: &App) -> Table<App, UnfinishedTrip, Filters>
698717

699718
table
700719
}
720+
721+
fn export_trip_table(app: &App) -> anyhow::Result<String> {
722+
let (finished, _) = produce_raw_data(app);
723+
let path = format!(
724+
"trip_table_{}_{}.csv",
725+
app.primary.map.get_name().as_filename(),
726+
app.primary.sim.time().as_filename()
727+
);
728+
729+
let mut out = std::io::Cursor::new(Vec::new());
730+
writeln!(
731+
out,
732+
"id,mode,modified,departure,duration,waiting_time,percent_waiting,duration_before"
733+
)?;
734+
735+
for trip in finished {
736+
writeln!(
737+
out,
738+
"{},{:?},{},{},{},{},{},{}",
739+
trip.id.0,
740+
trip.mode,
741+
trip.modified,
742+
trip.departure,
743+
trip.duration_after.inner_seconds(),
744+
trip.waiting.inner_seconds(),
745+
trip.percent_waiting,
746+
trip.duration_before.inner_seconds()
747+
)?;
748+
}
749+
750+
abstio::write_file(path, String::from_utf8(out.into_inner())?).map_err(anyhow::Error::from)
751+
}

widgetry_demo/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub fn main() {
1414
run(settings);
1515
}
1616

17-
1817
fn run(mut settings: Settings) {
1918
abstutil::logger::setup();
2019
settings = settings.read_svg(Box::new(abstio::slurp_bytes));
@@ -60,7 +59,7 @@ impl Demo {
6059
where
6160
F: Fn(usize) -> usize,
6261
{
63-
Series{
62+
Series {
6463
label: label.to_string(),
6564
color,
6665
pts: (0..(self.elapsed.inner_seconds() as usize))

0 commit comments

Comments
 (0)