Better text display in partitions

pull/1/head
Benedikt Terhechte 3 years ago
parent 7e67a1d2f5
commit 4802b27a2f

26
Cargo.lock generated

@ -81,6 +81,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "arrayvec"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9"
dependencies = [
"nodrop",
]
[[package]]
name = "atomic_refcell"
version = "0.1.8"
@ -739,6 +748,7 @@ dependencies = [
"eyre",
"flate2",
"lazy_static",
"num-format",
"rayon",
"regex",
"rsql_builder",
@ -1062,6 +1072,12 @@ dependencies = [
"libc",
]
[[package]]
name = "nodrop"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "nom"
version = "7.0.0"
@ -1082,6 +1098,16 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-format"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bafe4179722c2894288ee77a9f044f02811c86af699344c498b0840c698a2465"
dependencies = [
"arrayvec",
"itoa",
]
[[package]]
name = "num-integer"
version = "0.1.44"

@ -23,6 +23,7 @@ crossbeam-channel = "0.5.1"
eframe = { version = "*", optional = true}
rsql_builder = "0.1.2"
treemap = "0.3.2"
num-format = "0.4.0"
[features]
default = ["gui"]

@ -1,7 +1,8 @@
use std::collections::hash_map::DefaultHasher;
use crate::cluster_engine::{Engine, IntoRequest, Partition};
use eframe::egui::{self, Align2, Rgba, Stroke, TextStyle, Widget};
use eframe::egui::{self, epaint::Galley, Pos2, Rgba, Stroke, TextStyle, Widget};
use num_format::{Locale, ToFormattedString};
fn partition_to_color(partition: &Partition) -> Rgba {
let mut hasher = DefaultHasher::new();
@ -68,23 +69,36 @@ fn rectangle_ui(ui: &mut egui::Ui, partition: &Partition) -> egui::Response {
let painter = ui.painter();
painter.rect(rect, 0.0, color, stroke);
let center = rect.center();
let label = format!("{}\n{}", &partition.field.value(), &partition.count);
let mut center = rect.center();
let align_bottom = |galley: &std::sync::Arc<Galley>, center: &mut Pos2, spacing: f32| {
let mut position = center.clone();
position.x -= galley.size.x / 2.0;
position.y -= galley.size.y / 2.0;
center.y += galley.size.y + spacing;
if galley.size.x < rect.width() && galley.size.y < rect.height() {
Some(position)
} else {
None
}
};
let style = TextStyle::Body;
let galley = painter.layout_multiline(style, label.clone(), 32.0);
if galley.size.x < rect.width() && galley.size.y < rect.height() {
// Can't just paint the galley as it has no `anchor` prop..
painter.text(
center,
Align2::CENTER_CENTER,
&label,
style,
Rgba::BLACK.into(),
);
// Write the label and the amount
{
let text = format!("{}", partition.field.value());
let galley = painter.layout_no_wrap(TextStyle::Body, text);
if let Some(center) = align_bottom(&galley, &mut center, 2.0) {
painter.galley(center, galley, Rgba::BLACK.into());
}
}
{
let text = partition.count.to_formatted_string(&Locale::en);
let galley = painter.layout_no_wrap(TextStyle::Small, text);
if let Some(center) = align_bottom(&galley, &mut center, 5.0) {
painter.galley(center, galley, Rgba::BLACK.into());
}
}
let label = format!("{}\n{}", &partition.field.value(), &partition.count);
response.on_hover_text(&label)
}

Loading…
Cancel
Save