1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-29 13:48:51 +08:00

allow toggling x axis labels

This commit is contained in:
bakito 2023-05-03 07:14:10 +02:00
parent eee86868c2
commit 2235e5cddc
No known key found for this signature in database
GPG Key ID: BCCEB081DB8A24D8

View File

@ -22,9 +22,10 @@ type Plot struct {
DataLabels []string DataLabels []string
MaxVal float64 MaxVal float64
LineColors Colors LineColors Colors
AxesColor Color // TODO AxesColor Color // TODO
ShowAxes bool ShowAxes bool
ShowXAxisLabels bool
Marker PlotMarker Marker PlotMarker
DotMarkerRune rune DotMarkerRune rune
@ -72,6 +73,7 @@ func NewPlot() *Plot {
HorizontalScale: 1, HorizontalScale: 1,
DrawDirection: DrawRight, DrawDirection: DrawRight,
ShowAxes: true, ShowAxes: true,
ShowXAxisLabels: true,
PlotType: LineChart, PlotType: LineChart,
} }
} }
@ -167,25 +169,27 @@ func (self *Plot) plotAxes(buf *Buffer, maxVal float64) {
image.Pt(self.Inner.Min.X+yAxisLabelsWidth, i+self.Inner.Min.Y), image.Pt(self.Inner.Min.X+yAxisLabelsWidth, i+self.Inner.Min.Y),
) )
} }
// draw x axis labels if self.ShowXAxisLabels {
// draw 0 // draw x axis labels
buf.SetString( // draw 0
"0",
NewStyle(ColorWhite),
image.Pt(self.Inner.Min.X+yAxisLabelsWidth, self.Inner.Max.Y-1),
)
// draw rest
for x := self.Inner.Min.X + yAxisLabelsWidth + (xAxisLabelsGap)*self.HorizontalScale + 1; x < self.Inner.Max.X-1; {
label := fmt.Sprintf(
"%d",
(x-(self.Inner.Min.X+yAxisLabelsWidth)-1)/(self.HorizontalScale)+1,
)
buf.SetString( buf.SetString(
label, "0",
NewStyle(ColorWhite), NewStyle(ColorWhite),
image.Pt(x, self.Inner.Max.Y-1), image.Pt(self.Inner.Min.X+yAxisLabelsWidth, self.Inner.Max.Y-1),
) )
x += (len(label) + xAxisLabelsGap) * self.HorizontalScale // draw rest
for x := self.Inner.Min.X + yAxisLabelsWidth + (xAxisLabelsGap)*self.HorizontalScale + 1; x < self.Inner.Max.X-1; {
label := fmt.Sprintf(
"%d",
(x-(self.Inner.Min.X+yAxisLabelsWidth)-1)/(self.HorizontalScale)+1,
)
buf.SetString(
label,
NewStyle(ColorWhite),
image.Pt(x, self.Inner.Max.Y-1),
)
x += (len(label) + xAxisLabelsGap) * self.HorizontalScale
}
} }
// draw y axis labels // draw y axis labels
verticalScale := maxVal / float64(self.Inner.Dy()-xAxisLabelsHeight-1) verticalScale := maxVal / float64(self.Inner.Dy()-xAxisLabelsHeight-1)