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
//! # Visualization
//!
//! # Visualization Library Usage Guide
//!
//! This guide explains how to use the visualization layer to create financial charts and other types of visualizations.
//!
//! ## Setup
//!
//! The chart data builds with no feature; rendering through plotly.rs needs
//! `plotly`, and PNG/SVG export needs `static_export`:
//!
//! ```toml
//! [dependencies]
//! optionstratlib-visualization = { version = "0.22.1", features = ["plotly"] }
//! ```
//!
//! ## Core Concepts
//!
//! The library follows a domain modeling pattern where it separates:
//!
//! 1. **What to visualize** (the data to represent)
//! 2. **How to visualize it** (styles and configuration)
//! 3. **Where to display it** (browser, HTML file, PNG image)
//!
//! ### Main Trait: `Graph`
//!
//! The `Graph` trait is the central component that any object wanting to be visualized must implement:
//!
//! ```rust,ignore,no_run
//! use optionstratlib_visualization::visualization::{GraphConfig, GraphData};
//!
//! pub trait Graph {
//! fn graph_data(&self) -> GraphData;
//!
//! fn graph_config(&self) -> GraphConfig {
//! GraphConfig::default()
//! }
//!
//! // Rendering methods provided under `plotly` and `static_export`...
//! }
//! ```
//!
//! To create a chart, you only need to implement:
//! - `graph_data()`: to provide the data to visualize
//! - Optionally, `graph_config()`: to customize the appearance
//!
//! The trait has this one definition on every feature surface; the backend
//! features only add provided rendering methods (`to_plot`, `write_html`,
//! `show`, `render`, `to_interactive_html` under `plotly`, and `write_png`,
//! `write_svg` under `static_export`), so an implementation written without
//! a backend compiles unchanged with one.
//!
//! ### Data Types
//!
//! The library supports these types of visualizations through the `GraphData` enum:
//!
//! ```rust
//! use optionstratlib_visualization::visualization::{MultiSeries2D, Series2D, Surface3D};
//!
//! pub enum GraphData {
//! Series(Series2D), // Line or scatter 2D
//! MultiSeries(MultiSeries2D), // Multiple 2D series
//! GraphSurface(Surface3D), // 3D surface
//! }
//! ```
//!
//! ## Example: Simple Line Chart
//!
//! ```rust,no_run
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use std::fs;
//! use std::path::{Path, PathBuf};
//! use rust_decimal::Decimal;
//! use rust_decimal_macros::dec;
//! use optionstratlib_visualization::visualization::{Graph, GraphData, Series2D, GraphConfig, OutputType};
//! use optionstratlib_visualization::visualization::{LineStyle, ColorScheme, TraceMode};
//!
//! struct MyData {
//! x: Vec<Decimal>,
//! y: Vec<Decimal>,
//! }
//!
//! impl Graph for MyData {
//! fn graph_data(&self) -> GraphData {
//! GraphData::Series(Series2D {
//! x: self.x.clone(),
//! y: self.y.clone(),
//! name: "My series".to_string(),
//! mode: TraceMode::Lines,
//! line_color: Some("#1f77b4".to_string()),
//! line_width: Some(2.0),
//! })
//! }
//!
//! fn graph_config(&self) -> GraphConfig {
//! GraphConfig {
//! title: "My chart".to_string(),
//! width: 800,
//! height: 600,
//! x_label: Some("X Axis".to_string()),
//! y_label: Some("Y Axis".to_string()),
//! z_label: None,
//! line_style: LineStyle::Solid,
//! color_scheme: ColorScheme::Viridis,
//! legend: None,
//! show_legend: false,
//! }
//! }
//! }
//! #[cfg(feature = "static_export")]
//! {
//! // Using the chart
//! let data = MyData {
//! x: vec![dec!(1.0), dec!(2.0), dec!(3.0), dec!(4.0), dec!(5.0)],
//! y: vec![dec!(2.0), dec!(3.0), dec!(5.0), dec!(7.0), dec!(11.0)],
//! };
//! // Display in browser
//! data.show();
//! // Save as PNG
//! let filename: PathBuf = PathBuf::from("my_chart.png");
//! data.render(OutputType::Png(&filename))?;
//! if Path::new(&filename.clone()).exists() {
//! fs::remove_file(filename.clone())?;
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Example: 3D Surface
//!
//! ```rust
//!
//! use rust_decimal::Decimal;
//! use optionstratlib_visualization::visualization::{ColorScheme, Graph, GraphConfig, GraphData, LineStyle, Surface3D};
//!
//! struct SurfaceData {
//! x: Vec<Decimal>,
//! y: Vec<Decimal>,
//! z: Vec<Decimal>,
//! }
//!
//! impl Graph for SurfaceData {
//! fn graph_data(&self) -> GraphData {
//! GraphData::GraphSurface(Surface3D {
//! x: self.x.clone(),
//! y: self.y.clone(),
//! z: self.z.clone(),
//! name: "My surface".to_string(),
//! })
//! }
//!
//! fn graph_config(&self) -> GraphConfig {
//! GraphConfig {
//! title: "3D Surface".to_string(),
//! width: 800,
//! height: 600,
//! x_label: Some("X".to_string()),
//! y_label: Some("Y".to_string()),
//! z_label: Some("Z".to_string()),
//! line_style: LineStyle::Solid,
//! color_scheme: ColorScheme::Plasma,
//! legend: None,
//! show_legend: false,
//! }
//! }
//! }
//! ```
//!
//! ## Color Schemes
//!
//! The library provides several predefined color schemes:
//!
//! - `ColorScheme::Default`: Uses plotly's default colors
//! - `ColorScheme::Viridis`: A color palette ranging from violet to yellow
//! - `ColorScheme::Plasma`: A color palette ranging from dark blue to yellow
//! - `ColorScheme::Custom(Vec<String>)`: Define your own colors as hexadecimal strings
//!
//! ## Line Styles
//!
//! You can customize lines with:
//!
//! - `LineStyle::Solid`: Continuous line
//! - `LineStyle::Dotted`: Dotted line
//! - `LineStyle::Dashed`: Dashed line
//!
//! ## Display Modes
//!
//! For 2D series, you can choose how to display points:
//!
//! - `TraceMode::Lines`: Lines only
//! - `TraceMode::Markers`: Markers (points) only
//! - `TraceMode::LinesMarkers`: Both lines and markers
//!
//! ## Error Handling
//!
//! The library provides a `GraphError` type that encapsulates graph-specific errors and I/O errors. All methods that can fail return `Result<(), GraphError>`.
//!
//! ## Advanced Tips
//!
//! ### Serialization and Deserialization
//!
//! All main types implement `serde::Serialize` and `serde::Deserialize`, allowing you to save and load chart configurations in JSON or other compatible formats.
//!
//! ### Multiple Series
//!
//! For charts with multiple series, use the `GraphData::MultiSeries` type:
//!
//! ```rust
//! use rust_decimal_macros::dec;
//! use optionstratlib_visualization::visualization::{GraphData, Series2D, TraceMode};
//! let series1 = Series2D {
//! x: vec![dec!(1.0), dec!(2.0), dec!(3.0)],
//! y: vec![dec!(4.0), dec!(5.0), dec!(6.0)],
//! name: "Series 1".to_string(),
//! mode: TraceMode::Lines,
//! line_color: Some("#1f77b4".to_string()),
//! line_width: Some(2.0),
//! };
//!
//! let series2 = Series2D {
//! x: vec![dec!(1.0), dec!(2.0), dec!(3.0)],
//! y: vec![dec!(7.0), dec!(8.0), dec!(9.0)],
//! name: "Series 2".to_string(),
//! mode: TraceMode::Markers,
//! line_color: Some("#ff7f0e".to_string()),
//! line_width: Some(2.0),
//! };
//!
//! let graph_data = GraphData::MultiSeries(vec![series1, series2]);
//! ```
//!
//! ### Interactive HTML Generation
//!
//! If you need interactive HTML with advanced hover and tooltip functions, use the `to_interactive_html` method:
//!
//! ```rust
//! use optionstratlib_visualization::visualization::{Graph, GraphData, Series2D, TraceMode, GraphConfig};
//! use optionstratlib_visualization::error::GraphError;
//! use std::path::PathBuf;
//! use tracing::info;
//! use rust_decimal_macros::dec;
//! struct SimpleChart {
//! series: Series2D
//! }
//!
//! impl Graph for SimpleChart {
//! fn graph_data(&self) -> GraphData {
//! GraphData::Series(self.series.clone())
//! }
//!
//! fn graph_config(&self) -> GraphConfig {
//! GraphConfig {
//! title: "Interactive Chart Example".into(),
//! width: 800,
//! height: 600,
//! x_label: Some("X Axis".into()),
//! y_label: Some("Y Axis".into()),
//! ..GraphConfig::default()
//! }
//! }
//! }
//!
//! fn main() -> Result<(), GraphError> {
//! use std::fs;
//! use std::path::Path;
//! let series = Series2D {
//! x: vec![dec!(1.0), dec!(2.0), dec!(3.0)],
//! y: vec![dec!(4.0), dec!(5.0), dec!(6.0)],
//! name: "Series 1".to_string(),
//! mode: TraceMode::Lines,
//! line_color: Some("#1f77b4".to_string()),
//! line_width: Some(2.0),
//! };
//!
//! #[cfg(feature = "static_export")]
//! {
//! let chart = SimpleChart { series };
//! let filename: PathBuf = PathBuf::from("interactive_chart.html");
//! chart.to_interactive_html(&filename)?;
//! info!("Interactive HTML chart created successfully!");
//! if Path::new(&filename.clone()).exists() {
//! fs::remove_file(filename.clone())?;
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! ## Complete Examples
//!
//! Check the `examples/` directory for practical examples, including:
//! - Stock price charts with moving averages
//! - Volatility surfaces for options
//! - Scatter plots with multiple series
//!
//! ## Adaptation to Your Needs
//!
//! `GraphData` is a closed enum, so a downstream crate cannot add variants to
//! it. To chart a new type, implement `Graph` for it: build one of the
//! existing `GraphData` variants in `graph_data()` and style it through
//! `graph_config()`. A new kind of chart (a variant, or a change to `to_plot`)
//! is a change to this crate.
//!
//! Enjoy visualizing your financial data!
/// `impl Graph` and `impl Plottable` for `Curve` and `Vec<Curve>`.
/// Filesystem preparation for chart export. Owned by `visualization`:
/// writing a rendered chart to disk is the only production use of it.
/// The `Graph` contract, one definition on every feature surface.
/// `Plottable` trait and `PlotBuilder`.
/// `impl Graph` and `impl Plottable` for `Surface`.
/// Terminal tables for chain and simulation reports: the explicit adapter
/// that writes them to stdout (M6-05).
/// Colour helpers, and the Plotly trace builders behind `plotly`.
pub use GraphConfig;
pub use prepare_file_path;
pub use Graph;
pub use GraphType;
pub use ;
pub use ;
pub use ;
pub use get_color_from_scheme;
pub use ;