MCPcopy Create free account
hub / github.com/donkeyteethUX/iced_plot / new

Function new

examples/custom_ticks.rs:29–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27}
28
29fn new() -> PlotWidget {
30 // Generate some sample data representing temperature over time
31 let mut positions: Vec<[f64; 2]> = Vec::new();
32 let mut temps: Vec<f64> = Vec::new();
33 for hour in 0..=24 {
34 let t = hour as f64;
35 // Temperature varies throughout the day
36 let temp = 20.0 + 10.0 * (std::f64::consts::PI * (t - 6.0) / 12.0).sin();
37 positions.push([t * 3600.0, temp]);
38 temps.push(temp);
39 }
40
41 let (min_temp, max_temp) = temps.iter().fold((f64::MAX, f64::MIN), |acc, t| {
42 (acc.0.min(*t), acc.1.max(*t))
43 });
44 let temp_span = (max_temp - min_temp).max(1e-6);
45 let colors: Vec<Color> = temps
46 .iter()
47 .map(|t| {
48 let n = ((*t - min_temp) / temp_span) as f32;
49 let r = 0.2 + 0.8 * n;
50 let g = 0.3 + 0.2 * (1.0 - n);
51 let b = 1.0 - 0.8 * n;
52 Color::from_rgb(r, g, b)
53 })
54 .collect();
55
56 let series = Series::new(positions, MarkerStyle::circle(5.0), LineStyle::solid())
57 .with_label("Temperature")
58 .with_point_colors(colors)
59 .with_color(Color::from_rgb(1.0, 0.5, 0.2));
60
61 PlotWidgetBuilder::new()
62 .add_series(series)
63 .with_x_label("Time of Day\n\n\n")
64 .with_y_label("Temperature")
65 // Custom tick producer for X axis: place ticks every 4 hours
66 .with_x_tick_producer(|min, max| {
67 let hour_in_seconds = 3600.0;
68 let tick_interval = 4.0 * hour_in_seconds; // 4 hours
69
70 let start = (min / tick_interval).floor() * tick_interval;
71 let mut ticks = Vec::new();
72 let mut value = start;
73
74 while value <= max {
75 if value >= min {
76 ticks.push(Tick {
77 value,
78 step_size: tick_interval,
79 line_type: TickWeight::Major,
80 });
81 }
82 value += tick_interval;
83 }
84
85 ticks
86 })

Callers

nothing calls this directly

Calls 15

mapMethod · 0.80
with_point_colorsMethod · 0.80
buildMethod · 0.80
with_cursor_providerMethod · 0.80
with_y_tick_formatterMethod · 0.80
with_y_tick_producerMethod · 0.80
with_x_tick_formatterMethod · 0.80
with_x_tick_producerMethod · 0.80
with_y_labelMethod · 0.80
with_x_labelMethod · 0.80

Tested by

no test coverage detected