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

One affine transform for a data layer: a scale followed by a translate, applied by the backend to a single group. Individual bar shapes stay byte-identical across zoom/pan/auto-scale because only this group changes.

pub type Affine {
  Affine(scale_x: Float, scale_y: Float, dx: Float, dy: Float)
}

Constructors

  • Affine(scale_x: Float, scale_y: Float, dx: Float, dy: Float)

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

The O(1) part of a view: constant-size axis/crosshair shapes plus the four affine transforms. Contains NO accumulated bar shapes.

A framework adapter keeps the already-translated shapes for each data layer in its Model, wraps them in one affine group per layer using these values, and never re-translates the accumulated list on every frame. This is the package side of the Elm lazy boundary: the bar shapes are built once at push time, the frame is the only cheap per-view input.

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

Constructors

The four shape groups generated for exactly one incoming bar. Exposed so an adapter can translate this one bar’s shapes at push time, instead of re-translating the whole accumulated list on every view.

pub type GeneratedShapes {
  GeneratedShapes(
    candle: SpecShape,
    overlays: List(SpecShape),
    volume: SpecShape,
    kdj: 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 frame(
  chart: Chart,
  vp: Viewport,
  mouse: option.Option(#(Float, Float)),
) -> Frame

The O(1) view data for the current viewport and mouse: constant axis and crosshair shapes plus one affine transform per data layer. This is the ONLY view-side input the package exposes for a growing chart — it never touches candle_shapes / overlay_shapes / volume_shapes / kdj_shapes, so its cost is independent of the bar count.

The adapter combines this frame with shapes it translated once at push time. On an empty chart the axes and crosshair are empty and the affines are identity.

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

Push one bar, discarding the per-bar shapes. Equivalent to the advanced chart from push_generating; kept for callers that only need state.

pub fn push_generating(
  chart: Chart,
  bar: Bar,
) -> #(Chart, GeneratedShapes)

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

Returns both the advanced chart and the four shape groups generated by exactly this bar. An adapter translates ONLY generated at push time and stores the result, so view never re-translates the accumulated list.

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

Assemble six layers from already-generated shapes, using the same O(1) affines as frame. Kept for callers that want SpecShapes directly; the main adapter uses frame + push-time translation instead.

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