Skip to main content

Graph

pub trait Graph {
    // Required method
    fn graph_data(&self) -> GraphData;

    // Provided methods
    fn graph_config(&self) -> GraphConfig { ... }
    fn to_plot(&self) -> Plot { ... }
    fn write_png(&self, path: &Path) -> Result<(), GraphError> { ... }
    fn write_html(&self, path: &Path) -> Result<(), GraphError> { ... }
    fn write_svg(&self, path: &Path) -> Result<(), GraphError> { ... }
    fn show(&self) -> Result<(), GraphError> { ... }
    fn render(&self, output: OutputType<'_>) -> Result<(), GraphError> { ... }
    fn to_interactive_html(&self, path: &Path) -> Result<(), GraphError> { ... }
}
Expand description

Chart contract of every type the library can draw: strategies, option chains’ curves and surfaces, options, positions and simulations.

The required items are the same on every feature surface: implementors supply graph_data() and may override graph_config(). The backend features add provided renderers on top: to_plot, write_html, show, render and to_interactive_html under plotly, and write_png and write_svg under static_export. None of them needs to be implemented.

Required Methods§

Source

fn graph_data(&self) -> GraphData

Return the raw data ready for plotting.

Provided Methods§

Source

fn graph_config(&self) -> GraphConfig

Optional per‑object configuration overrides.

Source

fn to_plot(&self) -> Plot

Build a plotly::Plot according to data + config.

Source

fn write_png(&self, path: &Path) -> Result<(), GraphError>

Writes the chart to path as a PNG of graph_config().width x graph_config().height pixels, creating the parent directory when it is missing.

The image is drawn natively from Graph::graph_data and Graph::graph_config: no browser, WebDriver, network or installed font is involved, and concurrent exports, in threads or processes, are independent. Since 0.23 it no longer goes through Graph::to_plot, so an override of to_plot changes the HTML outputs but not this image.

§Errors
Source

fn write_html(&self, path: &Path) -> Result<(), GraphError>

Writes the graph data to an HTML file at the specified path.

This method generates a plot representation of the graph and saves it as an HTML document. It ensures that the provided file path is prepared (i.e., directories are created if necessary) before writing the file.

§Arguments
  • path - A reference to a std::path::Path specifying the file path where the HTML file will be written.
§Returns
  • Ok(()) if the HTML file is successfully written.
  • Err(GraphError) if an error occurs during file preparation or writing.
§Errors

This method can return the following errors:

  • A GraphError if the file path preparation fails.
  • Any other error propagated from the .to_plot().write_html() method.
§Notes

Ensure that the directory specified in the file path exists or can be created with appropriate permissions to avoid errors during file preparation.

Source

fn write_svg(&self, path: &Path) -> Result<(), GraphError>

Writes the chart to path as an SVG of graph_config().width x graph_config().height user units, creating the parent directory when it is missing.

Drawn natively like Graph::write_png, with real vector shapes. Every label is drawn as glyph outlines of the bundled Open Sans, so the file looks the same in every viewer and needs no font. Each label keeps its text as an accessible name (<g role="img" aria-label>) and the document carries a <title> and <desc>; the text can no longer be selected, searched or edited as text.

§Errors

Everything Graph::write_png returns, and GraphError::NotXmlText for a label, title or series name that contains a character XML cannot carry.

Source

fn show(&self) -> Result<(), GraphError>

Show the plot in browser

§Errors

Returns GraphError::Io when the page cannot be written to the system temp directory or the platform’s opener (open, xdg-open, explorer) cannot be started, as on a headless server, and GraphError::Render on a platform with no default opener.

Source

fn render(&self, output: OutputType<'_>) -> Result<(), GraphError>

One‑stop rendering with error propagation.

§Errors

A PNG or SVG target returns whatever Graph::write_png or Graph::write_svg returns, unchanged; in a build without static_export it is GraphError::Render. HTML and browser targets return GraphError::Render when serialization fails and GraphError::Io when the destination cannot be written.

Source

fn to_interactive_html(&self, path: &Path) -> Result<(), GraphError>

Generate interactive HTML with hover info + annotations.

§Errors

Propagates any GraphError returned by PlotlyChart::write_html, typically GraphError::Io when the target file cannot be created or written.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Graph for BearCallSpread

Source§

impl Graph for BearPutSpread

Source§

impl Graph for BullCallLadder

Source§

impl Graph for BullCallSpread

Source§

impl Graph for BullPutSpread

Source§

impl Graph for Collar

Source§

impl Graph for CoveredCall

Source§

impl Graph for Curve

Graph adapter for a single Curve; lives in visualization because the trait is visualization-owned and Curve is a math container (ADR-0001 D2, M1-05).

Source§

impl Graph for CustomStrategy

Source§

impl Graph for IronButterfly

Source§

impl Graph for IronCondor

Source§

impl Graph for LongButterflySpread

Source§

impl Graph for LongCall

Source§

impl Graph for LongPut

Source§

impl Graph for LongStraddle

Source§

impl Graph for LongStrangle

Source§

impl Graph for Options

Source§

impl Graph for PoorMansCoveredCall

Source§

impl Graph for Position

Implementation of the Graph trait for the Position struct, enabling graphical representation of financial options positions.

This implementation provides methods to visualize the profit/loss (PnL) profile of an options position across different price levels of the underlying asset. It handles the generation of appropriate title, data values for plotting, and special chart elements like break-even points.

The visualization capabilities allow traders to analyze the potential outcomes of their options positions at expiration across various price scenarios.

Source§

impl Graph for ProtectivePut

Source§

impl Graph for ShortButterflySpread

Source§

impl Graph for ShortCall

Source§

impl Graph for ShortPut

Source§

impl Graph for ShortStraddle

Source§

impl Graph for ShortStrangle

Source§

impl Graph for Surface

Graph adapter for a Surface; lives in visualization because the trait is visualization-owned and Surface is a math container (ADR-0001 D2, M1-05).

Source§

impl Graph for Vec<Curve>

Graph adapter for a family of curves drawn on one chart.

Source§

impl<X, Y> Graph for RandomWalk<X, Y>

Source§

impl<X, Y> Graph for Simulator<X, Y>

Implementors§