Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions cmd/jp/bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package main
import (
"fmt"
"log"
"math"
"reflect"

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

func barPlotData(xv, yv []reflect.Value) (x []string, y []float64) {
func barPlotData(xv, yv []reflect.Value, digits int) (x []string, y []float64) {
for i := range xv {
if xv[i].IsValid() && xv[i].CanInterface() {
x = append(x, fmt.Sprint(xv[i].Interface()))
Expand All @@ -20,15 +21,15 @@ func barPlotData(xv, yv []reflect.Value) (x []string, y []float64) {
if yv[i].IsValid() && yv[i].CanInterface() {
yvi, ok := yv[i].Interface().(float64)
if ok {
y = append(y, yvi)
y = append(y, math.Round(yvi*math.Pow10(digits))/math.Pow10(digits))
}
}
}
return
}

func barPlot(xv, yv []reflect.Value, c draw.Canvas) string {
groups, y := barPlotData(xv, yv)
func barPlot(xv, yv []reflect.Value, c draw.Canvas, digits int) string {
groups, y := barPlotData(xv, yv, digits)
chart := plot.NewBarChart(c)
data := new(data.Table)
if len(y) == 0 {
Expand Down
20 changes: 11 additions & 9 deletions cmd/jp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (
)

type configuration struct {
Box draw.Box
X string
Y string
XY string
PlotType enumVar
CanvasType enumVar
InputType enumVar
HistBins uint
Box draw.Box
X string
Y string
XY string
PlotType enumVar
CanvasType enumVar
InputType enumVar
HistBins uint
RoundDigits int
}

const (
Expand Down Expand Up @@ -97,6 +98,7 @@ func init() {
flag.IntVar(&config.Box.Width, "width", 0, "Plot width (default 0 (auto))")
flag.IntVar(&config.Box.Height, "height", 0, "Plot height (default 0 (auto))")
flag.UintVar(&config.HistBins, "bins", 0, "Number of histogram bins (default 0 (auto))")
flag.IntVar(&config.RoundDigits, "round", 6, "Number of digits up to which the y value is to be rounded (applicable to bar type)")
flag.Parse()
log.SetOutput(os.Stderr)
log.SetFlags(log.LstdFlags | log.Lshortfile)
Expand Down Expand Up @@ -191,7 +193,7 @@ func main() {
case plotTypeScatter:
out = scatterPlot(x, y, c)
case plotTypeBar:
out = barPlot(x, y, c)
out = barPlot(x, y, c, config.RoundDigits)
case plotTypeHist:
out = histogram(x, c, config.HistBins)
case plotTypeHist2D:
Expand Down
23 changes: 23 additions & 0 deletions examples/round.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"x": 1,
"y": 1.022447
},
{
"x": 2,
"y": 2.7639
},
{
"x": 3,
"y": 3.4678
},
{
"x": 4,
"y": 4.1672
},
{
"x": 5,
"y": 5.7396
}

]