NightVision Charting Library

repository·main·Indexed 43 days ago

https://github.com/project-nv/night-vision

A highly customizable charting library built with Svelte and designed for professional traders. A performance-improved continuation of TradingVueJS, version 0.4.0 provides a comprehensive API for managing chart panes, overlays, and data hubs, including a Layout API for geometric properties and a specialized Navy API for building high-performance custom overlays with optimized draw cycles.

Tokens
51K
Snippets
213
Records
303
Agent score
55%

What's inside NightVision

  1. What is NightVision?

    main

    NightVision is a highly customizable charting library designed for professional traders. It is a successor to the TradingVueJS project, designed to improve performance and design decisions while maintaining the core idea of using mapping functions and CanvasJS primitives to allow users to draw virtually any kind of graphics.

    Note: NightVision is currently in active development. The API is not yet finalized, but it is currently suitable for displaying candlestick and time-series data.

  2. What is DataScan and how to access it

    main

    DataScan (or Data Scanner) is a module within Night Vision used for analyzing the main dataset and tracking its changes. It is used to:

    • Detect the main interval (the time distance between data points).
    • Set the default time range.
    • Calculate hashes for panes and overlays to track changes.

    You can access the DataScan instance via the .scan property on a NightVision chart instance.

    let chart = new NightVision()
    console.log(chart.scan)
  3. What is DataHub and how to access it

    main

    DataHub is a container for the main dataset within a Night Vision chart. It manages the current visible subset of data (view), listens to internal update events (such as changes to overlay display flags), and provides helper functions for simplified data access. You can access the DataHub instance via the .hub property on a NightVision chart instance.

    let chart = new NightVision();
    const hub = chart.hub;
  4. What is a Grid in the Layout?

    main

    A Grid is a mathematical descriptor of a single pane within the chart. It acts as the main container for overlays and includes:

    • Scales: The x-axis and y-axis scales (defined inside the grid).
    • Dimensions: width and height of the pane.
    • Metadata: id, main (boolean indicating if it's the primary grid), offset, prec (precision), and pxStep.

    Grids allow the library to place components correctly and provide overlay scripts with the data needed to draw graphics.

  5. What is MetaHub and how to access it

    main

    MetaHub is a central repository for meta-information about panes and overlays. It stores properties that specify how elements should be rendered, such as legend formats, Y-transforms, precision samplers, and Y-range functions. The MetaHub listens to events emitted by layers and stores this information, which layout-building algorithms then use to adjust the output.

    You can access the MetaHub via the .meta property on your NightVision instance.

    let chart = new NightVision()
    console.log(chart.meta)
  6. What is a Scale in the Layout?

    main

    A Scale is a descriptor of the y-axis scale within a grid. It defines how data values (like price) are transformed into screen coordinates.

    Key properties include:

    • $hi and $lo: The high and low bounds of the scale.
    • $step: The scale step.
    • Transform Components: A and B used for the Y-transform calculation (A * y + B).
    • scaleSpecs: Configuration for the scale, such as id and whether it is a log scale.
    • prec: The precision used for calculations.
  7. Use the Overlay Environment Variables ($core, $props, $lib, $events)

    main

    Overlays have access to a specialized environment containing the current chart state.

    $core

    Contains core elements like data (full dataset), range, and view (visible range). It also provides layout for coordinate mapping.

    draw() {
        const layout = $core.layout
        const data = $core.data
        const view = $core.view
        // Example: mapping data to coordinates
        let x = layout.ti2x(p[0], i)
        let y = layout.value2y(p[idx])
    }

    $props

    Provides access to properties defined via prop(). If a property is passed via the props field of an OverlayObject, it overrides the script's default.

    prop('color', { type: 'color', def: '#2d7b2f33' })
    console.log($props.color) // #2d7b2f33

    $lib

    A collection of helper functions and primitives. For example, drawing candles:

    $lib.candleWick(ctx, candleDescriptor, layout)
    $lib.candleBody(ctx, candleDescriptor, layout)

    $events

    Used to interact with the MetaHub. For example, to lock or unlock scrolling:

    $events.emit('scroll-lock', true)  // Lock On
    $events.emit('scroll-lock', false) // Lock Off
    // Accessing $core in a draw loop
    draw() {
        const layout = $core.layout
        const data = $core.data
        const view = $core.view
        // ...
    }
  8. Understand the top-level dataset structure

    main

    The dataset serves as the central source of truth for the Night Vision library. It is an unreactive object designed for high performance; unlike frameworks like Vue, it does not use Proxies or hidden __ob__ objects, which helps avoid performance overhead when dealing with large nested datasets.

    To change the chart state, you must follow a two-step process:

    1. Modify the dataset object directly.
    2. Call the corresponding update method(s) to reflect those changes in the chart.

    Properties in the dataset are categorized by who is intended to manipulate them:

    • Human properties (marked with a human icon): Intended to be manipulated by the user.
    • Robot properties (marked with a robot icon): Intended to be manipulated by the library itself.
    {
        // Defines panes (vertical sections of the chart)
        panes: [
            {
                // Defines overlays (layers of graphics)
                overlays: [
                    {
                        name: "…", // Overlay name
                        type: "…", // Overlay type
                        data: […], // Time-series: [[<time>, value, …], …]
                        settings: {…}, // Overlay settings
                        props: {…}, // Overlay props
                        // ...
                    }
                ],
                settings: {…}, // Pane settings
            }
        ]
    }