added SetXAxisLabelFunc method to provide custom X-Axis labels

Signed-off-by: Markus Ressel <mail@markusressel.de>
This commit is contained in:
Markus Ressel 2024-10-12 19:32:49 +02:00
parent 40e5944bdc
commit d3dc728102
No known key found for this signature in database
GPG Key ID: F79626FAB2A3AACF

19
plot.go
View File

@ -64,6 +64,7 @@ type Plot struct {
axesLabelColor tcell.Color axesLabelColor tcell.Color
drawAxes bool drawAxes bool
drawXAxisLabel bool drawXAxisLabel bool
xAxisLabelFunc func(int) string
drawYAxisLabel bool drawYAxisLabel bool
yAxisLabelDataType PlotYAxisLabelDataType yAxisLabelDataType PlotYAxisLabelDataType
yAxisAutoScaleMin bool yAxisAutoScaleMin bool
@ -83,6 +84,7 @@ func NewPlot() *Plot {
axesLabelColor: tcell.ColorDimGray, axesLabelColor: tcell.ColorDimGray,
drawAxes: true, drawAxes: true,
drawXAxisLabel: true, drawXAxisLabel: true,
xAxisLabelFunc: func(i int) string { return strconv.Itoa(i) },
drawYAxisLabel: true, drawYAxisLabel: true,
yAxisLabelDataType: PlotYAxisLabelDataFloat, yAxisLabelDataType: PlotYAxisLabelDataFloat,
yAxisAutoScaleMin: false, yAxisAutoScaleMin: false,
@ -152,6 +154,11 @@ func (plot *Plot) SetDrawXAxisLabel(draw bool) {
plot.drawXAxisLabel = draw plot.drawXAxisLabel = draw
} }
// SetXAxisLabelFunc sets x axis label function.
func (plot *Plot) SetXAxisLabelFunc(f func(int) string) {
plot.xAxisLabelFunc = f
}
// SetDrawYAxisLabel set true in order to draw y axis label to screen. // SetDrawYAxisLabel set true in order to draw y axis label to screen.
func (plot *Plot) SetDrawYAxisLabel(draw bool) { func (plot *Plot) SetDrawYAxisLabel(draw bool) {
plot.drawYAxisLabel = draw plot.drawYAxisLabel = draw
@ -273,15 +280,9 @@ func (plot *Plot) drawAxesToScreen(screen tcell.Screen) {
func (plot *Plot) drawXAxisLabelToScreen( func (plot *Plot) drawXAxisLabelToScreen(
screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, width int, height int, screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, width int, height int,
) { ) {
tview.Print(screen, "0", for labelX := x + plotYAxisLabelsWidth; labelX < x+width-1; {
x+plotYAxisLabelsWidth, labelIndex := (labelX-(x+plotYAxisLabelsWidth)-1)/(plotHorizontalScale) + 1
y+height-plotXAxisLabelsHeight, label := plot.xAxisLabelFunc(labelIndex)
1,
tview.AlignLeft, plot.axesLabelColor)
for labelX := x + plotYAxisLabelsWidth +
(plotXAxisLabelsGap)*plotHorizontalScale + 1; labelX < x+width-1; {
label := strconv.Itoa((labelX-(x+plotYAxisLabelsWidth)-1)/(plotHorizontalScale) + 1)
tview.Print(screen, label, labelX, y+height-plotXAxisLabelsHeight, width, tview.AlignLeft, plot.axesLabelColor) tview.Print(screen, label, labelX, y+height-plotXAxisLabelsHeight, width, tview.AlignLeft, plot.axesLabelColor)