commit 482d3e36896349b9de41739cd9833ef0401e956a Author: Waozi Date: Sat Sep 26 18:25:07 2026 -0300 Add reusable Ziran charts and markers diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d64115d --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +.DEFAULT_GOAL := run + +.PHONY: run build check + +run build: + @$(MAKE) --silent --no-print-directory -C ../kryon build/bin/kryon + @../kryon/build/bin/kryon $@ + +check: + @$(MAKE) --silent --no-print-directory -C ../kryon build/bin/kryon + @../kryon/build/bin/kryon check + @mkdir -p build/indicator-test-c + @../ziran/build/bin/ziran build --target=c --entry indicator_test:main \ + --root tests --module-path src --module-path ../kryon/src/plot \ + --module-path ../kryon/src/ui -o build/indicator-test-c \ + tests/indicator_test.zi + @$(CC) -std=c99 -pedantic-errors -I../ziran/include \ + -Ibuild/indicator-test-c build/indicator-test-c/*.c -lm \ + -o build/indicator-test + @build/indicator-test diff --git a/README.md b/README.md new file mode 100644 index 0000000..e68a15d --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Plot + +Plot is a reusable Ziran chart project for Kryon applications. Its candle +chart draws OHLC bodies and wicks, volume, SMA 20, SMA 50, and EMA 20 over +Kryon's generic `Plot` surface. `ChartMarker` and `CandleMarkers` add dated, +clickable markers while leaving each marker's meaning to the caller. It also +has a simple line chart. Both accept +caller-owned values and colors; market data, networking, price formatting, +and time labels stay in the application. + +`make run` opens a small demo on the Cairo backend; `make check` checks the +Ziran source without opening a window. Use `src/candle_chart.zi` or +`src/line_chart.zi` from a project that already includes Kryon's UI modules. +Atr links these sources from its `src/` directory so its existing build rules +notice changes to Plot. diff --git a/kryon.toml b/kryon.toml new file mode 100644 index 0000000..c3340db --- /dev/null +++ b/kryon.toml @@ -0,0 +1,5 @@ +[project] +default_profile = "cairo" + +[profiles.cairo] +backend = "desktop" diff --git a/src/app.zi b/src/app.zi new file mode 100644 index 0000000..f0dfacb --- /dev/null +++ b/src/app.zi @@ -0,0 +1,54 @@ +#import "candle_chart" +#import "drawing_props" +#import "geometry" +#import "paint_queue" +#import "session" +#import "text_props" +#import "text_widget" + +#program_export +Frame :: (session: Session, viewport: Rectangle) -> s32 { + closes: [32]float32 = .{ + 42.0, 44.0, 43.0, 47.0, 45.0, 49.0, 48.0, 51.0, + 46.0, 48.0, 53.0, 52.0, 55.0, 54.0, 58.0, 57.0, + 56.0, 59.0, 61.0, 58.0, 60.0, 63.0, 62.0, 66.0, + 64.0, 67.0, 65.0, 68.0, 71.0, 69.0, 73.0, 72.0 + } + bars: [32]PriceBar + index: s32 = 0 + while index < 32 { + previous: float32 = closes[0] + if index > 0 { previous = closes[index - 1] } + bars[index].open = previous + bars[index].close = closes[index] + bars[index].high = previous + bars[index].low = previous + if closes[index] > previous { bars[index].high = closes[index] } + if closes[index] < previous { bars[index].low = closes[index] } + bars[index].high += 1.0 + bars[index].low -= 1.0 + bars[index].volume = cast(float32)(80 + index * 7) + index += 1 + } + title: TextProps + title.key = cast(u64)1 + title.text = "Plot" + title.font = 28 + title.bounds = Rectangle.{32.0, 24.0, viewport.width - 64.0, 48.0} + Text(session, title) + + chart: CandleChartProps + chart.key = cast(u64)2 + chart.bounds = Rectangle.{48.0, 104.0, + viewport.width - 96.0, viewport.height - 168.0} + chart.visible_count = 32 + chart.semantic_label = "Example candlestick chart with volume and SMA 20" + chart.up_color = ColorFromPacked(cast(u32)0x168265ff) + chart.down_color = ColorFromPacked(cast(u32)0xb34d53ff) + chart.grid_color = ColorFromPacked(cast(u32)0xe3eaf0ff) + chart.sma20_color = ColorFromPacked(cast(u32)0xd18a1bff) + chart.show_sma20 = true + values: []PriceBar = bars[:] + CandleChart(session, chart, values, CandleRangeFor(values, 32)) + return 0 +} diff --git a/src/candle_chart.zi b/src/candle_chart.zi new file mode 100644 index 0000000..b0626a5 --- /dev/null +++ b/src/candle_chart.zi @@ -0,0 +1,290 @@ +#import "drawing_props" +#import "button_props" +#import "button_widget" +#import "geometry" +#import "paint_queue" +#import "plot_props" +#import "plot_widget" +#import "session" + +using PlotMode; + +PriceBar :: struct { + timestamp: s64 + open: float32 + high: float32 + low: float32 + close: float32 + volume: float32 +} + +CandleRange :: struct { + low: float32 + high: float32 + min_value: float32 + max_value: float32 + max_volume: float32 +} + +CandleChartProps :: struct { + key: u64 + bounds: Rectangle + visible_count: s32 + semantic_label: string + up_color: Color + down_color: Color + grid_color: Color + sma20_color: Color + sma50_color: Color + ema20_color: Color + show_sma20: bool + show_sma50: bool + show_ema20: bool +} + +// Generic time markers. The chart caller owns the meaning of each marker. +ChartMarker :: struct { + timestamp: s64 + label: string + color: Color +} + +#program_export +CandleVisibleStart :: (count: s32, visible_count: s32) -> s32 { + if visible_count <= 0 || count <= visible_count { return 0 } + return count - visible_count +} + +#program_export +CandleRangeFor :: (bars: []PriceBar, visible_count: s32) -> CandleRange { + result: CandleRange + if bars.count == 0 { return result } + start: s32 = CandleVisibleStart(cast(s32)bars.count, visible_count) + result.low = bars[start].low + result.high = bars[start].high + index: s32 = start + while index < cast(s32)bars.count { + if bars[index].low < result.low { result.low = bars[index].low } + if bars[index].high > result.high { result.high = bars[index].high } + if bars[index].volume > result.max_volume { + result.max_volume = bars[index].volume + } + index += 1 + } + spread: float32 = result.high - result.low + if spread < 1.0 { spread = 1.0 } + padding: float32 = spread * 0.05 + result.min_value = result.low - padding + result.max_value = result.high + padding + return result +} + +#program_export +CandlePriceBounds :: (bounds: Rectangle) -> Rectangle { + result: Rectangle = bounds + result.height = bounds.height * 0.74 + return result +} + +#program_export +CandleVolumeBounds :: (bounds: Rectangle) -> Rectangle { + return Rectangle.{bounds.x, bounds.y + bounds.height * 0.79, + bounds.width, bounds.height * 0.18} +} + +CandleY :: (value: float32, bounds: Rectangle, range: CandleRange) -> float32 { + span: float32 = range.max_value - range.min_value + if span <= 0.0 { return bounds.y + bounds.height / 2.0 } + fraction: float32 = (range.max_value - value) / span + if fraction < 0.0 { fraction = 0.0 } + if fraction > 1.0 { fraction = 1.0 } + return bounds.y + fraction * bounds.height +} + +#program_export +SMAAt :: (bars: []PriceBar, index: s32, period: s32) -> float32 { + if period <= 0 || index < period - 1 || + index >= cast(s32)bars.count { return 0.0 } + total: float32 = 0.0 + cursor: s32 = index - period + 1 + while cursor <= index { + total += bars[cursor].close + cursor += 1 + } + return total / cast(float32)period +} + +#program_export +EMAAt :: (bars: []PriceBar, index: s32, period: s32) -> float32 { + if period <= 0 || index < period - 1 || + index >= cast(s32)bars.count { return 0.0 } + value: float32 = SMAAt(bars, period - 1, period) + alpha: float32 = 2.0 / cast(float32)(period + 1) + cursor: s32 = period + while cursor <= index { + value = alpha * bars[cursor].close + (1.0 - alpha) * value + cursor += 1 + } + return value +} + +DrawAverage :: (session: Session, props: CandleChartProps, + bars: []PriceBar, range: CandleRange, period: s32, + exponential: bool, color: Color) { + price: Rectangle = CandlePriceBounds(props.bounds) + start: s32 = CandleVisibleStart(cast(s32)bars.count, + props.visible_count) + count: s32 = cast(s32)bars.count - start + if count <= 0 { return } + step: float32 = price.width / cast(float32)count + previous: bool = false + previous_x: float32 = 0.0 + previous_y: float32 = 0.0 + index: s32 = start + while index < cast(s32)bars.count { + value: float32 = SMAAt(bars, index, period) + if exponential { value = EMAAt(bars, index, period) } + if value > 0.0 { + x: float32 = price.x + + (cast(float32)(index - start) + 0.5) * step + y: float32 = CandleY(value, price, range) + if previous { + PaintLine(session, -1, + Rectangle.{previous_x, previous_y, + x - previous_x, y - previous_y}, color) + } + previous_x = x + previous_y = y + previous = true + } + index += 1 + } +} + +#program_export +CandleChart :: (session: Session, props: CandleChartProps, + bars: []PriceBar, range: CandleRange) { + background: PlotProps + background.key = props.key + background.mode = cast(PlotMode)PlotLines + background.bounds = props.bounds + background.semantic_label = props.semantic_label + empty: [1]float32 + Plot(session, background, empty[0:0]) + if bars.count == 0 { return } + + price: Rectangle = CandlePriceBounds(props.bounds) + volume: Rectangle = CandleVolumeBounds(props.bounds) + grid_index: s32 = 0 + while grid_index < 5 { + fraction: float32 = cast(float32)grid_index / 4.0 + y: float32 = price.y + price.height * fraction + PaintLine(session, -1, + Rectangle.{price.x, y, price.width, 0.0}, props.grid_color) + grid_index += 1 + } + PaintLine(session, -1, + Rectangle.{volume.x, volume.y - 5.0, volume.width, 0.0}, + props.grid_color) + + start: s32 = CandleVisibleStart(cast(s32)bars.count, + props.visible_count) + count: s32 = cast(s32)bars.count - start + step: float32 = price.width / cast(float32)count + body_width: float32 = step * 0.63 + if body_width < 2.0 { body_width = 2.0 } + if body_width > 12.0 { body_width = 12.0 } + index: s32 = start + while index < cast(s32)bars.count { + bar: PriceBar = bars[index] + x: float32 = price.x + + (cast(float32)(index - start) + 0.5) * step + color: Color = props.up_color + if bar.close < bar.open { color = props.down_color } + wick_high: float32 = CandleY(bar.high, price, range) + wick_low: float32 = CandleY(bar.low, price, range) + PaintLine(session, -1, + Rectangle.{x, wick_high, 0.0, wick_low - wick_high}, color) + open_y: float32 = CandleY(bar.open, price, range) + close_y: float32 = CandleY(bar.close, price, range) + top: float32 = open_y + bottom: float32 = close_y + if bottom < top { + top = close_y + bottom = open_y + } + body_height: float32 = bottom - top + if body_height < 2.0 { + top -= 1.0 + body_height = 2.0 + } + PaintRoundedFill(session, -1, + Rectangle.{x - body_width / 2.0, top, + body_width, body_height}, 0.0, 0, color) + if range.max_volume > 0.0 { + volume_height: float32 = volume.height * + bar.volume / range.max_volume + PaintRoundedFill(session, -1, + Rectangle.{x - body_width / 2.0, + volume.y + volume.height - volume_height, + body_width, volume_height}, 0.0, 0, color) + } + index += 1 + } + if props.show_sma20 { + DrawAverage(session, props, bars, range, 20, false, + props.sma20_color) + } + if props.show_sma50 { + DrawAverage(session, props, bars, range, 50, false, + props.sma50_color) + } + if props.show_ema20 { + DrawAverage(session, props, bars, range, 20, true, + props.ema20_color) + } +} + +#program_export +CandleMarkerX :: (bars: []PriceBar, visible_count: s32, + bounds: Rectangle, timestamp: s64) -> float32 { + if bars.count == 0 { return -1.0 } + start: s32 = CandleVisibleStart(cast(s32)bars.count, visible_count) + count: s32 = cast(s32)bars.count - start + index: s32 = start + while index < cast(s32)bars.count { + if bars[index].timestamp == timestamp { + return bounds.x + + (cast(float32)(index - start) + 0.5) * + bounds.width / cast(float32)count + } + index += 1 + } + return -1.0 +} + +// Returns the activated marker index, or -1. Markers are rendered after the +// candles so their hit targets stay independent of the price and news model. +#program_export +CandleMarkers :: (session: Session, props: CandleChartProps, + bars: []PriceBar, markers: []ChartMarker) -> s32 { + selected: s32 = -1 + price: Rectangle = CandlePriceBounds(props.bounds) + index: s32 = 0 + while index < cast(s32)markers.count { + x: float32 = CandleMarkerX(bars, props.visible_count, + price, markers[index].timestamp) + if x >= 0.0 { + PaintLine(session, -1, + Rectangle.{x, price.y + 22.0, 0.0, price.height - 22.0}, + markers[index].color) + marker: ButtonProps + marker.key = props.key + cast(u64)1000 + cast(u64)index + marker.bounds = Rectangle.{x - 11.0, price.y + 2.0, 22.0, 20.0} + marker.label = markers[index].label + if Button(session, marker) != 0 { selected = index } + } + index += 1 + } + return selected +} diff --git a/src/line_chart.zi b/src/line_chart.zi new file mode 100644 index 0000000..8413a13 --- /dev/null +++ b/src/line_chart.zi @@ -0,0 +1,98 @@ +#import "drawing_props" +#import "geometry" +#import "paint_queue" +#import "plot_props" +#import "plot_widget" +#import "session" + +using PlotMode; + +ChartRange :: struct { + low: float32 + high: float32 + min_value: float32 + max_value: float32 +} + +LineChartProps :: struct { + key: u64 + bounds: Rectangle + semantic_label: string + line_color: Color + grid_color: Color +} + +#program_export +ChartRangeFor :: (values: []float32) -> ChartRange { + result: ChartRange + if values.count == 0 { return result } + result.low = values[0] + result.high = values[0] + index: s32 = 1 + while index < cast(s32)values.count { + if values[index] < result.low { result.low = values[index] } + if values[index] > result.high { result.high = values[index] } + index += 1 + } + spread: float32 = result.high - result.low + if spread < 1.0 { spread = 1.0 } + padding: float32 = spread * 0.10 + result.min_value = result.low - padding + result.max_value = result.high + padding + return result +} + +#program_export +LineChart :: (session: Session, props: LineChartProps, + values: []float32, chart_range: ChartRange) { + background: PlotProps + background.key = props.key + background.mode = cast(PlotMode)PlotLines + background.bounds = props.bounds + background.semantic_label = props.semantic_label + Plot(session, background, values[0:0]) + if values.count == 0 { return } + + span: float32 = chart_range.max_value - chart_range.min_value + if span <= 0.0 { return } + grid_index: s32 = 0 + while grid_index < 5 { + fraction: float32 = cast(float32)grid_index / 4.0 + y: float32 = props.bounds.y + props.bounds.height * fraction + PaintLine(session, -1, + Rectangle.{props.bounds.x, y, props.bounds.width, 0.0}, + props.grid_color) + grid_index += 1 + } + + if values.count == 1 { + y: float32 = props.bounds.y + props.bounds.height * + (chart_range.max_value - values[0]) / span + PaintLine(session, -1, + Rectangle.{props.bounds.x, y, props.bounds.width, 0.0}, + props.line_color) + } else { + index: s32 = 1 + while index < cast(s32)values.count { + x0: float32 = props.bounds.x + props.bounds.width * + cast(float32)(index - 1) / cast(float32)(values.count - 1) + x1: float32 = props.bounds.x + props.bounds.width * + cast(float32)index / cast(float32)(values.count - 1) + y0: float32 = props.bounds.y + props.bounds.height * + (chart_range.max_value - values[index - 1]) / span + y1: float32 = props.bounds.y + props.bounds.height * + (chart_range.max_value - values[index]) / span + PaintLine(session, -1, + Rectangle.{x0, y0, x1 - x0, y1 - y0}, props.line_color) + PaintLine(session, -1, + Rectangle.{x0, y0 + 1.0, x1 - x0, y1 - y0}, + props.line_color) + index += 1 + } + } + last_y: float32 = props.bounds.y + props.bounds.height * + (chart_range.max_value - values[values.count - 1]) / span + PaintRoundedFill(session, -1, + Rectangle.{props.bounds.x + props.bounds.width - 4.0, + last_y - 4.0, 8.0, 8.0}, 4.0, 12, props.line_color) +} diff --git a/tests/indicator_test.zi b/tests/indicator_test.zi new file mode 100644 index 0000000..96196e5 --- /dev/null +++ b/tests/indicator_test.zi @@ -0,0 +1,36 @@ +#import "candle_chart" +#import "geometry" + +#program_export +main :: () -> s32 { + bars: [5]PriceBar + closes: [5]float32 = .{1.0, 2.0, 3.0, 10.0, 5.0} + index: s32 = 0 + while index < 5 { + bars[index].timestamp = 1000 + cast(s64)index * 86400 + bars[index].close = closes[index] + bars[index].high = closes[index] + bars[index].low = closes[index] + bars[index].volume = cast(float32)(index + 1) + index += 1 + } + values: []PriceBar = bars[:] + if SMAAt(values, 1, 3) != 0.0 { return 1 } + if SMAAt(values, 2, 3) != 2.0 { return 2 } + if SMAAt(values, 4, 3) != 6.0 { return 3 } + if EMAAt(values, 2, 3) != 2.0 { return 4 } + if EMAAt(values, 3, 3) != 6.0 { return 5 } + if EMAAt(values, 4, 3) != 5.5 { return 6 } + if CandleVisibleStart(5, 3) != 2 { return 7 } + range: CandleRange = CandleRangeFor(values, 3) + if range.low != 3.0 || range.high != 10.0 || + range.max_volume != 5.0 { return 8 } + bounds: Rectangle = Rectangle.{0.0, 0.0, 300.0, 100.0} + visible: float32 = CandleMarkerX(values, 3, bounds, + bars[3].timestamp) + missing: float32 = CandleMarkerX(values, 3, bounds, + bars[0].timestamp) + if visible != 150.0 { return 9 } + if missing != -1.0 { return 10 } + return 0 +}