SVG Export Capabilities
Relevant source files
The following files were used as context for generating this wiki page:
ComChan provides a robust system for exporting real-time sensor data visualizations into high-quality Scalable Vector Graphics (SVG) files. This feature allows developers to capture the state of the 2D plotter for documentation, analysis, or presentation purposes. The export system is integrated directly into the plotter module and leverages the plotters crate for vector rendering.
The SVG export capability supports both light and dark modes, automatic axis scaling, and legend generation based on active sensor data. Users can trigger an export at any time during a plotting session using a keyboard shortcut, which generates a timestamped file containing all current in-memory data points for the monitored sensors.
Sources: src/export.rs:11-20, README.md:144, src/plotter.rs:334-355
Architecture and Data Flow
The export system relies on a central data structure maintained within the PlotterState. As sensor data is ingested, it is stored in a HashMap specifically designated for export, ensuring that visualizations reflect the actual received telemetry.
Export Trigger Logic
The export process is initiated via the user interface. When the user presses Ctrl+S, the system captures the current timestamp, formats a filename, and invokes the core export function.
Sources: src/plotter.rs:334-355, src/export.rs:11-18
Core Components
| Component | Description | Source File |
|---|---|---|
export_to_svg | Main function that renders data points to an SVG backend. | src/export.rs:11 |
export_data | A HashMap<String, Vec<(f64, f64)>> storing time-series data for each sensor. | src/plotter.rs:88 |
export_limit | A configuration value that prevents RAM exhaustion by limiting stored points. | src/plotter.rs:89, src/config.rs:105 |
is_dark_mode | A boolean toggle affecting the visual theme of the output SVG. | src/export.rs:16 |
Visual Styling and Themes
The system supports dynamic styling based on the user's preference for light or dark themes. This includes adjustments to background colors, grid lines, and trace colors to ensure high visibility.
Theme Configurations
The export engine uses specific constants for dark mode to provide a professional aesthetic.
- Dark Background:
RGBColor(30, 30, 30) - Dark Grid:
RGBColor(80, 80, 80)
Trace Color Palette
To maintain distinction between multiple sensors, the system cycles through a predefined palette of colors.
| Mode | Primary Trace Colors |
|---|---|
| Dark Mode | Cyan, Red, Green, Magenta, Yellow, White |
| Light Mode | Blue, Red, Green, Magenta, Cyan, Black |
| Sources: src/export.rs:78-82 |
Implementation Details
Data Constraints and Bounds
The export function automatically calculates the X and Y axis bounds based on the data present in the provided HashMap. It includes a 10% padding on the Y-axis to ensure data points do not touch the chart borders. If only a single data point exists (range is zero), it defaults to a range of +/- 1.0.
Sources: src/export.rs:30-54
Technical Constraints
To prevent memory issues in long-running sessions, the PlotterState enforces an export_limit. When the number of points for a specific sensor exceeds this limit, the system automatically drains the oldest 10% of data.
Sources: src/plotter.rs:188-192
// snippet from src/export.rs:11-20
pub fn export_to_svg(
data: &HashMap<String, Vec<(f64, f64)>>,
filename: &str,
sensor_order: &[String],
plot_title: &str,
is_dark_mode: bool,
) -> Result<(), Box<dyn Error>> {
if data.is_empty() {
return Err("No data to export".into());
}
Sources: src/export.rs:11-20
Configuration Options
The SVG export behavior is influenced by several configuration parameters defined in the MergedConfig and Args structures.
| Option | Type | Default | Description |
|---|---|---|---|
export_limit | usize | 1,000,000 | Maximum points to keep in memory per sensor for export. |
plot_title | String | "Sensor Data" | The caption displayed at the top of the SVG. |
dark | bool | false | Enables dark mode theme for the exported file. |
| Sources: src/config.rs:105-110, src/config.rs:271-285 |
Conclusion
The SVG export capability in ComChan provides a bridge between real-time terminal monitoring and static reporting. By utilizing the plotters library and maintaining a dedicated memory-managed data buffer, the system ensures that high-resolution vector visualizations can be generated instantly without disrupting the live telemetry stream.
Sources: src/plotter.rs:334-355, src/export.rs:11-105