A bundle that shows values against a good range could not draw a band chart, because the chart measures and paints with Kryon's native modules. The chart's records move to band_chart_props, a bundle built with KRYON_HOSTED_UI submits the chart as BandChartHostedHost, and a host answers it with BandChartHostCall from band_chart_host, which decodes the records with a codec generated on top of Kryon's widget codec. The check builds the bundles as ZX, which the toolchain now runs.
150 lines
5.2 KiB
Ziran
150 lines
5.2 KiB
Ziran
#import "kryon/session"
|
|
test_session: Session;
|
|
TestSession :: () -> Session {
|
|
if !SessionValid(test_session) { test_session = SessionOpen() }
|
|
return test_session
|
|
}
|
|
|
|
#import "kryon/geometry"
|
|
#import "kryon/paint_queue"
|
|
#import "plot"
|
|
#import "plot_props"
|
|
#import "plot_widget"
|
|
#import "band_chart"
|
|
#import "band_chart_props"
|
|
#import "kryon/tree"
|
|
#import "kryon/widget_kind"
|
|
|
|
using PlotMode;
|
|
using WidgetKind;
|
|
|
|
phase: s32;
|
|
|
|
#program_export
|
|
Frame :: () -> s32 {
|
|
lines: [3]float32
|
|
lines[0] = 0.0
|
|
lines[1] = 0.5
|
|
lines[2] = 1.0
|
|
bars: [3]float32
|
|
bars[0] = 0.25
|
|
bars[1] = 0.5
|
|
bars[2] = 1.0
|
|
automatic: PlotProps
|
|
observed: PlotRange = PlotObservedRange(bars[0:3], -1,
|
|
automatic)
|
|
if observed.min_value != 0.25 ||
|
|
observed.max_value != 1.0 ||
|
|
observed.range != 0.75 { return -2 }
|
|
if PlotOffset(0, 5) != 0 || PlotOffset(4, 6) != 2 ||
|
|
PlotOffset(4, -1) != 3 || PlotOffset(4, -6) != 2 ||
|
|
PlotOffset(1, 99) != 0 { return -3 }
|
|
fixed: PlotRange = PlotRangeFor(0.0, 10.0, -1.0, 20.0)
|
|
if fixed.min_value != 0.0 || fixed.max_value != 10.0 ||
|
|
fixed.range != 10.0 || PlotNormalize(5.0, fixed) != 0.5 ||
|
|
PlotNormalize(-5.0, fixed) != 0.0 ||
|
|
PlotNormalize(50.0, fixed) != 1.0 { return -4 }
|
|
singleton: PlotRange = PlotRangeFor(10.0, 5.0, 3.0, 3.0)
|
|
if singleton.min_value != 2.5 ||
|
|
singleton.max_value != 3.5 ||
|
|
singleton.range != 1.0 { return -5 }
|
|
line_props: PlotProps
|
|
line_props.key = cast(u64)11
|
|
line_props.bounds = Rectangle.{10.0, 10.0, 100.0, 60.0}
|
|
line_props.label = "Trend"
|
|
line_props.semantic_label = "XMR price trend"
|
|
line_props.overlay = "Now"
|
|
line_props.scale_min = 0.0
|
|
line_props.scale_max = 1.0
|
|
bar_props: PlotProps
|
|
bar_props.key = cast(u64)12
|
|
bar_props.bounds = Rectangle.{10.0, 90.0, 100.0, 60.0}
|
|
bar_props.label = "Bars"
|
|
bar_props.overlay = "Peak"
|
|
bar_props.offset = 1
|
|
bar_props.scale_min = 0.0
|
|
bar_props.scale_max = 1.0
|
|
bar_props.mode = cast(PlotMode)PlotBars
|
|
TreeStart(TestSession(), cast(u64)1, Rectangle.{0.0, 0.0, 140.0, 180.0})
|
|
if Plot(TestSession(), line_props, lines[0:3]) != 1 ||
|
|
Plot(TestSession(), bar_props, bars[0:3]) != 2 ||
|
|
!TreeFinish(TestSession()) || TreeCount(TestSession()) != 3 ||
|
|
TreeNodeAt(TestSession(), 1).kind != WidgetKindPlot ||
|
|
TreeNodeAt(TestSession(), 2).kind != WidgetKindPlot ||
|
|
TreeNodeAt(TestSession(), 1).semantic_label != "XMR price trend" ||
|
|
TreeNodeAt(TestSession(), 2).semantic_label != "Bars" { return -1 }
|
|
PaintFlush(TestSession())
|
|
old: s32 = phase
|
|
phase += 1
|
|
return old
|
|
}
|
|
|
|
#import "plot_widget_host"
|
|
#import "line_chart"
|
|
|
|
// This week over last week: five grid lines, then two segments drawn twice
|
|
// for each series.
|
|
CompareLines :: () -> s32 {
|
|
current: [3]float32 = .[1.0, 2.0, 3.0]
|
|
previous: [3]float32 = .[4.0, 2.0, 2.0]
|
|
both: ChartRange = ChartRangeForBoth(current[:], previous[:])
|
|
if both.low != 1.0 || both.high != 4.0 { return 3 }
|
|
chart: LineChartProps
|
|
chart.key = cast(u64)21
|
|
chart.bounds = Rectangle.{10.0, 10.0, 100.0, 60.0}
|
|
chart.semantic_label = "Visits this week and last week"
|
|
before: s32 = PlotLineCount()
|
|
TreeStart(TestSession(), cast(u64)2, Rectangle.{0.0, 0.0, 140.0, 180.0})
|
|
LineChartCompare(TestSession(), chart, current[:], previous[:], both)
|
|
if !TreeFinish(TestSession()) { return 4 }
|
|
PaintFlush(TestSession())
|
|
if PlotLineCount() - before != 5 + 4 + 4 { return 5 }
|
|
return 0
|
|
}
|
|
|
|
// The band chart's scale, slots and the point a pointer shows.
|
|
BandChecks :: () -> s32 {
|
|
props: BandChartProps
|
|
props.bounds = Rectangle.{0.0, 0.0, 312.0, 140.0}
|
|
props.slots = 30
|
|
props.min_value = 0.0
|
|
props.max_value = 10.0
|
|
props.top_label = "10"
|
|
props.bottom_label = "0"
|
|
props.font = 14
|
|
plot: Rectangle = BandChartPlotBounds(props)
|
|
if plot.x != 28.0 || plot.y != 10.0 || plot.width != 276.0 ||
|
|
plot.height != 120.0 { return 10 }
|
|
if BandChartY(plot, props, 10.0) != 10.0 || BandChartY(plot, props, 0.0) != 130.0 ||
|
|
BandChartY(plot, props, 5.0) != 70.0 || BandChartY(plot, props, 20.0) != 10.0 {
|
|
return 11
|
|
}
|
|
first: float32 = BandChartSlotX(plot, props, 0)
|
|
if first < 32.5 || first > 32.7 { return 12 }
|
|
points: [3]BandChartPoint
|
|
points[0] = BandChartPoint.{.slot = 0, .value = 2.0, .good = 1}
|
|
points[1] = BandChartPoint.{.slot = 10, .value = 5.0, .good = 0}
|
|
points[2] = BandChartPoint.{.slot = 29, .value = 9.0, .good = -1}
|
|
if BandChartNearest(props, points[:], BandChartSlotX(plot, props, 10) + 2.0, 20.0) != 1 ||
|
|
BandChartNearest(props, points[:], 300.0, 120.0) != 2 ||
|
|
BandChartNearest(props, points[0:0], 50.0, 50.0) != -1 { return 13 }
|
|
return 0
|
|
}
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
band := BandChecks()
|
|
if band != 0 { return band }
|
|
index: s32 = 0
|
|
while index < 2 {
|
|
if Frame() != index || PlotHostFailed() { return 1 }
|
|
if PlotBackgrounds() != (index + 1) * 2 ||
|
|
PlotMarks() != (index + 1) * 3 ||
|
|
PlotOutlines() != (index + 1) * 2 ||
|
|
PlotLineCount() != (index + 1) * 2 ||
|
|
PlotLabels() != (index + 1) * 4 { return 2 }
|
|
index += 1
|
|
}
|
|
return CompareLines()
|
|
}
|