pub struct Chart<'a> { /* private fields */ }Expand description
A chart: series, and how they map onto the axes.
Implementations§
Source§impl<'a> Chart<'a>
impl<'a> Chart<'a>
Sourcepub const fn of(series: &'a [Series<'a>]) -> Self
pub const fn of(series: &'a [Series<'a>]) -> Self
A chart of these series. The y axis spans their values (from zero for bars and areas), widened to round numbers.
Examples found in repository?
More examples
109pub fn describe(report: &mut Report, ui: &mut Ui) {
110 let t = ui.theme;
111 ui.column().pad(16.0).gap(12.0).grow().open();
112 ui.text("The year in sales", t.font.bold(), t.text);
113 let rows = [Series::bars("Sold", &SOLD), Series::line("Came back", &BACK).color(t.red)];
114 ui.chart(&Chart::of(&rows).categories(&MONTHS), 560.0, 260.0);
115 ui.row().gap(8.0).align(Align::Start, Align::Center).open();
116 if ui.primary_button("Save as PDF").clicked {
117 let file = std::env::temp_dir().join("report.pdf");
118 report.status = match std::fs::write(&file, paper().to_pdf()) {
119 Ok(()) => format!("Saved as {}", file.display()),
120 Err(error) => format!("Not saved: {error}"),
121 };
122 report.saved = Some(file.display().to_string());
123 }
124 if ui.button("Print").clicked && report.printing.is_none() {
125 report.printing = Some(taut::print(paper(), "The year in sales"));
126 }
127 if let Some(done) = report.printing.as_ref().and_then(PrintJob::outcome) {
128 report.printing = None;
129 report.status = match done {
130 PrintOutcome::Sent => "Sent to the printer".into(),
131 PrintOutcome::Closed => String::new(),
132 _ => "There is no printing here".into(),
133 };
134 }
135 ui.el().w(Length::Grow(1.0)).text(&report.status, t.font, t.text_dim).clip().show();
136 ui.close();
137 ui.close();
138}108fn numbers(shown: &[Second], last: Second, ui: &mut Ui) {
109 let t = ui.theme;
110 let minute = &shown[shown.len().saturating_sub(60)..];
111 let mean = |of: fn(&Second) -> f32| minute.iter().map(of).sum::<f32>() / minute.len() as f32;
112 // The numbers that matter most, each as large as can be read at a glance.
113 ui.wrapping_row().gap(12.0).open();
114 tile(ui, "Requests a second", &format!("{}", last.requests), &format!("{:.0} on average this minute", mean(|second| second.requests)));
115 tile(ui, "Answer time", &format!("{} ms", last.answer), &format!("{:.0} ms on average", mean(|second| second.answer)));
116 tile(ui, "Failed this minute", &format!("{:.0}", minute.iter().map(|second| second.failed).sum::<f32>()), "requests");
117 ui.close();
118
119 // How they went. A chart for each, so each has its own axis: three side by side where
120 // the row has room for them (the room it has in this run, as layout gives it: beside
121 // the strip a bar runs in, where there is one), else fewer.
122 let ends = (-(shown.len() as f32 - 1.0), 0.0);
123 let [requests, answers, failed] = [|second: &Second| second.requests, |second: &Second| second.answer, |second: &Second| second.failed].map(|of| shown.iter().map(of).collect::<Vec<f32>>());
124 ui.wrapping_row().gap(12.0).open();
125 let wide = ((ui.available_size().w - 24.0) / 3.0).floor().max(240.0);
126 chart(ui, "Requests a second", &Chart::of(&[Series::area("", &requests)]).x_range(ends.0, ends.1), wide);
127 chart(ui, "Answer time, ms", &Chart::of(&[Series::line("", &answers)]).x_range(ends.0, ends.1), wide);
128 let most = failed.iter().fold(5.0f32, |most, failed| most.max(*failed));
129 chart(ui, "Failed requests", &Chart::of(&[Series::bars("", &failed).color(t.red)]).y_range(0.0, most), wide);
130 ui.close();
131}789fn charts(app: &mut App, ui: &mut Ui) {
790 let kinds = KINDS.map(|kind| app.files.iter().filter(|file| file.2 == kind).count() as f32);
791 ui.column().grow().scroll().pad(14.0).gap(14.0).open();
792 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
793 ui.chart_view(&mut app.slices, &Chart::of(&[Series::pie("Files", &kinds)]).categories(&KINDS), 300.0, 160.0);
794 ui.chart(&Chart::of(&[Series::ring("Files", &kinds)]).categories(&KINDS), 230.0, 160.0);
795 ui.close();
796 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
797 let (read, written) = ([3.0, 5.0, 2.0, 6.0, 4.0, 1.0, 2.0], [1.0, 2.0, 4.0, 1.0, 3.0, 2.0, 0.5]);
798 ui.chart(&Chart::of(&[Series::bars("Read", &read), Series::bars("Written", &written)]).categories(&DAYS).stacked(), 300.0, 190.0);
799 dim_text(ui, "The wheel zooms the scatter, dragging moves it, two clicks show all of it again; its names switch its rows off and on.");
800 ui.close();
801 let [small_at, small, large_at, large] = &app.dots;
802 // (Each dot's height goes with where it stands across, and a little by chance.)
803 let small: Vec<f32> = small_at.iter().zip(small).map(|(at, chance)| at * 0.5 + chance / 5.0).collect();
804 let large: Vec<f32> = large_at.iter().zip(large).map(|(at, chance)| 30.0 + at * 0.6 + chance / 4.0).collect();
805 let dots = [Series::points("Small", &small).at(small_at), Series::points("Large", &large).at(large_at)];
806 ui.chart_view(&mut app.view, &Chart::of(&dots), 560.0, 220.0);
807 // The last minute, a value every tenth of a second: only what changed is drawn again.
808 ui.toggle(&mut app.live, "Live");
809 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |since| since.as_secs_f64());
810 if app.live && now - app.fed >= 0.1 {
811 let next = app.load.last().copied().unwrap_or(0.5) + ((now * 7.3).sin() * 0.08) as f32;
812 app.load.remove(0);
813 app.load.push(next.clamp(0.0, 1.0));
814 app.fed = now;
815 }
816 if app.live {
817 ui.wake_after(std::time::Duration::from_millis(100));
818 }
819 ui.chart(&Chart::of(&[Series::area("Load", &app.load)]).x_range(-59.9, 0.0).y_range(0.0, 1.0).time(app.fed), 560.0, 160.0);
820 ui.close();
821}
822
823/// What is under an item of the tree of files (`None`: what is at the top): a thousand
824/// folders; in each, two folders and 98 files; in each of those, five files. Each item's
825/// number says where it is.
826fn folders_under(under: Option<usize>) -> Vec<TreeItem> {
827 match under {
828 None => (0..1000).map(|n| TreeItem::with_children(n, format!("Folder {n:03}"))).collect(),
829 Some(top) if top < 1000 => {
830 let inner = (0..2).map(|n| TreeItem::with_children(1000 + top * 2 + n, format!("Inner {n}")));
831 inner.chain((0..98).map(|n| TreeItem::new(10_000 + top * 100 + n, format!("file {n:02}.txt")))).collect()
832 }
833 Some(inner) if inner < 10_000 => (0..5).map(|n| TreeItem::new(1_000_000 + inner * 5 + n, format!("deep {n}.txt"))).collect(),
834 Some(_) => Vec::new(),
835 }
836}
837
838/// The parts every web page and phone app has.
839fn web_widgets(app: &mut App, ui: &mut Ui) {
840 ui.column().grow().pad(14.0).scroll().open();
841 ui.column().max(640.0, f32::INFINITY).gap(14.0).open();
842 navigation(app, ui);
843 tags_and_people(app, ui);
844 cards_and_groups(app, ui);
845 ui.close();
846 ui.close();
847}
848
849/// A banner, a path of places, the steps of a task with the app's own buttons, pages of
850/// results, a box to search with and a button with a menu of other ways.
851fn navigation(app: &mut App, ui: &mut Ui) {
852 if app.banner {
853 app.banner = !ui.banner(Severity::Info, "The parts every web page and phone app has. This strip goes with its cross.").clicked;
854 ui.close();
855 }
856 if let Some(place) = ui.breadcrumbs(&["Home", "Pictures", "Holidays", "2026", "Mountains"]) {
857 ui.notice(&format!("Gone back to place {}", place + 1));
858 }
859 ui.steps(&mut app.step, &["Account", "Address", "Payment", "Done"]);
860 // Back and on through the steps are the app's own buttons.
861 ui.row().gap(8.0).open();
862 ui.set_enabled(app.step > 0);
863 app.step -= ui.button("Back").clicked as usize;
864 ui.set_enabled(app.step < 3);
865 app.step += ui.primary_button("Next").clicked as usize;
866 ui.set_enabled(true);
867 ui.close();
868 ui.pagination(&mut app.results, 24);
869 ui.row().gap(8.0).open();
870 ui.search(&mut app.query, "Search the pictures");
871 let save = ui.split_button("Save");
872 if save.clicked {
873 ui.notice("Saved");
874 }
875 if ui.menu(save.id) {
876 if ui.menu_item("Save as\u{2026}").clicked {
877 ui.notice("Saved under another name");
878 }
879 if ui.menu_item("Save a copy").clicked {
880 ui.notice("A copy was saved");
881 }
882 ui.close();
883 }
884 ui.close();
885}
886
887/// Tags, what shows where there are none yet, stars, and people with a count over an icon.
888fn tags_and_people(app: &mut App, ui: &mut Ui) {
889 let t = ui.theme;
890 ui.tag_input(&mut app.topics, &mut app.topic, "Topics: Enter or a comma after each");
891 // What shows where there is nothing yet: a picture, a sentence and a way on.
892 if app.topics.is_empty() {
893 ui.column().pad(16.0).gap(8.0).align(Align::Start, Align::Center).open();
894 ui.el().icon(STAR, 32.0, t.text_faint).show();
895 ui.label("No topics yet");
896 if ui.button("Add a few").clicked {
897 app.topics = vec!["mountains".into(), "lakes".into()];
898 }
899 ui.close();
900 }
901 ui.row().gap(10.0).open();
902 ui.rating(&mut app.stars, 5, 0.5);
903 ui.note(&format!("{} of 5", app.stars));
904 ui.close();
905 // People: a picture of one, the first letters of the others; and a count over an icon.
906 let face = face(app, ui);
907 ui.row().gap(8.0).open();
908 ui.avatar("Ada Lovelace", Some(face), 36.0);
909 for name in ["Grace Hopper", "Alan Turing", "Katherine Johnson"] {
910 ui.avatar(name, None, 36.0);
911 }
912 ui.el().w(Length::Px(8.0)).show();
913 ui.el().flow(Flow::Stack).open();
914 ui.icon_button(BELL, "Notices, 3 new");
915 let badge = ui.el().place((Align::End, Align::Start)).offset(6.0, -6.0).min(16.0, 16.0).pad((4.0, 0.0)).radius(8.0).fill(t.red);
916 badge.text("3", t.small_font, t.on_accent).text_align(Align::Center).show();
917 ui.close();
918 ui.close();
919}
920
921/// Cards, what stands in while something loads, buttons that stay down in one frame,
922/// sections of which one is open at a time, and keys written as keys.
923fn cards_and_groups(app: &mut App, ui: &mut Ui) {
924 let t = ui.theme;
925 // Cards side by side, each pressed as one; their own buttons are pressed by themselves.
926 ui.row().gap(10.0).align(Align::Start, Align::Start).open();
927 for (number, (name, words)) in [("Mountains", "A walk of four hours, with a lake at the top."), ("The sea", "Two days along the coast, from bay to bay.")].into_iter().enumerate() {
928 let card = ui.card();
929 ui.text(name, t.font.bold(), t.text);
930 dim_text(ui, words);
931 ui.toggle(&mut app.liked[number], "Liked");
932 ui.close();
933 if card.clicked {
934 ui.notice(&format!("Opened \u{201c}{name}\u{201d}"));
935 }
936 }
937 ui.close();
938 ui.switch(&mut app.loading, "Show what stands in while something loads");
939 if app.loading {
940 ui.row().gap(10.0).open();
941 ui.skeleton().size(40.0, 40.0).radius(20.0).show();
942 ui.column().w(Length::Grow(1.0)).gap(8.0).open();
943 ui.skeleton().show();
944 ui.skeleton().w(Length::Px(160.0)).show();
945 ui.close();
946 ui.close();
947 }
948 // A row of buttons that stay down, in one frame: a group to a screen reader.
949 let styles = ui.row().place((Align::Start, Align::Center)).pad(2.0).gap(2.0).fill(t.panel).border(t.line, 1.0).radius(t.radius).open();
950 ui.tell(&styles, Role::Group, None, "Text style");
951 for (on, name) in app.styles.iter_mut().zip(["Bold", "Italic", "Underline"]) {
952 ui.toggle(on, name);
953 }
954 ui.close();
955 // Sections of which one is open at a time.
956 for (number, (title, words)) in [("What it is", "A page of the parts people know."), ("How it goes", "Each part is one call.")].into_iter().enumerate() {
957 let mut open = app.section == Some(number);
958 if ui.fold(&mut open, title) {
959 dim_text(ui, words);
960 }
961 app.section = if open { Some(number) } else { app.section.filter(|was| *was != number) };
962 }
963 ui.row().gap(8.0).open();
964 ui.label("Find");
965 ui.shortcut("Ctrl+F");
966 ui.el().w(Length::Px(12.0)).show();
967 ui.label("Commands");
968 ui.shortcut("Ctrl+K");
969 ui.close();
970 if !app.banner && ui.link("Show the banner again").clicked {
971 app.banner = true;
972 }
973}
974
975/// A quiet line that breaks where the room for it ends.
976fn dim_text(ui: &mut Ui, text: &str) {
977 let t = ui.theme;
978 ui.el().w(Length::Grow(1.0)).text(text, t.small_font, t.text_dim).wrap().show();
979}
980
981/// The page of more parts: three columns, the parts other libraries name, and a code editor.
982fn more_parts(app: &mut App, ui: &mut Ui) {
983 // Sixteen values to a wave, so one falls on each peak and trough and a smooth line
984 // through them is round there, not flattened between two values either side.
985 let wave: Vec<f32> = (0..48).map(|i| (i as f32 * std::f32::consts::PI / 8.0).sin() * app.amount).collect();
986 ui.column().grow().scroll().open();
987 ui.row().pad(14.0).gap(28.0).align(Align::Start, Align::Start).open();
988 rows_tree_and_chart(app, ui, &wave);
989 numbers_and_plots(app, ui, &wave);
990 form_parts(app, ui);
991 ui.close();
992 parts_by_other_names(app, ui);
993 code_and_inspector(app, ui);
994 ui.close();
995}
996
997/// Rows to pick, each with a menu; a tree; and the wave in a chart.
998fn rows_tree_and_chart(app: &mut App, ui: &mut Ui, wave: &[f32]) {
999 ui.column().w(Length::Px(240.0)).gap(2.0).open();
1000 dim_text(ui, "Rows to pick. Right-click one for a menu.");
1001 for (number, name) in ["Inbox", "Drafts", "Sent", "Archive"].iter().enumerate() {
1002 let row = ui.selectable(app.selected_row == number, name);
1003 if row.clicked {
1004 app.selected_row = number;
1005 }
1006 if ui.context_menu(&row) {
1007 if ui.menu_item("Pick this one").clicked {
1008 app.selected_row = number;
1009 }
1010 ui.menu_item("Leave it");
1011 ui.close();
1012 }
1013 }
1014 ui.tree(&mut app.tree_open[0], "A tree");
1015 if app.tree_open[0] {
1016 ui.label("A leaf");
1017 ui.tree(&mut app.tree_open[1], "A branch");
1018 if app.tree_open[1] {
1019 ui.label("A leaf, a step further in");
1020 ui.close();
1021 }
1022 ui.close();
1023 }
1024 // The wave in a chart, and half of it, both drawn as smooth curves.
1025 let half: Vec<f32> = wave.iter().map(|value| value / 2.0).collect();
1026 ui.el().h(Length::Px(10.0)).show();
1027 ui.chart(&Chart::of(&[Series::area("Wave", wave).smooth(), Series::line("Half", &half).smooth()]), 240.0, 150.0);
1028 ui.close();
1029}Sourcepub const fn categories(self, names: &'a [&'a str]) -> Self
pub const fn categories(self, names: &'a [&'a str]) -> Self
Category names along the x axis instead of numbers: each series’ first value is over the first name, and so on. (For a pie, its slice names.)
Examples found in repository?
More examples
109pub fn describe(report: &mut Report, ui: &mut Ui) {
110 let t = ui.theme;
111 ui.column().pad(16.0).gap(12.0).grow().open();
112 ui.text("The year in sales", t.font.bold(), t.text);
113 let rows = [Series::bars("Sold", &SOLD), Series::line("Came back", &BACK).color(t.red)];
114 ui.chart(&Chart::of(&rows).categories(&MONTHS), 560.0, 260.0);
115 ui.row().gap(8.0).align(Align::Start, Align::Center).open();
116 if ui.primary_button("Save as PDF").clicked {
117 let file = std::env::temp_dir().join("report.pdf");
118 report.status = match std::fs::write(&file, paper().to_pdf()) {
119 Ok(()) => format!("Saved as {}", file.display()),
120 Err(error) => format!("Not saved: {error}"),
121 };
122 report.saved = Some(file.display().to_string());
123 }
124 if ui.button("Print").clicked && report.printing.is_none() {
125 report.printing = Some(taut::print(paper(), "The year in sales"));
126 }
127 if let Some(done) = report.printing.as_ref().and_then(PrintJob::outcome) {
128 report.printing = None;
129 report.status = match done {
130 PrintOutcome::Sent => "Sent to the printer".into(),
131 PrintOutcome::Closed => String::new(),
132 _ => "There is no printing here".into(),
133 };
134 }
135 ui.el().w(Length::Grow(1.0)).text(&report.status, t.font, t.text_dim).clip().show();
136 ui.close();
137 ui.close();
138}789fn charts(app: &mut App, ui: &mut Ui) {
790 let kinds = KINDS.map(|kind| app.files.iter().filter(|file| file.2 == kind).count() as f32);
791 ui.column().grow().scroll().pad(14.0).gap(14.0).open();
792 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
793 ui.chart_view(&mut app.slices, &Chart::of(&[Series::pie("Files", &kinds)]).categories(&KINDS), 300.0, 160.0);
794 ui.chart(&Chart::of(&[Series::ring("Files", &kinds)]).categories(&KINDS), 230.0, 160.0);
795 ui.close();
796 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
797 let (read, written) = ([3.0, 5.0, 2.0, 6.0, 4.0, 1.0, 2.0], [1.0, 2.0, 4.0, 1.0, 3.0, 2.0, 0.5]);
798 ui.chart(&Chart::of(&[Series::bars("Read", &read), Series::bars("Written", &written)]).categories(&DAYS).stacked(), 300.0, 190.0);
799 dim_text(ui, "The wheel zooms the scatter, dragging moves it, two clicks show all of it again; its names switch its rows off and on.");
800 ui.close();
801 let [small_at, small, large_at, large] = &app.dots;
802 // (Each dot's height goes with where it stands across, and a little by chance.)
803 let small: Vec<f32> = small_at.iter().zip(small).map(|(at, chance)| at * 0.5 + chance / 5.0).collect();
804 let large: Vec<f32> = large_at.iter().zip(large).map(|(at, chance)| 30.0 + at * 0.6 + chance / 4.0).collect();
805 let dots = [Series::points("Small", &small).at(small_at), Series::points("Large", &large).at(large_at)];
806 ui.chart_view(&mut app.view, &Chart::of(&dots), 560.0, 220.0);
807 // The last minute, a value every tenth of a second: only what changed is drawn again.
808 ui.toggle(&mut app.live, "Live");
809 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |since| since.as_secs_f64());
810 if app.live && now - app.fed >= 0.1 {
811 let next = app.load.last().copied().unwrap_or(0.5) + ((now * 7.3).sin() * 0.08) as f32;
812 app.load.remove(0);
813 app.load.push(next.clamp(0.0, 1.0));
814 app.fed = now;
815 }
816 if app.live {
817 ui.wake_after(std::time::Duration::from_millis(100));
818 }
819 ui.chart(&Chart::of(&[Series::area("Load", &app.load)]).x_range(-59.9, 0.0).y_range(0.0, 1.0).time(app.fed), 560.0, 160.0);
820 ui.close();
821}Sourcepub const fn x_range(self, first: f32, last: f32) -> Self
pub const fn x_range(self, first: f32, last: f32) -> Self
The x values of the first and last value (default 0 and count minus one): sixty values
for the last minute go x_range(-59.0, 0.0).
Examples found in repository?
108fn numbers(shown: &[Second], last: Second, ui: &mut Ui) {
109 let t = ui.theme;
110 let minute = &shown[shown.len().saturating_sub(60)..];
111 let mean = |of: fn(&Second) -> f32| minute.iter().map(of).sum::<f32>() / minute.len() as f32;
112 // The numbers that matter most, each as large as can be read at a glance.
113 ui.wrapping_row().gap(12.0).open();
114 tile(ui, "Requests a second", &format!("{}", last.requests), &format!("{:.0} on average this minute", mean(|second| second.requests)));
115 tile(ui, "Answer time", &format!("{} ms", last.answer), &format!("{:.0} ms on average", mean(|second| second.answer)));
116 tile(ui, "Failed this minute", &format!("{:.0}", minute.iter().map(|second| second.failed).sum::<f32>()), "requests");
117 ui.close();
118
119 // How they went. A chart for each, so each has its own axis: three side by side where
120 // the row has room for them (the room it has in this run, as layout gives it: beside
121 // the strip a bar runs in, where there is one), else fewer.
122 let ends = (-(shown.len() as f32 - 1.0), 0.0);
123 let [requests, answers, failed] = [|second: &Second| second.requests, |second: &Second| second.answer, |second: &Second| second.failed].map(|of| shown.iter().map(of).collect::<Vec<f32>>());
124 ui.wrapping_row().gap(12.0).open();
125 let wide = ((ui.available_size().w - 24.0) / 3.0).floor().max(240.0);
126 chart(ui, "Requests a second", &Chart::of(&[Series::area("", &requests)]).x_range(ends.0, ends.1), wide);
127 chart(ui, "Answer time, ms", &Chart::of(&[Series::line("", &answers)]).x_range(ends.0, ends.1), wide);
128 let most = failed.iter().fold(5.0f32, |most, failed| most.max(*failed));
129 chart(ui, "Failed requests", &Chart::of(&[Series::bars("", &failed).color(t.red)]).y_range(0.0, most), wide);
130 ui.close();
131}More examples
789fn charts(app: &mut App, ui: &mut Ui) {
790 let kinds = KINDS.map(|kind| app.files.iter().filter(|file| file.2 == kind).count() as f32);
791 ui.column().grow().scroll().pad(14.0).gap(14.0).open();
792 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
793 ui.chart_view(&mut app.slices, &Chart::of(&[Series::pie("Files", &kinds)]).categories(&KINDS), 300.0, 160.0);
794 ui.chart(&Chart::of(&[Series::ring("Files", &kinds)]).categories(&KINDS), 230.0, 160.0);
795 ui.close();
796 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
797 let (read, written) = ([3.0, 5.0, 2.0, 6.0, 4.0, 1.0, 2.0], [1.0, 2.0, 4.0, 1.0, 3.0, 2.0, 0.5]);
798 ui.chart(&Chart::of(&[Series::bars("Read", &read), Series::bars("Written", &written)]).categories(&DAYS).stacked(), 300.0, 190.0);
799 dim_text(ui, "The wheel zooms the scatter, dragging moves it, two clicks show all of it again; its names switch its rows off and on.");
800 ui.close();
801 let [small_at, small, large_at, large] = &app.dots;
802 // (Each dot's height goes with where it stands across, and a little by chance.)
803 let small: Vec<f32> = small_at.iter().zip(small).map(|(at, chance)| at * 0.5 + chance / 5.0).collect();
804 let large: Vec<f32> = large_at.iter().zip(large).map(|(at, chance)| 30.0 + at * 0.6 + chance / 4.0).collect();
805 let dots = [Series::points("Small", &small).at(small_at), Series::points("Large", &large).at(large_at)];
806 ui.chart_view(&mut app.view, &Chart::of(&dots), 560.0, 220.0);
807 // The last minute, a value every tenth of a second: only what changed is drawn again.
808 ui.toggle(&mut app.live, "Live");
809 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |since| since.as_secs_f64());
810 if app.live && now - app.fed >= 0.1 {
811 let next = app.load.last().copied().unwrap_or(0.5) + ((now * 7.3).sin() * 0.08) as f32;
812 app.load.remove(0);
813 app.load.push(next.clamp(0.0, 1.0));
814 app.fed = now;
815 }
816 if app.live {
817 ui.wake_after(std::time::Duration::from_millis(100));
818 }
819 ui.chart(&Chart::of(&[Series::area("Load", &app.load)]).x_range(-59.9, 0.0).y_range(0.0, 1.0).time(app.fed), 560.0, 160.0);
820 ui.close();
821}Sourcepub const fn y_range(self, least: f32, most: f32) -> Self
pub const fn y_range(self, least: f32, most: f32) -> Self
Fixed y axis bounds instead of the data’s: a percentage always shown 0 to 100.
Examples found in repository?
108fn numbers(shown: &[Second], last: Second, ui: &mut Ui) {
109 let t = ui.theme;
110 let minute = &shown[shown.len().saturating_sub(60)..];
111 let mean = |of: fn(&Second) -> f32| minute.iter().map(of).sum::<f32>() / minute.len() as f32;
112 // The numbers that matter most, each as large as can be read at a glance.
113 ui.wrapping_row().gap(12.0).open();
114 tile(ui, "Requests a second", &format!("{}", last.requests), &format!("{:.0} on average this minute", mean(|second| second.requests)));
115 tile(ui, "Answer time", &format!("{} ms", last.answer), &format!("{:.0} ms on average", mean(|second| second.answer)));
116 tile(ui, "Failed this minute", &format!("{:.0}", minute.iter().map(|second| second.failed).sum::<f32>()), "requests");
117 ui.close();
118
119 // How they went. A chart for each, so each has its own axis: three side by side where
120 // the row has room for them (the room it has in this run, as layout gives it: beside
121 // the strip a bar runs in, where there is one), else fewer.
122 let ends = (-(shown.len() as f32 - 1.0), 0.0);
123 let [requests, answers, failed] = [|second: &Second| second.requests, |second: &Second| second.answer, |second: &Second| second.failed].map(|of| shown.iter().map(of).collect::<Vec<f32>>());
124 ui.wrapping_row().gap(12.0).open();
125 let wide = ((ui.available_size().w - 24.0) / 3.0).floor().max(240.0);
126 chart(ui, "Requests a second", &Chart::of(&[Series::area("", &requests)]).x_range(ends.0, ends.1), wide);
127 chart(ui, "Answer time, ms", &Chart::of(&[Series::line("", &answers)]).x_range(ends.0, ends.1), wide);
128 let most = failed.iter().fold(5.0f32, |most, failed| most.max(*failed));
129 chart(ui, "Failed requests", &Chart::of(&[Series::bars("", &failed).color(t.red)]).y_range(0.0, most), wide);
130 ui.close();
131}More examples
789fn charts(app: &mut App, ui: &mut Ui) {
790 let kinds = KINDS.map(|kind| app.files.iter().filter(|file| file.2 == kind).count() as f32);
791 ui.column().grow().scroll().pad(14.0).gap(14.0).open();
792 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
793 ui.chart_view(&mut app.slices, &Chart::of(&[Series::pie("Files", &kinds)]).categories(&KINDS), 300.0, 160.0);
794 ui.chart(&Chart::of(&[Series::ring("Files", &kinds)]).categories(&KINDS), 230.0, 160.0);
795 ui.close();
796 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
797 let (read, written) = ([3.0, 5.0, 2.0, 6.0, 4.0, 1.0, 2.0], [1.0, 2.0, 4.0, 1.0, 3.0, 2.0, 0.5]);
798 ui.chart(&Chart::of(&[Series::bars("Read", &read), Series::bars("Written", &written)]).categories(&DAYS).stacked(), 300.0, 190.0);
799 dim_text(ui, "The wheel zooms the scatter, dragging moves it, two clicks show all of it again; its names switch its rows off and on.");
800 ui.close();
801 let [small_at, small, large_at, large] = &app.dots;
802 // (Each dot's height goes with where it stands across, and a little by chance.)
803 let small: Vec<f32> = small_at.iter().zip(small).map(|(at, chance)| at * 0.5 + chance / 5.0).collect();
804 let large: Vec<f32> = large_at.iter().zip(large).map(|(at, chance)| 30.0 + at * 0.6 + chance / 4.0).collect();
805 let dots = [Series::points("Small", &small).at(small_at), Series::points("Large", &large).at(large_at)];
806 ui.chart_view(&mut app.view, &Chart::of(&dots), 560.0, 220.0);
807 // The last minute, a value every tenth of a second: only what changed is drawn again.
808 ui.toggle(&mut app.live, "Live");
809 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |since| since.as_secs_f64());
810 if app.live && now - app.fed >= 0.1 {
811 let next = app.load.last().copied().unwrap_or(0.5) + ((now * 7.3).sin() * 0.08) as f32;
812 app.load.remove(0);
813 app.load.push(next.clamp(0.0, 1.0));
814 app.fed = now;
815 }
816 if app.live {
817 ui.wake_after(std::time::Duration::from_millis(100));
818 }
819 ui.chart(&Chart::of(&[Series::area("Load", &app.load)]).x_range(-59.9, 0.0).y_range(0.0, 1.0).time(app.fed), 560.0, 160.0);
820 ui.close();
821}Sourcepub const fn stacked(self) -> Self
pub const fn stacked(self) -> Self
Stacks bar series instead of grouping them: each total is as high as their sum.
Examples found in repository?
789fn charts(app: &mut App, ui: &mut Ui) {
790 let kinds = KINDS.map(|kind| app.files.iter().filter(|file| file.2 == kind).count() as f32);
791 ui.column().grow().scroll().pad(14.0).gap(14.0).open();
792 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
793 ui.chart_view(&mut app.slices, &Chart::of(&[Series::pie("Files", &kinds)]).categories(&KINDS), 300.0, 160.0);
794 ui.chart(&Chart::of(&[Series::ring("Files", &kinds)]).categories(&KINDS), 230.0, 160.0);
795 ui.close();
796 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
797 let (read, written) = ([3.0, 5.0, 2.0, 6.0, 4.0, 1.0, 2.0], [1.0, 2.0, 4.0, 1.0, 3.0, 2.0, 0.5]);
798 ui.chart(&Chart::of(&[Series::bars("Read", &read), Series::bars("Written", &written)]).categories(&DAYS).stacked(), 300.0, 190.0);
799 dim_text(ui, "The wheel zooms the scatter, dragging moves it, two clicks show all of it again; its names switch its rows off and on.");
800 ui.close();
801 let [small_at, small, large_at, large] = &app.dots;
802 // (Each dot's height goes with where it stands across, and a little by chance.)
803 let small: Vec<f32> = small_at.iter().zip(small).map(|(at, chance)| at * 0.5 + chance / 5.0).collect();
804 let large: Vec<f32> = large_at.iter().zip(large).map(|(at, chance)| 30.0 + at * 0.6 + chance / 4.0).collect();
805 let dots = [Series::points("Small", &small).at(small_at), Series::points("Large", &large).at(large_at)];
806 ui.chart_view(&mut app.view, &Chart::of(&dots), 560.0, 220.0);
807 // The last minute, a value every tenth of a second: only what changed is drawn again.
808 ui.toggle(&mut app.live, "Live");
809 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |since| since.as_secs_f64());
810 if app.live && now - app.fed >= 0.1 {
811 let next = app.load.last().copied().unwrap_or(0.5) + ((now * 7.3).sin() * 0.08) as f32;
812 app.load.remove(0);
813 app.load.push(next.clamp(0.0, 1.0));
814 app.fed = now;
815 }
816 if app.live {
817 ui.wake_after(std::time::Duration::from_millis(100));
818 }
819 ui.chart(&Chart::of(&[Series::area("Load", &app.load)]).x_range(-59.9, 0.0).y_range(0.0, 1.0).time(app.fed), 560.0, 160.0);
820 ui.close();
821}Sourcepub const fn time(self, origin: f64) -> Self
pub const fn time(self, origin: f64) -> Self
X values are seconds after origin (seconds since 1970, UTC), labelled as local dates
and times at round steps (a second to a year). The last hour, one value a second:
Chart::of(&rows).across(-3599.0, 0.0).time(now).
Examples found in repository?
789fn charts(app: &mut App, ui: &mut Ui) {
790 let kinds = KINDS.map(|kind| app.files.iter().filter(|file| file.2 == kind).count() as f32);
791 ui.column().grow().scroll().pad(14.0).gap(14.0).open();
792 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
793 ui.chart_view(&mut app.slices, &Chart::of(&[Series::pie("Files", &kinds)]).categories(&KINDS), 300.0, 160.0);
794 ui.chart(&Chart::of(&[Series::ring("Files", &kinds)]).categories(&KINDS), 230.0, 160.0);
795 ui.close();
796 ui.row().gap(16.0).align(Align::Start, Align::Start).open();
797 let (read, written) = ([3.0, 5.0, 2.0, 6.0, 4.0, 1.0, 2.0], [1.0, 2.0, 4.0, 1.0, 3.0, 2.0, 0.5]);
798 ui.chart(&Chart::of(&[Series::bars("Read", &read), Series::bars("Written", &written)]).categories(&DAYS).stacked(), 300.0, 190.0);
799 dim_text(ui, "The wheel zooms the scatter, dragging moves it, two clicks show all of it again; its names switch its rows off and on.");
800 ui.close();
801 let [small_at, small, large_at, large] = &app.dots;
802 // (Each dot's height goes with where it stands across, and a little by chance.)
803 let small: Vec<f32> = small_at.iter().zip(small).map(|(at, chance)| at * 0.5 + chance / 5.0).collect();
804 let large: Vec<f32> = large_at.iter().zip(large).map(|(at, chance)| 30.0 + at * 0.6 + chance / 4.0).collect();
805 let dots = [Series::points("Small", &small).at(small_at), Series::points("Large", &large).at(large_at)];
806 ui.chart_view(&mut app.view, &Chart::of(&dots), 560.0, 220.0);
807 // The last minute, a value every tenth of a second: only what changed is drawn again.
808 ui.toggle(&mut app.live, "Live");
809 let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0.0, |since| since.as_secs_f64());
810 if app.live && now - app.fed >= 0.1 {
811 let next = app.load.last().copied().unwrap_or(0.5) + ((now * 7.3).sin() * 0.08) as f32;
812 app.load.remove(0);
813 app.load.push(next.clamp(0.0, 1.0));
814 app.fed = now;
815 }
816 if app.live {
817 ui.wake_after(std::time::Duration::from_millis(100));
818 }
819 ui.chart(&Chart::of(&[Series::area("Load", &app.load)]).x_range(-59.9, 0.0).y_range(0.0, 1.0).time(app.fed), 560.0, 160.0);
820 ui.close();
821}