1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! ChartState constructors and builder methods.
//!
//! Extracted from the main chart module to keep file sizes manageable.
use ratatui::style::Color;
use super::series::DEFAULT_PALETTE;
use super::{
BarMode, ChartAnnotation, ChartKind, ChartState, DataSeries, Scale, ThresholdLine, VerticalLine,
};
impl ChartState {
/// Applies default palette colors to series that have the default Cyan color.
fn apply_palette_colors(mut series: Vec<DataSeries>) -> Vec<DataSeries> {
for (i, s) in series.iter_mut().enumerate() {
if s.color() == Color::Cyan {
s.set_color(DEFAULT_PALETTE[i % DEFAULT_PALETTE.len()]);
}
}
series
}
/// Creates a line chart state with the given series.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::line(vec![
/// DataSeries::new("Series A", vec![1.0, 2.0, 3.0]),
/// ]);
/// assert_eq!(state.series().len(), 1);
/// ```
pub fn line(series: Vec<DataSeries>) -> Self {
Self {
series: Self::apply_palette_colors(series),
kind: ChartKind::Line,
..Default::default()
}
}
/// Creates a vertical bar chart state.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartKind, ChartState, DataSeries};
///
/// let state = ChartState::bar_vertical(vec![
/// DataSeries::new("Sales", vec![10.0, 20.0, 30.0]),
/// ]);
/// assert_eq!(state.kind(), &ChartKind::BarVertical);
/// ```
pub fn bar_vertical(series: Vec<DataSeries>) -> Self {
Self {
series: Self::apply_palette_colors(series),
kind: ChartKind::BarVertical,
..Default::default()
}
}
/// Creates a horizontal bar chart state.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartKind, ChartState, DataSeries};
///
/// let state = ChartState::bar_horizontal(vec![
/// DataSeries::new("Memory", vec![512.0, 768.0, 1024.0]),
/// ]);
/// assert_eq!(state.kind(), &ChartKind::BarHorizontal);
/// ```
pub fn bar_horizontal(series: Vec<DataSeries>) -> Self {
Self {
series: Self::apply_palette_colors(series),
kind: ChartKind::BarHorizontal,
..Default::default()
}
}
/// Creates an area chart state with the given series.
///
/// Area charts render as filled line charts using shared axes.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartKind, ChartState, DataSeries};
///
/// let state = ChartState::area(vec![
/// DataSeries::new("CPU", vec![45.0, 52.0, 48.0]),
/// ]);
/// assert_eq!(state.kind(), &ChartKind::Area);
/// ```
pub fn area(series: Vec<DataSeries>) -> Self {
Self {
series: Self::apply_palette_colors(series),
kind: ChartKind::Area,
..Default::default()
}
}
/// Creates a scatter plot state with the given series.
///
/// Scatter plots render individual data points without connecting lines.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartKind, ChartState, DataSeries};
///
/// let state = ChartState::scatter(vec![
/// DataSeries::new("Points", vec![10.0, 25.0, 15.0, 30.0]),
/// ]);
/// assert_eq!(state.kind(), &ChartKind::Scatter);
/// ```
pub fn scatter(series: Vec<DataSeries>) -> Self {
Self {
series: Self::apply_palette_colors(series),
kind: ChartKind::Scatter,
..Default::default()
}
}
/// Sets the title (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::line(vec![DataSeries::new("CPU", vec![50.0])])
/// .with_title("CPU Usage");
/// assert_eq!(state.title(), Some("CPU Usage"));
/// ```
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Sets the X-axis label (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::line(vec![DataSeries::new("CPU", vec![50.0])])
/// .with_x_label("Time");
/// assert_eq!(state.x_label(), Some("Time"));
/// ```
pub fn with_x_label(mut self, label: impl Into<String>) -> Self {
self.x_label = Some(label.into());
self
}
/// Sets the Y-axis label (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::line(vec![DataSeries::new("CPU", vec![50.0])])
/// .with_y_label("Percent");
/// assert_eq!(state.y_label(), Some("Percent"));
/// ```
pub fn with_y_label(mut self, label: impl Into<String>) -> Self {
self.y_label = Some(label.into());
self
}
/// Sets whether to show the legend (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::line(vec![DataSeries::new("CPU", vec![50.0])])
/// .with_show_legend(false);
/// assert!(!state.show_legend());
/// ```
pub fn with_show_legend(mut self, show: bool) -> Self {
self.show_legend = show;
self
}
/// Sets the maximum display points for line charts (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::line(vec![DataSeries::new("Temp", vec![20.0, 22.0])])
/// .with_max_display_points(200);
/// assert_eq!(state.max_display_points(), 200);
/// ```
pub fn with_max_display_points(mut self, max: usize) -> Self {
self.max_display_points = max;
self
}
/// Sets the bar width (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::bar_vertical(vec![DataSeries::new("Sales", vec![10.0])])
/// .with_bar_width(5);
/// assert_eq!(state.bar_width(), 5);
/// ```
pub fn with_bar_width(mut self, width: u16) -> Self {
self.bar_width = width.max(1);
self
}
/// Sets the bar gap (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::bar_vertical(vec![DataSeries::new("Sales", vec![10.0])])
/// .with_bar_gap(2);
/// assert_eq!(state.bar_gap(), 2);
/// ```
pub fn with_bar_gap(mut self, gap: u16) -> Self {
self.bar_gap = gap;
self
}
/// Sets the bar rendering mode (builder pattern).
///
/// Controls how multiple series are displayed in bar charts:
/// - [`BarMode::Single`]: Only the active series is shown (default).
/// - [`BarMode::Grouped`]: All series are shown side-by-side at each position.
/// - [`BarMode::Stacked`]: All series are stacked vertically at each position.
///
/// # Example
///
/// ```rust
/// use envision::component::{BarMode, ChartState, DataSeries};
///
/// let state = ChartState::bar_vertical(vec![
/// DataSeries::new("Q1", vec![10.0, 20.0]),
/// DataSeries::new("Q2", vec![15.0, 25.0]),
/// ])
/// .with_bar_mode(BarMode::Grouped);
/// assert_eq!(state.bar_mode(), &BarMode::Grouped);
/// ```
pub fn with_bar_mode(mut self, mode: BarMode) -> Self {
self.bar_mode = mode;
self
}
/// Sets the category labels for bar chart x-axis (builder pattern).
///
/// When set, these labels replace numeric indices on the x-axis of bar charts.
/// If fewer categories are provided than data points, remaining bars fall back
/// to numeric labels. Extra categories beyond the data length are ignored.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::bar_vertical(vec![
/// DataSeries::new("Importance", vec![0.85, 0.72, 0.64, 0.51]),
/// ])
/// .with_categories(vec!["Income", "Education", "Age", "Hours/Week"]);
/// assert_eq!(state.categories(), &["Income", "Education", "Age", "Hours/Week"]);
/// ```
pub fn with_categories(mut self, categories: Vec<impl Into<String>>) -> Self {
self.categories = categories.into_iter().map(Into::into).collect();
self
}
/// Adds a threshold line (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
/// use ratatui::style::Color;
///
/// let state = ChartState::area(vec![DataSeries::new("CPU", vec![50.0])])
/// .with_threshold(95.0, "SLO", Color::Yellow);
/// assert_eq!(state.thresholds().len(), 1);
/// ```
pub fn with_threshold(mut self, value: f64, label: impl Into<String>, color: Color) -> Self {
self.thresholds
.push(ThresholdLine::new(value, label, color));
self
}
/// Sets the manual Y-axis range (builder pattern).
///
/// Values outside this range will be clipped by the chart widget.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::area(vec![DataSeries::new("CPU", vec![50.0])])
/// .with_y_range(0.0, 100.0);
/// assert_eq!(state.y_min(), Some(0.0));
/// assert_eq!(state.y_max(), Some(100.0));
/// ```
pub fn with_y_range(mut self, min: f64, max: f64) -> Self {
self.y_min = Some(min);
self.y_max = Some(max);
self
}
/// Sets the Y-axis scale (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, Scale};
///
/// let state = ChartState::line(vec![])
/// .with_y_scale(Scale::Log10);
/// assert_eq!(state.y_scale(), &Scale::Log10);
/// ```
pub fn with_y_scale(mut self, scale: Scale) -> Self {
self.y_scale = scale;
self
}
/// Adds a vertical reference line (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
/// use ratatui::style::Color;
///
/// let state = ChartState::line(vec![DataSeries::new("CPU", vec![50.0, 60.0, 70.0])])
/// .with_vertical_line(1.0, "Deploy", Color::Yellow);
/// assert_eq!(state.vertical_lines().len(), 1);
/// ```
pub fn with_vertical_line(
mut self,
x_value: f64,
label: impl Into<String>,
color: Color,
) -> Self {
self.vertical_lines
.push(VerticalLine::new(x_value, label, color));
self
}
/// Sets whether to show grid lines at tick positions (builder pattern).
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let state = ChartState::line(vec![DataSeries::new("CPU", vec![50.0])])
/// .with_grid(true);
/// assert!(state.show_grid());
/// ```
pub fn with_grid(mut self, show: bool) -> Self {
self.show_grid = show;
self
}
/// Sets custom string labels for the X-axis (builder pattern).
///
/// When set, these labels replace the numeric tick labels on the X-axis
/// of line, area, and scatter charts. The caller is responsible for
/// formatting the labels (e.g., formatting timestamps as strings).
///
/// Labels are spaced evenly across the X-axis width. If there are more
/// labels than can fit, a subset is displayed.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
///
/// let series = DataSeries::new("Requests", vec![100.0, 250.0, 180.0, 320.0, 90.0]);
/// let state = ChartState::line(vec![series])
/// .with_x_labels(vec!["00:00", "06:00", "12:00", "18:00", "24:00"])
/// .with_title("Request Rate (24h)");
/// assert_eq!(state.x_labels().unwrap(), &["00:00", "06:00", "12:00", "18:00", "24:00"]);
/// ```
pub fn with_x_labels(mut self, labels: Vec<impl Into<String>>) -> Self {
self.x_labels = Some(labels.into_iter().map(Into::into).collect());
self
}
/// Adds a text annotation at a data coordinate (builder pattern).
///
/// Annotations are rendered as text labels near the specified (x, y)
/// position in the chart's data space. Useful for labeling notable
/// data points such as peaks, anomalies, or events.
///
/// # Example
///
/// ```rust
/// use envision::component::{ChartState, DataSeries};
/// use ratatui::style::Color;
///
/// let state = ChartState::line(vec![
/// DataSeries::new("CPU", vec![50.0, 90.0, 60.0]),
/// ])
/// .with_annotation(1.0, 90.0, "Peak", Color::Yellow);
/// assert_eq!(state.annotations().len(), 1);
/// assert_eq!(state.annotations()[0].label, "Peak");
/// ```
pub fn with_annotation(
mut self,
x: f64,
y: f64,
label: impl Into<String>,
color: Color,
) -> Self {
self.annotations
.push(ChartAnnotation::new(x, y, label, color));
self
}
}