This commit is contained in:
5684185+vsariola@users.noreply.github.com
2026-01-17 14:54:40 +02:00
parent 06a1fb6b52
commit 655d736149
5 changed files with 137 additions and 120 deletions

View File

@ -17,6 +17,7 @@ import (
type (
Plot struct {
origXlim, origYlim plotRange
fixedYLevel float32
xScale, yScale float32
xOffset float32
@ -41,10 +42,11 @@ type (
plotLogScale float32
)
func NewPlot(xlim, ylim plotRange) *Plot {
func NewPlot(xlim, ylim plotRange, fixedYLevel float32) *Plot {
return &Plot{
origXlim: xlim,
origYlim: ylim,
origXlim: xlim,
origYlim: ylim,
fixedYLevel: fixedYLevel,
}
}
@ -82,9 +84,11 @@ func (p *Plot) Layout(gtx C, data PlotDataFunc, xticks, yticks PlotTickFunc, cur
})
// draw cursor
paint.ColorOp{Color: style.CursorColor}.Add(gtx.Ops)
csx := plotPx(s.X).toScreen(xlim.toRelative(cursornx))
fillRect(gtx, clip.Rect{Min: image.Pt(csx, 0), Max: image.Pt(csx+1, s.Y)})
if cursornx == cursornx { // check for NaN
paint.ColorOp{Color: style.CursorColor}.Add(gtx.Ops)
csx := plotPx(s.X).toScreen(xlim.toRelative(cursornx))
fillRect(gtx, clip.Rect{Min: image.Pt(csx, 0), Max: image.Pt(csx+1, s.Y)})
}
// draw curves
for chn := range numchns {
@ -119,7 +123,9 @@ func (s plotPx) fromScreen(px int) plotRel { return plotRel(float32(px) /
func (s plotPx) fromScreenF32(px float32) plotRel { return plotRel(px / float32(s-1)) }
func (o *Plot) xlim() plotRange { return o.origXlim.scale(o.xScale).offset(o.xOffset) }
func (o *Plot) ylim() plotRange { return o.origYlim.scale(o.yScale) }
func (o *Plot) ylim() plotRange {
return o.origYlim.offset(-o.fixedYLevel).scale(o.yScale).offset(o.fixedYLevel)
}
func fillRect(gtx C, rect clip.Rect) {
stack := rect.Push(gtx.Ops)
@ -164,6 +170,8 @@ func (o *Plot) update(gtx C) {
num := o.ylim().fromRelative(plotPx(s.Y).fromScreenF32(e.Position.Y))
den := o.ylim().fromRelative(plotPx(s.Y).fromScreenF32(o.dragStartPoint.Y))
num -= o.fixedYLevel
den -= o.fixedYLevel
if l := math.Abs(float64(num / den)); l > 1e-3 && l < 1e3 {
o.yScale -= float32(math.Log(l))
o.yScale = min(max(o.yScale, -1e3), 1e3)