Plotly.NET Documentation

repository·dev·Indexed 21 days ago

https://github.com/plotly/plotly.net

An interactive charting library for .NET languages built on top of plotly.js. It provides multiple API layers, from high-level type-safe chart creation to low-level object manipulation. The library is distributed via NuGet through packages including Plotly.NET (core F# API), Plotly.NET.CSharp (idiomatic C# wrapper), Plotly.NET.Interactive (notebook extensions), and Plotly.NET.ImageExport (static image rendering). It supports high-performance data transport using encoded typed arrays for plotly.js >= 2.28.0.

Tokens
6.9K
Snippets
20
Records
23
Agent score
71%

What's inside Plotly.NET

  1. Use encoded arrays in the high-level Chart API

    dev

    Plotly.NET supports passing encoded arrays (optimized data structures) directly into high-level Chart.* constructors. To avoid confusion with plain array inputs, the API uses specific encoded-only overloads.

    Key Rules for Encoded Overloads:

    • Explicit Parameters: Encoded overloads use specific parameter names like xEncoded, yEncoded, or zEncoded.
    • No Mixing: An encoded overload will not expose the corresponding plain x, y, or z parameters in the same signature. This prevents ambiguity between plain and encoded data.
    • No Tuples: Encoded overloads do not support tuple-based or zipped inputs; they are designed for direct encoded array arguments.
    • Precedence: If both plain and encoded values are provided (e.g., via lower-level object manipulation), the encoded value takes precedence during serialization.

    Common Chart Patterns:

    • Scatter: Chart.Scatter(xEncoded, yEncoded, ?Mode, ?Name, ...)
    • Bar: Chart.Bar(valuesEncoded, ?keysEncoded, ?MultiWidthEncoded, ?Name, ...)
    • Heatmap: Chart.Heatmap(zEncoded, ?xEncoded, ?yEncoded, ?Name, ...)
    // Example intended usage pattern
    Chart.Scatter(xEncoded, yEncoded, Mode.Lines, "My Encoded Scatter")
  2. Rendering Plotly.NET charts in UI libraries

    dev

    Plotly.NET generates JSON that is consumed by plotly.js. Therefore, to render charts in a UI library, the environment must be able to execute JavaScript and display HTML.

    • Web-based UI (e.g., Blazor): Requires a way to display HTML/JS.
    • Desktop (Windows Forms / WPF): Can use a WebView control to display the interactive charts.
    • Static Rendering: If your UI cannot run JavaScript, use Plotly.NET.ImageExport on the backend to generate static images of the charts to be served to the UI.
  3. How Plotly.NET packages are structured

    dev

    Plotly.NET is composed of several layers to support different workflows:

    • Plotly.NET (Core): The foundation, written in F#. It provides high-level type-safe APIs (like Chart) and low-level chart object manipulation. It is interoperable with C#.
    • Plotly.NET.CSharp: A thin wrapper around the core API written in C#. It is designed to make the library more idiomatic and ergonomic for C# developers.
    • Plotly.NET.Interactive: Specialized for notebook environments to enable interactive rendering.
    • Plotly.NET.ImageExport: Used for programmatic conversion of charts into static images.
  4. Use Encoded Typed Arrays for faster data transport

    dev

    Plotly.NET supports the plotly.js >= 2.28.0 encoded data_array format. This format allows for faster transport of large trace data fields (like x, y, z, and error arrays) by using a base64 encoded payload instead of plain sequences.

    To use this feature, you can provide values via parallel ...Encoded optional parameters using the EncodedTypedArray type. If both a plain sequence (e.g., x) and an encoded version (e.g., XEncoded) are provided, the encoded version takes precedence during serialization.

    // Conceptual usage of encoded parameters
    // Note: Exact API syntax depends on the specific trace implementation
    Chart.Scatter
        |> Scatter.setXEncoded (EncodedTypedArray.ofFloat64Array(flatArray, shape = [| rows; cols |]))
        |> Chart.show
  5. How the Chart API extension mechanism works

    dev

    Plotly.NET uses a specific F# pattern to provide a unified Chart API across multiple files without requiring the user to change how they call methods.

    Each chart family (e.g., 2D, 3D, Map) is defined in its own module using the [<AutoOpen>] attribute and an [<Extension>] type named Chart. When these modules are opened, the compiler resolves the static members as extension methods on the base Plotly.NET.Chart type.

    This allows a developer to call Chart.Scatter(...) or Chart.Surface(...) uniformly, even though the underlying implementation resides in different physical files (like Chart2D.fs or Chart3D.fs).

    [<AutoOpen>]
    module Chart2D =
        [<Extension>]
        type Chart =
            [<Extension>]
            static member Scatter(...) = ...
  6. Quick start with C#

    dev

    To use Plotly.NET in C# projects, add the Plotly.NET.CSharp package.

    In Polyglot Notebooks

    Reference both Plotly.NET.Interactive and Plotly.NET.CSharp using the #r "nuget: ..." syntax. Use the Plotly.NET.CSharp namespace to access the idiomatic API.

    #r "nuget: Plotly.NET.Interactive"
    #r "nuget: Plotly.NET.CSharp"
    
    using Plotly.NET.CSharp;
    
    Chart.Point<int, int, string>(
        x: Enumerable.Range(0,11),
        y: Enumerable.Range(0,11)
    )
  7. Install Plotly.NET via NuGet

    dev

    Plotly.NET is distributed via NuGet. Depending on your language and requirements, you should install one or more of the following packages:

    • Plotly.NET: The core API (written in F#). Required for all users.
    • Plotly.NET.CSharp: Provides an idiomatic C# API to reduce friction when using the core library in C# projects.
    • Plotly.NET.Interactive: Provides interactive formatting extensions for .NET interactive notebooks (like Polyglot Notebooks).
    • Plotly.NET.ImageExport: Provides extensions to render charts as static images programmatically.
    | Package Name| Nuget |
    |---|---|
    | Plotly.NET | [Plotly.NET](https://www.nuget.org/packages/Plotly.NET/) |
    | Plotly.NET.Interactive | [Plotly.NET.Interactive](https://www.nuget.org/packages/Plotly.NET.Interactive/) |
    | Plotly.NET.ImageExport | [Plotly.NET.ImageExport](https://www.nuget.org/packages/Plotly.NET.ImageExport/) |
    | Plotly.NET.CSharp | [Plotly.NET.CSharp](https://www.nuget.org/packages/Plotly.NET.CSharp/) |
  8. Configure encoded dimensions for Splom charts

    dev

    For Scatter Plot Matrix (Splom) charts, encoded support is managed at the Dimension level rather than through a single massive chart-level signature. This allows you to maintain the standard Chart.Splom(dimensions, ...) workflow while using encoded data.

    To use encoded data in a Splom chart:

    1. Create dimensions using Dimension.initSplom and provide the ValuesEncoded argument.
    2. Pass these dimensions to Chart.Splom.

    Alternatively, a high-level convenience overload is available:

    • Chart.Splom(keyValuesEncoded = seq<string * EncodedTypedArray>, ...)

    This ensures that the underlying dimensions[i].values field in the serialized JSON contains the encoded bdata structure.

    // Using the high-level convenience overload
    Chart.Splom(keyValuesEncoded = [ "DimensionA", encodedArray1; "DimensionB", encodedArray2 ])
    
    // OR using the Dimension-based approach
    let dim1 = Dimension.initSplom(Label = "A", ValuesEncoded = encodedArray1)
    let dim2 = Dimension.initSplom(Label = "B", ValuesEncoded = encodedArray2)
    Chart.Splom(dimensions = [ dim1; dim2 ])
  9. Quick start with F#

    dev

    To use Plotly.NET in F# projects, add the Plotly.NET package.

    In Polyglot Notebooks

    Reference the Plotly.NET.Interactive package using the #r "nuget: ..." syntax. To display a chart, ensure the cell ends with the chart object.

    In .fsx Scripts

    Reference the Plotly.NET package. To display a chart in your browser, pipe the chart object to Chart.show.

    // Polyglot Notebook
    #r "nuget: Plotly.NET.Interactive"
    
    open Plotly.NET
    
    Chart.Point(
        x = [0 .. 10],
        y = [0 .. 10]
    )
    |> Chart.withTitle "Hello World!"
    
    // .fsx Script
    #r "nuget: Plotly.NET"
    
    open Plotly.NET
    
    Chart.Point(
        x = [0 .. 10],
        y = [0 .. 10]
    )
    |> Chart.withTitle "Hello World!"
    |> Chart.show
  10. Reproduce the Plotly.NET package locally

    dev

    To reproduce the package for local testing or development, run the ./Repack.ps1 script in PowerShell. This script performs the following actions:

    1. Cleans your local NuGet cache in ~/.nuget/packages.
    2. Packs the library into the Plotly.NET/pkg folder.

    Note: The version of the reproduced package is always 0.0.0-dev.

    ./Repack.ps1
  11. Use local NuGet packages in an Interactive Notebook

    dev

    When working with locally reproduced packages in an interactive notebook (like Polyglot Notebooks), you must use absolute paths in the #i directive to point to the directory containing your local NuGet packages. Use the #r directive to reference specific packages and versions.

    Example configuration:

    • Use #i "nuget: <ABSOLUTE_PATH_TO_PKG_FOLDER>" to initialize the local package source.
    • Use #r "nuget: <PackageName>, <Version>" to load the specific libraries.
    // Use absolute paths for local nuget packages
    #i "nuget: C:/Users/schne/source/repos/plotly/Plotly.NET/pkg"
    #r "nuget: Plotly.NET, 0.0.1-dev"
    #r "nuget: Plotly.NET.Interactive, 0.0.1-dev"
  12. Configure Surface plot axes and export options

    dev

    After creating a chart, you can use the following methods to refine its properties:

    • Chart.withZAxisStyle(name, MinMax): Sets the name of the Z-axis and defines its minimum and maximum range.
    • Chart.withConfig(config): Updates the chart's configuration. For example, you can change the default export format of the 'Download plot' button using ConfigObjects.ToImageButtonOptions.init(Format=...).