summaryrefslogtreecommitdiffstats
path: root/cmd/jp/bar.go
blob: 15dd1965dde3c727ff38f83a99a6b12409b17fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main

import (
	"fmt"
	"reflect"

	"github.com/sgreben/jp/pkg/draw"
	"github.com/sgreben/jp/pkg/plot"
)

func barPlotData(xvv, yvv [][]reflect.Value) (x []string, y []float64) {
	for _, xv := range xvv {
		for i := range xv {
			if xv[i].IsValid() && xv[i].CanInterface() {
				x = append(x, fmt.Sprint(xv[i].Interface()))
			}
		}
	}
	for _, yv := range yvv {
		for i := range yv {
			if yv[i].IsValid() && yv[i].CanInterface() {
				yvi, ok := yv[i].Interface().(float64)
				if ok {
					y = append(y, yvi)
				}
			}
		}
	}
	return
}

func barPlot(xvv, yvv [][]reflect.Value, c draw.Canvas) string {
	groups, y := barPlotData(xvv, yvv)
	chart := plot.NewBarChart(c)
	data := new(plot.DataTable)
	if len(groups) != len(y) {
		for i := range y {
			data.AddColumn(fmt.Sprint(i))
		}
	} else {
		for _, g := range groups {
			data.AddColumn(g)
		}
	}
	data.AddRow(y...)
	return chart.Draw(data)
}