The plot surface moves here from Kryon, which no longer ships it. Plot exports PlotWidget for apps that draw their own series next to the Plot charts, and every Kryon import names its package (kryon/Kryon, kryon/session, ...). The widget's behavior test runs as a portable bundle and in Go, binding Kryon's host capabilities by import path.
168 lines
5.0 KiB
Ziran
168 lines
5.0 KiB
Ziran
#import "kryon/control_props"
|
|
#import "kryon/drawing_props"
|
|
#import "kryon/geometry"
|
|
#import "kryon/style"
|
|
|
|
using StyleField;
|
|
|
|
PlotRange :: struct {
|
|
min_value: float32
|
|
max_value: float32
|
|
range: float32
|
|
}
|
|
|
|
PlotMark :: struct {
|
|
bounds: Rectangle
|
|
color: u32
|
|
}
|
|
|
|
PlotTextPaint :: struct {
|
|
label_bounds: Rectangle
|
|
overlay_bounds: Rectangle
|
|
text_color: u32
|
|
show_label: bool
|
|
show_overlay: bool
|
|
}
|
|
|
|
PlotMetric :: (fields: u32, field: u32, value: float32,
|
|
fallback: float32, scale: float32) -> float32 {
|
|
if (fields & field) == cast(u32)0 || value < 0.0 { value = fallback }
|
|
return value * scale
|
|
}
|
|
|
|
#program_export
|
|
PlotOffset :: (count: s32, offset: s32) -> s32 {
|
|
if count <= 0 {
|
|
return 0
|
|
}
|
|
wrapped: s32 = offset % count
|
|
if wrapped < 0 {
|
|
wrapped = wrapped + count
|
|
}
|
|
return wrapped
|
|
}
|
|
|
|
#program_export
|
|
PlotRangeFor :: (scale_min: float32, scale_max: float32,
|
|
observed_min: float32, observed_max: float32) -> PlotRange {
|
|
result: PlotRange
|
|
result.min_value = scale_min
|
|
result.max_value = scale_max
|
|
if result.min_value >= result.max_value {
|
|
result.min_value = observed_min
|
|
result.max_value = observed_max
|
|
}
|
|
if result.min_value == result.max_value {
|
|
result.min_value = result.min_value - 0.5
|
|
result.max_value = result.max_value + 0.5
|
|
}
|
|
result.range = result.max_value - result.min_value
|
|
if result.range <= 0.0 {
|
|
result.range = 1.0
|
|
}
|
|
return result
|
|
}
|
|
|
|
#program_export
|
|
PlotNormalize :: (value: float32, plot_range: PlotRange) -> float32 {
|
|
t: float32 = (value - plot_range.min_value) / plot_range.range
|
|
if t < 0.0 {
|
|
return 0.0
|
|
}
|
|
if t > 1.0 {
|
|
return 1.0
|
|
}
|
|
return t
|
|
}
|
|
|
|
#program_export
|
|
PlotHistogramBar :: (bounds: Rectangle, index: s32, count: s32,
|
|
value: float32, plot_range: PlotRange, mark_frame: StyleFrame) -> PlotMark {
|
|
mark: PlotMark
|
|
if count <= 0 {
|
|
mark.color = mark_frame.value.background
|
|
return mark
|
|
}
|
|
step: float32 = bounds.width / cast(float32)count
|
|
height: float32 = PlotNormalize(value, plot_range) * bounds.height
|
|
width: float32 = step - 2.0
|
|
if width < 1.0 {
|
|
width = step
|
|
}
|
|
mark.bounds.x = bounds.x + cast(float32)index * step + 1.0
|
|
mark.bounds.y = bounds.y + bounds.height - height
|
|
mark.bounds.width = width
|
|
mark.bounds.height = height
|
|
mark.color = mark_frame.value.background
|
|
return mark
|
|
}
|
|
|
|
#program_export
|
|
PlotSingleLine :: (bounds: Rectangle, value: float32,
|
|
plot_range: PlotRange, mark_frame: StyleFrame) -> PlotMark {
|
|
mark: PlotMark
|
|
y: float32 = bounds.y + (1.0 - PlotNormalize(value, plot_range)) * bounds.height
|
|
mark.bounds.x = bounds.x
|
|
mark.bounds.y = y
|
|
mark.bounds.width = bounds.width
|
|
mark.bounds.height = 0.0
|
|
mark.color = mark_frame.value.background
|
|
return mark
|
|
}
|
|
|
|
#program_export
|
|
PlotLineSegment :: (bounds: Rectangle, index: s32, count: s32,
|
|
a: float32, b: float32, plot_range: PlotRange, mark_frame: StyleFrame) -> PlotMark {
|
|
mark: PlotMark
|
|
if count <= 1 {
|
|
return PlotSingleLine(bounds, b, plot_range, mark_frame)
|
|
}
|
|
x1: float32 = bounds.x + cast(float32)(index - 1) * bounds.width / cast(float32)(count - 1)
|
|
x2: float32 = bounds.x + cast(float32)index * bounds.width / cast(float32)(count - 1)
|
|
y1: float32 = bounds.y + (1.0 - PlotNormalize(a, plot_range)) * bounds.height
|
|
y2: float32 = bounds.y + (1.0 - PlotNormalize(b, plot_range)) * bounds.height
|
|
mark.bounds.x = x1
|
|
mark.bounds.y = y1
|
|
mark.bounds.width = x2 - x1
|
|
mark.bounds.height = y2 - y1
|
|
mark.color = mark_frame.value.background
|
|
return mark
|
|
}
|
|
|
|
#program_export
|
|
PlotTextPaintFor :: (bounds: Rectangle, label_width: float32,
|
|
overlay_width: float32, scale: float32, frame: StyleFrame,
|
|
show_label: bool, show_overlay: bool) -> PlotTextPaint {
|
|
paint: PlotTextPaint
|
|
if scale <= 0.0 {
|
|
scale = 1.0
|
|
}
|
|
pad_x: float32 = PlotMetric(frame.value.fields, cast(u32)StylePaddingX,
|
|
frame.value.padding_x, 6.0, scale)
|
|
pad_y: float32 = PlotMetric(frame.value.fields, cast(u32)StylePaddingY,
|
|
frame.value.padding_y, 4.0, scale)
|
|
text_height: float32 = PlotMetric(frame.value.fields, cast(u32)StyleFontSize,
|
|
frame.value.font_size, 18.0, scale)
|
|
if text_height <= 0.0 { text_height = 18.0 * scale }
|
|
paint.text_color = frame.value.foreground
|
|
paint.show_label = show_label
|
|
paint.show_overlay = show_overlay
|
|
paint.label_bounds.x = bounds.x + pad_x
|
|
paint.label_bounds.y = bounds.y + pad_y
|
|
paint.label_bounds.width = label_width
|
|
paint.label_bounds.height = text_height
|
|
paint.overlay_bounds.x = bounds.x + bounds.width - overlay_width - pad_x
|
|
paint.overlay_bounds.y = bounds.y + pad_y
|
|
paint.overlay_bounds.width = overlay_width
|
|
paint.overlay_bounds.height = text_height
|
|
if !show_label {
|
|
paint.label_bounds.width = 0.0
|
|
paint.label_bounds.height = 0.0
|
|
}
|
|
if !show_overlay {
|
|
paint.overlay_bounds.width = 0.0
|
|
paint.overlay_bounds.height = 0.0
|
|
}
|
|
return paint
|
|
}
|