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
drawAxes bool
drawXAxisLabel bool
xAxisLabelFunc func(int) string
drawYAxisLabel bool
yAxisLabelDataType PlotYAxisLabelDataType
yAxisAutoScaleMin bool
@ -83,6 +84,7 @@ func NewPlot() *Plot {
axesLabelColor: tcell.ColorDimGray,
drawAxes: true,
drawXAxisLabel: true,
xAxisLabelFunc: func(i int) string { return strconv.Itoa(i) },
drawYAxisLabel: true,
yAxisLabelDataType: PlotYAxisLabelDataFloat,
yAxisAutoScaleMin: false,
@ -152,6 +154,11 @@ func (plot *Plot) SetDrawXAxisLabel(draw bool) {
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.
func (plot *Plot) SetDrawYAxisLabel(draw bool) {
plot.drawYAxisLabel = draw
@ -273,15 +280,9 @@ func (plot *Plot) drawAxesToScreen(screen tcell.Screen) {
func (plot *Plot) drawXAxisLabelToScreen(
screen tcell.Screen, plotYAxisLabelsWidth int, x int, y int, width int, height int,
) {
tview.Print(screen, "0",
x+plotYAxisLabelsWidth,
y+height-plotXAxisLabelsHeight,
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)
for labelX := x + plotYAxisLabelsWidth; labelX < x+width-1; {
labelIndex := (labelX-(x+plotYAxisLabelsWidth)-1)/(plotHorizontalScale) + 1
label := plot.xAxisLabelFunc(labelIndex)
tview.Print(screen, label, labelX, y+height-plotXAxisLabelsHeight, width, tview.AlignLeft, plot.axesLabelColor)