Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom axis formatters on plot #297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions widgets/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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),
Expand All @@ -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),
)
Expand Down