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

Merge 28667e1f55d695754a68c1f179e972fa153fe09e into 2b8f0c7960e9553acea6d579a740713066da5e13

This commit is contained in:
Carl Verge 2024-01-30 02:58:06 -08:00 committed by GitHub
commit fc9279f2ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -31,6 +31,8 @@ type Plot struct {
PlotType PlotType
HorizontalScale int
DrawDirection DrawDirection // TODO
YAxisFmter func(v float64) string
XAxisFmter func(v int) string
}
const (
@ -176,10 +178,12 @@ func (self *Plot) plotAxes(buf *Buffer, maxVal float64) {
)
// 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,
)
val := (x-(self.Inner.Min.X+yAxisLabelsWidth)-1)/(self.HorizontalScale) + 1
label := fmt.Sprintf("%d", val)
if self.XAxisFmter != nil {
label = self.XAxisFmter(val)
}
buf.SetString(
label,
NewStyle(ColorWhite),
@ -190,8 +194,13 @@ func (self *Plot) plotAxes(buf *Buffer, maxVal float64) {
// draw y axis labels
verticalScale := maxVal / float64(self.Inner.Dy()-xAxisLabelsHeight-1)
for i := 0; i*(yAxisLabelsGap+1) < self.Inner.Dy()-1; i++ {
val := float64(i) * verticalScale * (yAxisLabelsGap + 1)
label := fmt.Sprintf("%.2f", val)
if self.YAxisFmter != nil {
label = self.YAxisFmter(val)
}
buf.SetString(
fmt.Sprintf("%.2f", float64(i)*verticalScale*(yAxisLabelsGap+1)),
label,
NewStyle(ColorWhite),
image.Pt(self.Inner.Min.X, self.Inner.Max.Y-(i*(yAxisLabelsGap+1))-2),
)