bar_stream_chart

Incremental candlestick chart specification, independent of any GUI framework.

One push(chart, bar) is the only input. It generates that bar’s shapes at once and prepends them; shapes merely assembles already-generated shapes. No databar_list copy, no bars field, no batch input, no replay.

The output is a framework-neutral ChartShapes IR. A caller translates it into a backend (plushie canvas in this project). Colour semantics follow the Chinese market: up is red, down is green.

Types

The drawing input: one bar, with only the fields the chart consumes. No glib import - the caller converts its own bar into this value.

pub type Bar {
  Bar(
    idx: Int,
    open: Float,
    high: Float,
    low: Float,
    close: Float,
    volume: Int,
    sma_medium: Float,
    yin_leaf_cma: Float,
    yang_leaf_cma: Float,
    k: Float,
    d: Float,
    j: Float,
    bb_u3: Float,
    bb_l3: Float,
    signal: Signal,
  )
}

Constructors

  • Bar(
      idx: Int,
      open: Float,
      high: Float,
      low: Float,
      close: Float,
      volume: Int,
      sma_medium: Float,
      yin_leaf_cma: Float,
      yang_leaf_cma: Float,
      k: Float,
      d: Float,
      j: Float,
      bb_u3: Float,
      bb_l3: Float,
      signal: Signal,
    )

The O(1) carried live bounds as derived scalars. Not a model copy: these are extremum accumulators the view needs for the affine axis transform.

pub type BoundsSnapshot {
  BoundsSnapshot(
    min: Float,
    max: Float,
    vol_max: Float,
    kdj_min: Float,
    kdj_max: Float,
  )
}

Constructors

  • BoundsSnapshot(
      min: Float,
      max: Float,
      vol_max: Float,
      kdj_min: Float,
      kdj_max: Float,
    )

Chart

opaque

The chart’s drawing state. No bars list: it holds only generated shapes, carried O(1) bounds, and one geometric point set for the next connector.

pub opaque type Chart

The six canvas layers produced for one view.

pub type ChartShapes {
  ChartShapes(
    axes: List(SpecShape),
    candles: List(SpecShape),
    overlays: List(SpecShape),
    volume: List(SpecShape),
    kdj: List(SpecShape),
    crosshair: List(SpecShape),
  )
}

Constructors

Canvas dimensions, fixed at chart creation.

pub type Geometry {
  Geometry(width: Float, height: Float)
}

Constructors

  • Geometry(width: Float, height: Float)

A path command in the output IR.

pub type PathCommand {
  SpecMoveTo(x: Float, y: Float)
  SpecLineTo(x: Float, y: Float)
}

Constructors

  • SpecMoveTo(x: Float, y: Float)
  • SpecLineTo(x: Float, y: Float)

Price axis transform, chosen at chart birth.

pub type PriceScale {
  PriceScaleLinear
  PriceScaleLog
}

Constructors

  • PriceScaleLinear
  • PriceScaleLog

A stable identity for one generated shape, used by the backend’s diff.

pub type ShapeId {
  ShapeId(String)
}

Constructors

  • ShapeId(String)

The signal attached to a chart bar. Closed set: a new signal is a compile error in every marker generator.

pub type Signal {
  SignalBuy
  SignalSell
  SignalHold
  SignalCall
  SignalPut
  SignalWatch
}

Constructors

  • SignalBuy
  • SignalSell
  • SignalHold
  • SignalCall
  • SignalPut
  • SignalWatch

One generated shape, independent of any rendering framework.

pub type SpecShape {
  SpecRect(
    id: ShapeId,
    x: Float,
    y: Float,
    width: Float,
    height: Float,
    fill: String,
    opacity: option.Option(Float),
  )
  SpecPath(
    id: ShapeId,
    commands: List(PathCommand),
    stroke: Stroke,
  )
  SpecLine(
    id: ShapeId,
    x1: Float,
    y1: Float,
    x2: Float,
    y2: Float,
    stroke: Stroke,
  )
  SpecText(
    id: ShapeId,
    x: Float,
    y: Float,
    content: String,
    fill: String,
    size: Float,
  )
  SpecGroup(
    id: ShapeId,
    children: List(SpecShape),
    transforms: List(SpecTransform),
  )
  SpecInteractive(
    id: ShapeId,
    children: List(SpecShape),
    tooltip: String,
  )
}

Constructors

An affine transform on a group. One scale or one translate, applied by the backend; zoom/pan never touch individual bar shapes.

pub type SpecTransform {
  SpecScale(x: Float, y: Float)
  SpecTranslate(x: Float, y: Float)
}

Constructors

  • SpecScale(x: Float, y: Float)
  • SpecTranslate(x: Float, y: Float)

A stroke style in the output IR.

pub type Stroke {
  Stroke(color: String, width: Float)
}

Constructors

  • Stroke(color: String, width: Float)

Visible bar range. Zoom and pan change only these two Ints.

pub type Viewport {
  Viewport(first_visible: Int, visible_count: Int)
}

Constructors

  • Viewport(first_visible: Int, visible_count: Int)

Values

pub fn bounds(chart: Chart) -> BoundsSnapshot

Read the carried live bounds; empty charts report a neutral axis.

pub fn candle_shapes(chart: Chart) -> List(SpecShape)

The already-generated candle shapes, newest first. These are derived output, not a bar list; callers may compare them across pushes to assert that auto-scale never rewrites old bars.

pub fn clamp_viewport(vp: Viewport, total: Int) -> Viewport

Keep a viewport inside the data bounds.

pub fn default_viewport(total: Int) -> Viewport

Show the most-recent min(160, total) bars.

pub fn empty(geometry: Geometry, scale: PriceScale) -> Chart

An empty chart with fixed geometry and price scale. The first push sets the axes from that first bar; no bar is ever loaded in bulk.

pub fn latest_close(chart: Chart) -> option.Option(Float)

The latest close as a scalar; never exposes the whole bar.

pub fn len(chart: Chart) -> Int

The chart’s bar count, read from the carried counter, never a list length.

pub fn pan_by_pixels(
  vp: Viewport,
  total: Int,
  width: Float,
  dx_pixels: Float,
) -> Viewport

Pan by a horizontal mouse delta in pixels.

pub fn push(chart: Chart, bar: Bar) -> Chart

The only input: one bar in, that bar’s shapes generated and prepended.

pub fn shapes(
  chart: Chart,
  vp: Viewport,
  mouse: option.Option(#(Float, Float)),
) -> ChartShapes

Assemble six layers from already-generated shapes. Zoom/pan are one group transform per layer; no bar is revisited, removed or rebuilt.

pub fn zoom_at(
  vp: Viewport,
  total: Int,
  width: Float,
  delta_y: Float,
  cursor_x: Float,
) -> Viewport

Zoom around the cursor: delta_y < 0 zooms in, delta_y > 0 zooms out.

✨ Search Document