Skip to main content

chartml_core/plugin/
renderer.rs

1use serde::{Deserialize, Serialize};
2
3use crate::data::DataTable;
4use crate::element::{ChartElement, Dimensions};
5use crate::error::ChartError;
6use crate::spec::VisualizeSpec;
7use crate::theme::Theme;
8
9/// Configuration passed to chart renderers.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ChartConfig {
12    /// The visualize spec from the parsed YAML.
13    pub visualize: VisualizeSpec,
14    /// Chart title (if any).
15    pub title: Option<String>,
16    /// Available width in pixels.
17    pub width: f64,
18    /// Available height in pixels.
19    pub height: f64,
20    /// Color palette to use.
21    pub colors: Vec<String>,
22    /// Theme colors for chart chrome (axes, grid, text).
23    /// Not serialized — set at render time by the consuming application.
24    #[serde(skip, default)]
25    pub theme: Theme,
26}
27
28/// Chart renderer plugin — converts data + config into a ChartElement tree.
29pub trait ChartRenderer: Send + Sync {
30    /// Render data with the given config into a ChartElement tree.
31    fn render(&self, data: &DataTable, config: &ChartConfig) -> Result<ChartElement, ChartError>;
32
33    /// Optional: provide default dimensions for this chart type.
34    fn default_dimensions(&self, _spec: &VisualizeSpec) -> Option<Dimensions> {
35        None
36    }
37}