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

custom axis formatters on plot

This adds options on Plots for custom formatting on X and Y axis labels
This commit is contained in:
Carl Verge 2021-04-16 15:17:09 -07:00 committed by GitHub
parent f976fe697a
commit 28667e1f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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),
)