From 608bb80bc1f29b5632fcc12622902aec4241788f Mon Sep 17 00:00:00 2001 From: gucio321 Date: Tue, 25 Jun 2024 10:24:09 +0200 Subject: [PATCH 1/4] add SwitchPlotAxes function --- Plot.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Plot.go b/Plot.go index 64cfb4a8..dc2147b0 100644 --- a/Plot.go +++ b/Plot.go @@ -242,6 +242,26 @@ func (p *PlotCanvasWidget) Build() { } } +type ( + PlotXAxis = imgui.PlotAxisEnum + PlotYAxis = imgui.PlotAxisEnum +) + +const ( + AxisX1 = PlotXAxis(imgui.AxisX1) + AxisX2 = PlotXAxis(imgui.AxisX2) + AxisX3 = PlotXAxis(imgui.AxisX3) + AxisY1 = PlotYAxis(imgui.AxisY1) + AxisY2 = PlotYAxis(imgui.AxisY2) + AxisY3 = PlotYAxis(imgui.AxisY3) +) + +func SwitchPlotAxes(x PlotXAxis, y PlotYAxis) PlotWidget { + return Custom(func() { + imgui.PlotSetAxes(imgui.PlotAxisEnum(x), imgui.PlotAxisEnum(y)) + }) +} + // BarPlot adds bar plot (column chart) to the canvas. type BarPlot struct { title string @@ -251,7 +271,6 @@ type BarPlot struct { offset int } -// Bar adds a plot bar (column chart). func Bar(title string, data []float64) *BarPlot { return &BarPlot{ title: title, From 18bf177fd843f49c21abbe79385917f853ed28b4 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Tue, 25 Jun 2024 10:29:57 +0200 Subject: [PATCH 2/4] enable/disable 2nd and 3rd axis if needed --- Plot.go | 78 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/Plot.go b/Plot.go index dc2147b0..4b0966bc 100644 --- a/Plot.go +++ b/Plot.go @@ -6,6 +6,20 @@ import ( imgui "github.com/AllenDang/cimgui-go" ) +type ( + PlotXAxis = imgui.PlotAxisEnum + PlotYAxis = imgui.PlotAxisEnum +) + +const ( + AxisX1 = PlotXAxis(imgui.AxisX1) + AxisX2 = PlotXAxis(imgui.AxisX2) + AxisX3 = PlotXAxis(imgui.AxisX3) + AxisY1 = PlotYAxis(imgui.AxisY1) + AxisY2 = PlotYAxis(imgui.AxisY2) + AxisY3 = PlotYAxis(imgui.AxisY3) +) + // PlotWidget is implemented by all the particular plots, which can be used // in (*PlotCanvasWidget).Plots. type PlotWidget interface { @@ -75,6 +89,32 @@ func Plot(title string) *PlotCanvasWidget { } } +func (p *PlotCanvasWidget) SetXAxisLabel(axis PlotXAxis, label string) *PlotCanvasWidget { + switch axis { + case AxisX1: + p.xLabel = label + case AxisX2: + p.y2Label = label + case AxisX3: + p.y3Label = label + } + + return p +} + +func (p *PlotCanvasWidget) SetYAxisLabel(axis PlotYAxis, label string) *PlotCanvasWidget { + switch axis { + case AxisY1: + p.yLabel = label + case AxisY2: + p.y2Label = label + case AxisY3: + p.y3Label = label + } + + return p +} + // AxisLimits sets X and Y axis limits. func (p *PlotCanvasWidget) AxisLimits(xmin, xmax, ymin, ymax float64, cond ExecCondition) *PlotCanvasWidget { p.xMin = xmin @@ -222,17 +262,21 @@ func (p *PlotCanvasWidget) Build() { imgui.PlotAxisFlags(p.yFlags), ) - imgui.PlotSetupAxisV( - imgui.AxisY2, - Context.FontAtlas.RegisterString(p.y2Label), - imgui.PlotAxisFlags(p.y2Flags), - ) + if p.y2Label != "" { + imgui.PlotSetupAxisV( + imgui.AxisY2, + Context.FontAtlas.RegisterString(p.y2Label), + imgui.PlotAxisFlags(p.y2Flags), + ) + } - imgui.PlotSetupAxisV( - imgui.AxisY3, - Context.FontAtlas.RegisterString(p.y3Label), - imgui.PlotAxisFlags(p.y3Flags), - ) + if p.y3Label != "" { + imgui.PlotSetupAxisV( + imgui.AxisY3, + Context.FontAtlas.RegisterString(p.y3Label), + imgui.PlotAxisFlags(p.y3Flags), + ) + } for _, plot := range p.plots { plot.Plot() @@ -242,20 +286,6 @@ func (p *PlotCanvasWidget) Build() { } } -type ( - PlotXAxis = imgui.PlotAxisEnum - PlotYAxis = imgui.PlotAxisEnum -) - -const ( - AxisX1 = PlotXAxis(imgui.AxisX1) - AxisX2 = PlotXAxis(imgui.AxisX2) - AxisX3 = PlotXAxis(imgui.AxisX3) - AxisY1 = PlotYAxis(imgui.AxisY1) - AxisY2 = PlotYAxis(imgui.AxisY2) - AxisY3 = PlotYAxis(imgui.AxisY3) -) - func SwitchPlotAxes(x PlotXAxis, y PlotYAxis) PlotWidget { return Custom(func() { imgui.PlotSetAxes(imgui.PlotAxisEnum(x), imgui.PlotAxisEnum(y)) From d88ad85ace120e5cc069a168c427dcaf4aa5dfc9 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Tue, 25 Jun 2024 10:30:34 +0200 Subject: [PATCH 3/4] examples: update plot example --- examples/plot/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/plot/main.go b/examples/plot/main.go index 12faea76..6c44dd0c 100644 --- a/examples/plot/main.go +++ b/examples/plot/main.go @@ -29,8 +29,9 @@ func loop() { g.Plot("Plot 基本图表").AxisLimits(0, 100, -1.2, 1.2, g.ConditionOnce).XTicks(lineTicks, false).Plots( g.Line("Plot Line 线图", linedata), g.Line("Plot Line2", linedata2), + g.SwitchPlotAxes(g.AxisX1, g.AxisY2), g.Scatter("Scatter 散点图", scatterdata), - ), + ).SetYAxisLabel(g.AxisY2, "secondary axis"), g.Plot("Plot Time Axe 时间线").AxisLimits(timeDataMin, timeDataMax, 0, 1, g.ConditionOnce).Plots( g.LineXY("Time Line 时间线", timeDataX, timeDataY), g.ScatterXY("Time Scatter 时间散点图", timeDataX, timeScatterY), From 8415b2c4293bdf2b8b0b71d62dbbc7cc7dc9adc3 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Tue, 25 Jun 2024 10:37:03 +0200 Subject: [PATCH 4/4] fix lint errors --- Plot.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Plot.go b/Plot.go index 4b0966bc..3c83ae8d 100644 --- a/Plot.go +++ b/Plot.go @@ -12,12 +12,12 @@ type ( ) const ( - AxisX1 = PlotXAxis(imgui.AxisX1) - AxisX2 = PlotXAxis(imgui.AxisX2) - AxisX3 = PlotXAxis(imgui.AxisX3) - AxisY1 = PlotYAxis(imgui.AxisY1) - AxisY2 = PlotYAxis(imgui.AxisY2) - AxisY3 = PlotYAxis(imgui.AxisY3) + AxisX1 = imgui.AxisX1 + AxisX2 = imgui.AxisX2 + AxisX3 = imgui.AxisX3 + AxisY1 = imgui.AxisY1 + AxisY2 = imgui.AxisY2 + AxisY3 = imgui.AxisY3 ) // PlotWidget is implemented by all the particular plots, which can be used @@ -288,7 +288,7 @@ func (p *PlotCanvasWidget) Build() { func SwitchPlotAxes(x PlotXAxis, y PlotYAxis) PlotWidget { return Custom(func() { - imgui.PlotSetAxes(imgui.PlotAxisEnum(x), imgui.PlotAxisEnum(y)) + imgui.PlotSetAxes(x, y) }) }