Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement plot 2nd and 3rd axis #805

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions Plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import (
imgui "github.com/AllenDang/cimgui-go"
)

type (
PlotXAxis = imgui.PlotAxisEnum
PlotYAxis = imgui.PlotAxisEnum
)

const (
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
// in (*PlotCanvasWidget).Plots.
type PlotWidget interface {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -242,6 +286,12 @@ func (p *PlotCanvasWidget) Build() {
}
}

func SwitchPlotAxes(x PlotXAxis, y PlotYAxis) PlotWidget {
return Custom(func() {
imgui.PlotSetAxes(x, y)
})
}

// BarPlot adds bar plot (column chart) to the canvas.
type BarPlot struct {
title string
Expand All @@ -251,7 +301,6 @@ type BarPlot struct {
offset int
}

// Bar adds a plot bar (column chart).
func Bar(title string, data []float64) *BarPlot {
return &BarPlot{
title: title,
Expand Down
3 changes: 2 additions & 1 deletion examples/plot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading