diff options
author | Sergey Grebenshchikov <sgreben@gmail.com> | 2018-03-29 00:53:05 +0200 |
---|---|---|
committer | Sergey Grebenshchikov <sgreben@gmail.com> | 2018-03-30 01:00:44 +0200 |
commit | ef658b5e65bfef1c1ee62125ec6f0c33a27eb7e7 (patch) | |
tree | 88b265cb06e76b54c6f467b65e944457bd7c308d /cmd/jp/hist.go | |
parent | 3791de72267fc1b561e7f22595c9e90353f2f351 (diff) |
Add histogram plot type, display bar chart labels on the right instead of overlapping.
Diffstat (limited to 'cmd/jp/hist.go')
-rw-r--r-- | cmd/jp/hist.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/cmd/jp/hist.go b/cmd/jp/hist.go new file mode 100644 index 0000000..77c04e6 --- /dev/null +++ b/cmd/jp/hist.go @@ -0,0 +1,50 @@ +package main + +import ( + "log" + "reflect" + + "github.com/sgreben/jp/pkg/data" + "github.com/sgreben/jp/pkg/draw" + "github.com/sgreben/jp/pkg/plot" +) + +func histogramData(xv []reflect.Value, nbins uint) (groups []string, counts []float64) { + var x []float64 + for i := range xv { + if xv[i].IsValid() && xv[i].CanInterface() { + xvi, ok := xv[i].Interface().(float64) + if ok { + x = append(x, xvi) + } + } + } + if len(x) == 0 { + log.Fatal("no valid x values given") + } + bins := data.NewBins(x) + bins.Number = int(nbins) + if nbins == 0 { + bins.ChooseSturges() + } + hist := data.Histogram(x, bins) + groups = make([]string, len(hist)) + counts = make([]float64, len(hist)) + for i, b := range hist { + groups[i] = b.String() + counts[i] = float64(b.Count) + } + return +} + +func histogram(xv []reflect.Value, c draw.Canvas, nbins uint) string { + groups, counts := histogramData(xv, nbins) + chart := plot.NewBarChart(c) + chart.BarPaddingX = 0 + data := new(data.Table) + for _, g := range groups { + data.AddColumn(g) + } + data.AddRow(counts...) + return chart.Draw(data) +} |