embedded_charts/
prelude.rs

1//! Convenience re-exports for common types and traits.
2//!
3//! This module provides a comprehensive prelude that re-exports the most commonly used
4//! types, traits, and functions from the embedded-charts library. By importing this
5//! prelude, you get access to everything needed for typical chart creation and usage.
6//!
7//! # Usage
8//!
9//! ```rust
10//! use embedded_charts::prelude::*;
11//! use embedded_graphics::pixelcolor::Rgb565;
12//!
13//! // Now you have access to all common types and functions
14//! let mut data: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
15//! data.push(Point2D::new(0.0, 10.0))?;
16//!
17//! # #[cfg(feature = "line")]
18//! let chart = LineChart::builder()
19//!     .line_color(Rgb565::BLUE)
20//!     .build()?;
21//! # Ok::<(), embedded_charts::error::ChartError>(())
22//! ```
23//!
24//! # What's Included
25//!
26//! ## Core Chart Types
27//! - [`LineChart`], [`CurveChart`], [`BarChart`], [`PieChart`], `ScatterChart`, `GaugeChart`
28//! - Chart builders and style configurations
29//! - Animation support (feature-gated)
30//!
31//! ## Data Management
32//! - [`Point2D`], [`StaticDataSeries`], [`MultiSeries`]
33//! - Data bounds calculation utilities
34//! - Streaming data support (feature-gated)
35//!
36//! ## Styling and Themes
37//! - Color palettes and themes
38//! - Line styles, fill patterns, and borders
39//! - Typography support (feature-gated)
40//!
41//! ## Layout and Rendering
42//! - Chart layout and positioning
43//! - Rendering primitives and utilities
44//! - Memory management tools
45//!
46//! ## Utility Modules
47//! - [`types`] - Common type aliases for convenience
48//! - [`constants`] - Predefined constants for margins, spacing, etc.
49//! - [`quick`] - Quick-start functions for common chart configurations
50//!
51//! ## Utility Macros
52//! - [`data_points!`] - Create data series from tuples
53//! - [`chart_config!`] - Fluent chart configuration syntax
54//!
55//! # Quick Start Examples
56//!
57//! ## Simple Line Chart
58//! ```rust
59//! # #[cfg(feature = "line")]
60//! # {
61//! # fn test() -> Result<(), embedded_charts::error::ChartError> {
62//! use embedded_charts::prelude::*;
63//! use embedded_graphics::pixelcolor::Rgb565;
64//!
65//! let data = data_points![(0.0, 10.0), (1.0, 20.0), (2.0, 15.0)];
66//! let chart = quick::line_chart().build()?;
67//! # Ok(())
68//! # }
69//! # }
70//! ```
71//!
72//! ## Professional Styled Chart
73//! ```rust
74//! # #[cfg(feature = "line")]
75//! # {
76//! # fn test() -> Result<(), embedded_charts::error::ChartError> {
77//! use embedded_charts::prelude::*;
78//! use embedded_graphics::pixelcolor::Rgb565;
79//!
80//! let chart = quick::professional_line_chart()
81//!     .with_markers(MarkerStyle::default())
82//!     .build()?;
83//!
84//! let config = chart_config! {
85//!     title: "Temperature Monitor",
86//!     background: Rgb565::WHITE,
87//!     margins: constants::DEFAULT_MARGINS,
88//!     grid: true,
89//! };
90//! # Ok(())
91//! # }
92//! # }
93//! ```
94//!
95//! ## Multi-Series Chart
96//! ```rust
97//! use embedded_charts::prelude::*;
98//!
99//! let mut multi_series: MultiSeries<Point2D, 8, 256> = MultiSeries::new();
100//! let temp_data = data_points![(0.0, 22.5), (1.0, 23.1), (2.0, 24.2)];
101//! let humidity_data = data_points![(0.0, 65.0), (1.0, 68.0), (2.0, 72.0)];
102//!
103//! multi_series.add_series(temp_data)?;
104//! multi_series.add_series(humidity_data)?;
105//! # Ok::<(), embedded_charts::error::DataError>(())
106//! ```
107//!
108//! # Feature-Gated Exports
109//!
110//! Some exports are only available when specific features are enabled:
111//!
112//! - **animations**: Animation and streaming chart support
113//! - **color-support**: Extended color palette functions
114//! - **std**: Standard library time providers and error traits
115//!
116//! # Memory Efficiency
117//!
118//! All re-exported types are designed for embedded systems:
119//! - Static allocation with compile-time bounds
120//! - No heap usage in `no_std` environments
121//! - Configurable memory usage through type parameters
122
123// Math abstraction layer
124pub use crate::math::{Math, Number, NumericConversion};
125
126// Core traits
127pub use crate::chart::traits::{
128    Chart, ChartBuilder, ChartConfig, IncrementalChart, Margins, StylableChart,
129};
130
131#[cfg(feature = "animations")]
132pub use crate::chart::traits::{AnimatedChart, StreamingChart};
133
134pub use crate::chart::traits::{AxisChart, LegendChart};
135
136// Legend types
137pub use crate::legend::{
138    BackgroundStyle, CompactLegend, CompactLegendBuilder, CustomLegend, CustomLegendBuilder,
139    DefaultLegend, DefaultLegendEntry, DefaultLegendRenderer, Legend, LegendAlignment,
140    LegendBuilder, LegendEntry, LegendEntryType, LegendMargins, LegendOrientation, LegendRenderer,
141    LegendStyle, PositionCalculator, SpacingStyle, StandardLegend, StandardLegendBuilder,
142    StandardLegendRenderer, SymbolStyle, TextStyle,
143};
144
145pub use crate::legend::types::{
146    CompactLegendEntry, CustomLayoutParams, CustomLegendEntry, MarkerShape as LegendMarkerShape,
147    MarkerStyle as LegendMarkerStyle, StandardLegendEntry, SymbolShape,
148};
149
150pub use crate::legend::position::LegendPosition as LegendPos;
151
152// Axes types
153pub use crate::axes::{
154    AxisConfig, AxisOrientation, AxisPosition, AxisStyle, AxisValue, CustomAxisBuilder,
155    CustomTickGenerator, LinearAxis, LinearAxisBuilder, LinearTickGenerator, TickStyle,
156};
157
158pub use crate::axes::builder::presets;
159
160pub use crate::axes::traits::{Axis, AxisRenderer, Tick, TickGenerator};
161
162// Axis range calculation
163pub use crate::axes::range::{
164    calculate_nice_range, calculate_nice_ranges_from_bounds, calculate_nice_ranges_separate_config,
165    RangeCalculationConfig,
166};
167
168// Grid types
169pub use crate::grid::{
170    CustomGrid, CustomGridBuilder, GridBuilder, GridContainer, GridLineStyle, GridSpacing,
171    GridStyle, GridSystem, GridType, GridVisibility, LinearGrid, LinearGridBuilder, MajorGridStyle,
172    MinorGridStyle, TickBasedGrid, TickBasedGridBuilder,
173};
174
175pub use crate::grid::traits::{
176    DefaultGridRenderer, Grid, GridConfiguration, GridOrientation, GridRenderer,
177};
178
179pub use crate::grid::traits::TickAlignedGrid;
180
181// Chart types
182#[cfg(feature = "line")]
183pub use crate::chart::{LineChart, LineChartBuilder, LineChartStyle, MarkerShape, MarkerStyle};
184
185#[cfg(feature = "line")]
186pub use crate::chart::{CurveChart, CurveChartBuilder};
187
188#[cfg(feature = "line")]
189pub use crate::math::interpolation::{InterpolationConfig, InterpolationType};
190
191#[cfg(all(feature = "line", feature = "animations"))]
192pub use crate::chart::{AnimatedLineChart, AnimatedLineChartBuilder};
193
194#[cfg(feature = "bar")]
195pub use crate::chart::{BarChart, BarChartBuilder, BarChartStyle, BarOrientation};
196
197#[cfg(all(feature = "bar", feature = "animations"))]
198pub use crate::chart::{AnimatedBarChart, AnimatedBarChartBuilder};
199
200#[cfg(feature = "bar")]
201pub use crate::chart::bar::BarWidth;
202
203#[cfg(feature = "pie")]
204pub use crate::chart::{PieChart, PieChartBuilder, PieChartStyle};
205
206#[cfg(feature = "scatter")]
207pub use crate::chart::{
208    CollisionSettings, CollisionStrategy, ColorMapping, ColorMappingStrategy, PointShape,
209    PointStyle, ScatterChart, ScatterChartBuilder, ScatterChartStyle, SizeMapping, SizeScaling,
210};
211
212#[cfg(feature = "gauge")]
213pub use crate::chart::{
214    ArcStyle, CenterStyle, GaugeChart, GaugeChartBuilder, GaugeChartStyle, GaugeType, NeedleShape,
215    NeedleStyle, ThresholdZone, TickStyle as GaugeTickStyle, ValueDisplayStyle, ValueRange,
216};
217
218#[cfg(feature = "stacked-charts")]
219pub use crate::chart::stacked::{
220    AnimatedStackedBarChart, AnimatedStackedBarChartBuilder, AnimatedStackedLineChart,
221    AnimatedStackedLineChartBuilder, StackedBarWidth, StackedData,
222};
223
224// Data types
225pub use crate::data::{
226    calculate_bounds, calculate_multi_series_bounds, DataBounds, DataPoint, DataSeries,
227    FloatBounds, IntBounds, IntPoint, MultiSeries, Point2D, StaticDataSeries, TimestampedPoint,
228};
229
230#[cfg(feature = "animations")]
231pub use crate::data::SlidingWindowSeries;
232
233// Streaming types
234#[cfg(feature = "animations")]
235pub use crate::data::streaming::{
236    ChartInstance, ChartInstanceConfig, ChartType, ErrorRecovery, ManagerConfig, ManagerMetrics,
237    MemoryStrategy, MonitoringLevel, PipelineConfig, PipelineMetrics, SourceConfig, SourceState,
238    StreamingChartManager, StreamingConfig, StreamingDataPipeline, StreamingDataSource,
239    StreamingMetrics, SyncMode, SyncState, UnifiedStreamingBuffer,
240};
241
242// Style types
243pub use crate::style::{
244    BorderStyle, ColorInterpolation, ColorPalette, ColorUtils, FillPattern, FillStyle, LineCap,
245    LineJoin, LinePattern, LineStyle, StrokeStyle,
246};
247
248// Theme types
249pub use crate::style::themes::Theme;
250
251#[cfg(feature = "color-support")]
252pub use crate::style::rgb565_palettes;
253
254// Layout types
255pub use crate::layout::{ChartLayout, ComponentPositioning, Viewport};
256
257// Rendering types
258pub use crate::render::{
259    ChartRenderer, ClippingRenderer, EnhancedChartRenderer, PrimitiveRenderer,
260};
261
262#[cfg(feature = "animations")]
263pub use crate::render::AnimationFrameRenderer;
264
265pub use crate::render::text::TextRenderer;
266
267// Memory management
268pub use crate::memory::{
269    ChartMemoryManager, FixedCapacityCollections, LabelStorage, ManagedSlidingWindow, MemoryStats,
270};
271
272// Error types
273pub use crate::error::{
274    ChartError, ChartResult, DataError, DataResult, LayoutError, LayoutResult, RenderError,
275    RenderResult,
276};
277
278#[cfg(feature = "animations")]
279pub use crate::error::{AnimationError, AnimationResult};
280
281// Animation types
282#[cfg(feature = "animations")]
283pub use crate::animation::{
284    ChartAnimator, EasingFunction, Interpolatable, MultiStateAnimator, Progress, StreamingAnimator,
285    TimeBasedProgress,
286};
287
288// Time abstraction types
289pub use crate::time::{
290    ManualTimeProvider, Microseconds, Milliseconds, MonotonicTimeProvider, TimeProvider,
291};
292
293#[cfg(feature = "std")]
294pub use crate::time::StdTimeProvider;
295
296// Fluent API for convenient chart creation
297pub use crate::fluent::quick as fluent_quick;
298pub use crate::fluent::{Chart as FluentChart, ChartPreset};
299
300// Re-export embedded-graphics types commonly used with charts
301pub use embedded_graphics::{
302    pixelcolor::{BinaryColor, Rgb565},
303    prelude::*,
304    primitives::{Circle, Line, Rectangle},
305};
306
307// Re-export heapless types for data storage
308pub use heapless::{String, Vec};
309
310// Enhanced heapless utilities for no_std support
311pub use crate::heapless_utils::{sizes, string, vec, CircularBuffer, HeaplessConfig, HeaplessPool};
312
313// Re-export heapless utility macros
314pub use crate::{heapless_string, heapless_vec};
315
316/// Common type aliases for convenience
317pub mod types {
318    use super::*;
319
320    /// Standard RGB565 line chart
321    #[cfg(feature = "line")]
322    pub type Rgb565LineChart = LineChart<Rgb565>;
323
324    /// Standard RGB565 line chart builder
325    #[cfg(feature = "line")]
326    pub type Rgb565LineChartBuilder = LineChartBuilder<Rgb565>;
327
328    /// Standard RGB565 curve chart
329    #[cfg(feature = "line")]
330    pub type Rgb565CurveChart = CurveChart<Rgb565>;
331
332    /// Standard RGB565 curve chart builder
333    #[cfg(feature = "line")]
334    pub type Rgb565CurveChartBuilder = CurveChartBuilder<Rgb565>;
335
336    // Note: Animated chart types are not yet implemented
337    // /// Standard RGB565 animated line chart
338    // #[cfg(feature = "animations")]
339    // pub type Rgb565AnimatedLineChart = AnimatedLineChart<Rgb565>;
340
341    // /// Standard RGB565 animated line chart builder
342    // #[cfg(feature = "animations")]
343    // pub type Rgb565AnimatedLineChartBuilder = AnimatedLineChartBuilder<Rgb565>;
344
345    // /// Standard RGB565 animated bar chart
346    // #[cfg(all(feature = "bar", feature = "animations"))]
347    // pub type Rgb565AnimatedBarChart = AnimatedBarChart<Rgb565>;
348
349    // /// Standard RGB565 animated bar chart builder
350    // #[cfg(all(feature = "bar", feature = "animations"))]
351    // pub type Rgb565AnimatedBarChartBuilder = AnimatedBarChartBuilder<Rgb565>;
352
353    /// Standard floating point data series with 256 point capacity
354    pub type StandardDataSeries = StaticDataSeries<Point2D, 256>;
355
356    /// Standard multi-series container (8 series, 256 points each)
357    pub type StandardMultiSeries = MultiSeries<Point2D, 8, 256>;
358
359    /// Standard color palette with 8 colors
360    pub type StandardColorPalette = ColorPalette<Rgb565, 8>;
361
362    /// Standard sliding window for real-time data (100 points)
363    #[cfg(feature = "animations")]
364    pub type StandardSlidingWindow = SlidingWindowSeries<Point2D, 100>;
365
366    /// Standard memory manager (4KB pool)
367    pub type StandardMemoryManager = ChartMemoryManager<4096>;
368
369    /// Standard label storage (16 labels, 32 chars each)
370    pub type StandardLabelStorage = LabelStorage<16, 32>;
371}
372
373/// Commonly used constants
374pub mod constants {
375    use super::*;
376
377    /// Default chart margins
378    pub const DEFAULT_MARGINS: Margins = Margins {
379        top: 10,
380        right: 10,
381        bottom: 10,
382        left: 10,
383    };
384
385    /// Minimal margins for small displays
386    pub const MINIMAL_MARGINS: Margins = Margins {
387        top: 5,
388        right: 5,
389        bottom: 5,
390        left: 5,
391    };
392
393    /// Default grid spacing
394    pub const DEFAULT_GRID_SPACING: Size = Size::new(20, 20);
395
396    /// Fine grid spacing
397    pub const FINE_GRID_SPACING: Size = Size::new(10, 10);
398
399    /// Coarse grid spacing
400    pub const COARSE_GRID_SPACING: Size = Size::new(50, 50);
401}
402
403/// Quick start functions for common chart types
404pub mod quick {
405    use super::*;
406
407    /// Create a simple line chart with default styling
408    #[cfg(feature = "line")]
409    pub fn line_chart() -> LineChartBuilder<Rgb565> {
410        LineChart::builder()
411    }
412
413    /// Create a line chart with professional styling
414    #[cfg(feature = "line")]
415    pub fn professional_line_chart() -> LineChartBuilder<Rgb565> {
416        LineChart::builder()
417            .line_color(Rgb565::new(70 >> 3, 130 >> 2, 180 >> 3)) // Steel Blue
418            .line_width(2)
419    }
420
421    /// Create a simple smooth curve chart with default styling
422    #[cfg(feature = "line")]
423    pub fn curve_chart() -> CurveChartBuilder<Rgb565> {
424        CurveChart::builder()
425    }
426
427    /// Create a smooth curve chart with professional styling
428    #[cfg(feature = "line")]
429    pub fn professional_curve_chart() -> CurveChartBuilder<Rgb565> {
430        CurveChart::builder()
431            .line_color(Rgb565::new(70 >> 3, 130 >> 2, 180 >> 3)) // Steel Blue
432            .line_width(2)
433            .interpolation_type(InterpolationType::CubicSpline)
434            .subdivisions(12)
435    }
436
437    /// Create a simple data series from tuples
438    pub fn data_series_from_tuples(data: &[(f32, f32)]) -> ChartResult<types::StandardDataSeries> {
439        StaticDataSeries::from_tuples(data).map_err(ChartError::from)
440    }
441
442    /// Create a default color palette
443    #[cfg(feature = "color-support")]
444    pub fn default_colors() -> types::StandardColorPalette {
445        rgb565_palettes::default_palette()
446    }
447
448    /// Create a professional color palette
449    #[cfg(feature = "color-support")]
450    pub fn professional_colors() -> types::StandardColorPalette {
451        rgb565_palettes::professional_palette()
452    }
453
454    /// Create a pastel color palette
455    #[cfg(feature = "color-support")]
456    pub fn pastel_colors() -> types::StandardColorPalette {
457        rgb565_palettes::pastel_palette()
458    }
459
460    /// Create a vibrant color palette
461    #[cfg(feature = "color-support")]
462    pub fn vibrant_colors() -> types::StandardColorPalette {
463        rgb565_palettes::vibrant_palette()
464    }
465
466    /// Create a nature-inspired color palette
467    #[cfg(feature = "color-support")]
468    pub fn nature_colors() -> types::StandardColorPalette {
469        rgb565_palettes::nature_palette()
470    }
471
472    /// Create an ocean-inspired color palette
473    #[cfg(feature = "color-support")]
474    pub fn ocean_colors() -> types::StandardColorPalette {
475        rgb565_palettes::ocean_palette()
476    }
477
478    /// Create a sunset-inspired color palette
479    #[cfg(feature = "color-support")]
480    pub fn sunset_colors() -> types::StandardColorPalette {
481        rgb565_palettes::sunset_palette()
482    }
483
484    /// Create a cyberpunk-inspired color palette
485    #[cfg(feature = "color-support")]
486    pub fn cyberpunk_colors() -> types::StandardColorPalette {
487        rgb565_palettes::cyberpunk_palette()
488    }
489
490    /// Create a minimal color palette
491    #[cfg(feature = "color-support")]
492    pub fn minimal_colors() -> ColorPalette<Rgb565, 6> {
493        rgb565_palettes::minimal_palette()
494    }
495
496    /// Create a retro color palette
497    #[cfg(feature = "color-support")]
498    pub fn retro_colors() -> types::StandardColorPalette {
499        rgb565_palettes::retro_palette()
500    }
501
502    /// Create a light theme
503    pub fn light_theme() -> Theme<Rgb565> {
504        Theme::light()
505    }
506
507    /// Create a dark theme
508    pub fn dark_theme() -> Theme<Rgb565> {
509        Theme::dark()
510    }
511
512    /// Create a vibrant theme
513    pub fn vibrant_theme() -> Theme<Rgb565> {
514        Theme::vibrant()
515    }
516
517    /// Create a pastel theme
518    pub fn pastel_theme() -> Theme<Rgb565> {
519        Theme::pastel()
520    }
521
522    /// Create a nature theme
523    pub fn nature_theme() -> Theme<Rgb565> {
524        Theme::nature()
525    }
526
527    /// Create an ocean theme
528    pub fn ocean_theme() -> Theme<Rgb565> {
529        Theme::ocean()
530    }
531
532    /// Create a sunset theme
533    pub fn sunset_theme() -> Theme<Rgb565> {
534        Theme::sunset()
535    }
536
537    /// Create a cyberpunk theme
538    pub fn cyberpunk_theme() -> Theme<Rgb565> {
539        Theme::cyberpunk()
540    }
541
542    /// Create a minimal theme
543    pub fn minimal_theme() -> Theme<Rgb565> {
544        Theme::minimal()
545    }
546
547    /// Create a retro theme
548    pub fn retro_theme() -> Theme<Rgb565> {
549        Theme::retro()
550    }
551}
552
553/// Utility macros for common operations
554#[macro_export]
555macro_rules! data_points {
556    [$(($x:expr, $y:expr)),* $(,)?] => {
557        {
558            // Use a reasonable default capacity of 256 points
559            let mut series = $crate::data::StaticDataSeries::<$crate::data::Point2D, 256>::new();
560            $(
561                series.push($crate::data::Point2D::new($x, $y)).unwrap();
562            )*
563            series
564        }
565    };
566}
567
568/// Macro for creating chart configurations with a fluent syntax.
569///
570/// # Examples
571///
572/// ```rust,no_run
573/// use embedded_charts::prelude::*;
574/// use embedded_graphics::pixelcolor::Rgb565;
575///
576/// let config = chart_config! {
577///     title: "My Chart",
578///     background: Rgb565::CSS_WHITE,
579///     margins: Margins::symmetric(10, 10),
580///     grid: true,
581/// };
582/// ```
583#[macro_export]
584macro_rules! chart_config {
585    (
586        $(title: $title:expr,)?
587        $(background: $bg:expr,)?
588        $(margins: $margins:expr,)?
589        $(grid: $grid:expr,)?
590    ) => {
591        {
592            let mut config = $crate::chart::traits::ChartConfig::default();
593            $(
594                config.title = Some($crate::heapless::String::try_from($title).unwrap());
595            )?
596            $(
597                config.background_color = Some($bg);
598            )?
599            $(
600                config.margins = $margins;
601            )?
602            $(
603                config.show_grid = $grid;
604            )?
605            config
606        }
607    };
608}
609
610pub use chart_config;
611/// Re-export the macros
612pub use data_points;