openpine_vm/visuals/
plotshape.rs

1use openpine_compiler::instructions::Color;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    series::Series,
6    visuals::{Location, PlotDisplay, PlotFormat, Shape, Size},
7};
8
9/// A plotshape series.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct PlotShape {
12    /// Plot title (`None` when the caller did not supply one).
13    pub title: Option<String>,
14    pub(crate) series: Series<Option<f64>>,
15    /// Shape style.
16    pub style: Shape,
17    /// Placement location.
18    pub location: Location,
19    /// Per-bar color.
20    pub colors: Series<Option<Color>>,
21    /// Bar index offset.
22    pub offset: i32,
23    /// Optional text to display.
24    pub text: Option<String>,
25    /// Per-bar text color.
26    pub text_colors: Series<Option<Color>>,
27    /// Whether the plot is user-editable.
28    pub editable: bool,
29    /// Shape size.
30    pub size: Size,
31    /// Optional limit for the number of visible bars.
32    pub show_last: Option<usize>,
33    /// Display targets.
34    pub display: PlotDisplay,
35    /// Optional formatting hint.
36    pub format: Option<PlotFormat>,
37    /// Optional decimal precision.
38    pub precision: Option<i32>,
39    /// Whether to force overlay rendering.
40    pub force_overlay: bool,
41}
42
43impl PlotShape {
44    /// Returns the value at `index` interpreted as boolean.
45    #[inline]
46    pub fn bool_value(&self, index: usize) -> Option<bool> {
47        self.series[index].as_ref().map(|v| *v != 0.0)
48    }
49
50    /// Returns the value at `index` as float.
51    #[inline]
52    pub fn float_value(&self, index: usize) -> Option<f64> {
53        self.series[index]
54    }
55
56    pub(crate) fn append_new(&mut self) {
57        self.series.append_new();
58        self.colors.append_new();
59        self.text_colors.append_new();
60    }
61}