From 1ec75e1fd2f55c5c1721d26e6e06347cf05c5c63 Mon Sep 17 00:00:00 2001 From: David Blyth Date: Thu, 8 Mar 2018 13:26:59 -0600 Subject: [PATCH] Moved things into axis.go --- ticks.go => axis.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) rename ticks.go => axis.go (73%) diff --git a/ticks.go b/axis.go similarity index 73% rename from ticks.go rename to axis.go index cbefe9b..8142f14 100644 --- a/ticks.go +++ b/axis.go @@ -7,6 +7,20 @@ import ( "gonum.org/v1/plot" ) +type LogScale struct{} + +func (LogScale) Normalize(min, max, x float64) float64 { + logMin := log10(min) + return (log10(x) - logMin) / (log10(max) - logMin) +} + +func log10(x float64) float64 { + if x <= 0 { + return -1 + } + return math.Log10(x) +} + type PreciseTicks struct { NSuggestedTicks int } @@ -103,6 +117,26 @@ func round(x float64, prec int) float64 { return x / pow } +type LogTicks struct{} + +func (LogTicks) Ticks(min, max float64) []plot.Tick { + val := math.Pow10(int(log10(min))) + max = math.Pow10(int(math.Ceil(log10(max)))) + var ticks []plot.Tick + for val < max { + for i := 1; i < 10; i++ { + if i == 1 { + ticks = append(ticks, plot.Tick{Value: val, Label: formatFloatTick(val, -1)}) + } + ticks = append(ticks, plot.Tick{Value: val * float64(i)}) + } + val *= 10 + } + ticks = append(ticks, plot.Tick{Value: val, Label: formatFloatTick(val, -1)}) + + return ticks +} + func formatFloatTick(v float64, prec int) string { return strconv.FormatFloat(v, 'g', prec, 64) } -- GitLab