LineChartCompare draws an earlier series, such as last week, in compare_color under the current one on a single scale, so a chart shows at a glance whether this period beats the last. ChartRangeForBoth sizes that scale to fit both. LineChart now shares the card, grid, and series drawing with it.
117 lines
3.9 KiB
Ziran
117 lines
3.9 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 "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
|
|
}
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
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()
|
|
}
|