104 lines
5.2 KiB
TypeScript
104 lines
5.2 KiB
TypeScript
import * as App from "kryon/app";
|
|
import { StyleTokenColor } from "kryon/drawing_props";
|
|
import { ExampleBounds, InstallPlotStyles } from "kryon/examples/optional_scene_styles";
|
|
import { Rectangle } from "kryon/geometry";
|
|
import { Gap } from "kryon/layout";
|
|
import { Session } from "kryon/session";
|
|
import { ThemeMetricRole } from "kryon/theme";
|
|
import { Box } from "kryon/tsx/box";
|
|
import { Column } from "kryon/tsx/layout";
|
|
import { Text } from "kryon/tsx/text";
|
|
import { CandleChartProps, CandleRangeFor, ChartMarker, ChartPriceLevel, PriceBar } from "plot/Plot";
|
|
import { CandleChart, CandleMarkers, CandlePriceLevels } from "plot/tsx/charts";
|
|
|
|
|
|
// Demo domain data stays module-level so interactive marker and level
|
|
// selection never rebuilds the candles, markers or levels themselves.
|
|
let exampleBars: [PriceBar, PriceBar, PriceBar, PriceBar, PriceBar, PriceBar, PriceBar, PriceBar] = [
|
|
{timestamp: s64(1700000000), open: 2, high: 4, low: 1, close: 3, volume: 20},
|
|
{timestamp: s64(1700086400), open: 3, high: 6, low: 2, close: 5, volume: 40},
|
|
{timestamp: s64(1700172800), open: 4, high: 5, low: 3, close: 4, volume: 60},
|
|
{timestamp: s64(1700259200), open: 5, high: 9, low: 4, close: 8, volume: 80},
|
|
{timestamp: s64(1700345600), open: 4, high: 7, low: 3, close: 6, volume: 100},
|
|
{timestamp: s64(1700432000), open: 7, high: 10, low: 6, close: 9, volume: 120},
|
|
{timestamp: s64(1700518400), open: 6, high: 9, low: 5, close: 8, volume: 140},
|
|
{timestamp: s64(1700604800), open: 8, high: 12, low: 7, close: 11, volume: 160},
|
|
];
|
|
let exampleMarkers: [ChartMarker, ChartMarker] = [
|
|
{timestamp: s64(1700172800), label: "Earnings", color: {r: u8(35), g: u8(160), b: u8(90), a: u8(255)}},
|
|
{timestamp: s64(1700432000), label: "Split", color: {r: u8(70), g: u8(120), b: u8(220), a: u8(255)}},
|
|
];
|
|
let exampleLevels: [ChartPriceLevel, ChartPriceLevel] = [
|
|
{value: 6.0, label: "Entry", color: {r: u8(216), g: u8(150), b: u8(40), a: u8(255)}},
|
|
{value: 10.0, label: "Alert", color: {r: u8(200), g: u8(60), b: u8(60), a: u8(255)}},
|
|
];
|
|
let selectedMarker: s32 = s32(-1);
|
|
let selectedLevel: s32 = s32(-1);
|
|
|
|
function DrawExample(session: Session, bounds: Rectangle, key: u64): void {
|
|
const range = CandleRangeFor(nativeSlice(exampleBars), s32(8));
|
|
const chartProps: CandleChartProps = {key: key, bounds: bounds, visible_count: s32(8),
|
|
semantic_label: "Example candles, markers and levels",
|
|
up_color: {r: u8(35), g: u8(115), b: u8(92), a: u8(255)},
|
|
down_color: {r: u8(185), g: u8(65), b: u8(67), a: u8(255)},
|
|
grid_color: {r: u8(180), g: u8(200), b: u8(210), a: u8(150)}};
|
|
<CandleChart session={session} bars={nativeSlice(exampleBars)} range={range} props={chartProps}/>;
|
|
const marker = CandleMarkers({session: session, props: chartProps, bars: nativeSlice(exampleBars),
|
|
markers: nativeSlice(exampleMarkers)});
|
|
if (marker >= s32(0)) { selectedMarker = marker; }
|
|
const level = CandlePriceLevels({session: session, props: chartProps, range: range,
|
|
levels: nativeSlice(exampleLevels)});
|
|
if (level >= s32(0)) { selectedLevel = level; }
|
|
}
|
|
|
|
function MarkerStatus(): string {
|
|
let label = "Marker: none";
|
|
if (selectedMarker === s32(0)) { label = "Marker: Earnings"; }
|
|
if (selectedMarker === s32(1)) { label = "Marker: Split"; }
|
|
return label;
|
|
}
|
|
|
|
function LevelStatus(): string {
|
|
let label = "Level: none";
|
|
if (selectedLevel === s32(0)) { label = "Level: Entry"; }
|
|
if (selectedLevel === s32(1)) { label = "Level: Alert"; }
|
|
return label;
|
|
}
|
|
|
|
export function SetExampleOptions(_disabled: boolean, _reducedMotion: boolean): void {
|
|
}
|
|
|
|
export function Frame(frame: App.Frame): void {
|
|
const session: Session = {slot: frame.slot, epoch: frame.epoch};
|
|
const viewport = frame.viewport;
|
|
<Box session={session} props={{bounds: viewport, fill: StyleTokenColor("canvas")}}/>;
|
|
<Column session={session} props={{bounds: viewport, padding: Gap(ThemeMetricRole.ThemeMetricSpace6),
|
|
gap: Gap(ThemeMetricRole.ThemeMetricSpace4), gap_override: true}}>
|
|
<Text session={session} props={{bounds: {height: 36}, text: "CandleChart", size: s32(23)}}/>
|
|
</Column>;
|
|
DrawStandalone(session, viewport);
|
|
// Status bounds are explicit viewport coordinates; they stay below the
|
|
// fixed-height chart area and resize with the window without placeholder
|
|
// layout widgets.
|
|
const chartBounds = ExampleBounds(viewport);
|
|
const statusY = viewport.y + viewport.height - 132.0;
|
|
<Text session={session} props={{bounds: {x: chartBounds.x, y: statusY,
|
|
width: chartBounds.width, height: 26}, text: MarkerStatus(), size: s32(20)}}/>;
|
|
<Text session={session} props={{bounds: {x: chartBounds.x, y: statusY + 34.0,
|
|
width: chartBounds.width, height: 26}, text: LevelStatus(), size: s32(20)}}/>;
|
|
}
|
|
|
|
function DrawStandalone(session: Session, viewport: Rectangle): void {
|
|
InstallPlotStyles(StyleTokenColor("canvas").r < u8(128));
|
|
DrawExample(session, ExampleBounds(viewport), u64(0));
|
|
}
|
|
|
|
export function GalleryFrame(frame: App.Frame): void {
|
|
const session: Session = {slot: frame.slot, epoch: frame.epoch};
|
|
DrawExample(session, ExampleBounds(frame.viewport), u64(600));
|
|
}
|
|
|
|
export function main(): s32 {
|
|
return App.Run({title: "candlechart"}, Frame);
|
|
}
|