Script Management
The ScriptManagement<T> trait defines the complete script lifecycle API for ChartView. It covers adding, removing, querying, and configuring scripts.
T is a user-defined tag type attached to each script — use String for script names, a UUID, or any type implementing Clone + PartialEq + Display.
Tags must be unique across all scripts on the same chart. set_scripts uses tags to diff the old and new lists: scripts whose tag appears in both lists are kept as-is (no recomputation); scripts only in the new list are added; scripts only in the old list are removed. If two scripts share the same tag the diff is undefined. Use a name, a UUID, or any value that is distinct per script.
Import
use navi_chart::{ChartView, ScriptManagement};Adding Scripts
add_script is part of ScriptManagement<T>, taking a value of type Script — the associated script handle supplied by the provider (P::Script):
// LocalChartProvider — Script = String (source code)
let id = cv.add_script(source.to_string(), "RSI".to_string());
// Custom ChartProvider — Script is whatever type P::Script resolves to
let id = cv.add_script(script_handle, "RSI".to_string());Use add_script_with_complete to be notified when the initial replay finishes:
let id = cv.add_script_with_complete(source, "RSI".to_string(), |result| {
match result {
Ok(()) => println!("RSI loaded"),
Err(e) => eprintln!("RSI error: {e}"),
}
});Adding a script triggers a session restart — all scripts replay from bar 0 with the updated list.
Batch Replace
To replace all scripts at once, use set_scripts. When a stream is already running it diffs the new list against the current scripts by tag — keeping unchanged scripts in place, adding new ones, and removing deleted ones — without restarting the stream. Only newly added scripts are recomputed. If no stream is running yet, a single stream start covers all scripts at once.
let ids = cv.set_scripts(vec![
(source1, "RSI".to_string()),
(source2, "MACD".to_string()),
]);Use set_scripts_with_complete to be notified when the initial replay finishes:
let ids = cv.set_scripts_with_complete(
vec![(source1, "RSI".to_string()), (source2, "MACD".to_string())],
|result| match result {
Ok(()) => println!("all loaded"),
Err(e) => eprintln!("error: {e}"),
},
);The callback fires once — with the first error from any script, or Ok(()) when all scripts complete successfully.
Passing an empty Vec is equivalent to remove_all_scripts.
Generic Code
Because add_script is in the trait, you can write generic functions that add scripts to any chart view:
use navi_chart::{ScriptManagement, ScriptId};
fn add_and_track<C>(cv: &C, source: C::Script, tag: String) -> ScriptId
where
C: ScriptManagement<String>,
{
cv.add_script(source, tag)
}Method Reference
| Method | Description |
|---|---|
add_script(script, tag) | Add a script and return its [ScriptId]. Triggers a session restart. |
add_script_with_complete(script, tag, cb) | Like add_script but fires cb when the initial replay finishes. |
set_scripts(scripts) | Replace all scripts with a single stream restart. Returns Vec<ScriptId> in input order. |
set_scripts_with_complete(scripts, cb) | Like set_scripts but fires cb once when all scripts finish or on first error. |
remove_script(id) | Remove a script by ScriptId. Frees all resources and re-renders. |
remove_all_scripts() | Remove every script from the chart. |
set_script_config(id, config) | Apply a ScriptConfig (input overrides + strategy config overrides + visual overrides). Input or strategy config changes trigger a session restart; visual-only changes re-render immediately. |
strategy_report(id) | Return the strategy report, or None if the script is not a strategy or hasn't produced output yet. |
scripts() | Return all scripts as (tag, id) pairs, in insertion order. |
has_script_tag(tag) | Return true if a script with the given tag is present. |
script_id_for_tag(tag) | Look up a ScriptId by tag. |
script_config(id) | Return the current ScriptConfig for a script. |
graph_configs(id) | Return the original (pre-override) visual configuration per series graph. |
script_overrides(id) | Return the current visual overrides per series graph. |
script_inputs(id) | Return the script's input definitions with their default values. |
script_error(id) | Return the last compilation or runtime error, if any. |
tag_for(id) | Reverse-lookup: return the tag for a given ScriptId. |
Notes
ScriptIdis opaque and non-zero. Store it if you need to configure or remove the script later; usetag_forto recover the tag from an event.
Persistence
The built-in save / load APIs return a ChartSnapshot<T, P::Script> generic over both the tag type and the provider's script handle:
let snapshot = cv.save(); // ChartSnapshot<String, P::Script>
let json = serde_json::to_string(&snapshot).unwrap();
// ...
cv.load(serde_json::from_str(&json).unwrap());See ChartView — Persistence for the full save/load flows. For the ScriptConfig data model and the configuration UI tutorial, see Script Configuration UI.