Skip to content

Commit

Permalink
add bar chart plot
Browse files Browse the repository at this point in the history
  • Loading branch information
shravanasati committed Feb 1, 2024
1 parent 260d95c commit a34e1fa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Binary file added barchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions internal/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func Export(formats []string, filename string, results []*SpeedResult, timeUnit
for _, format := range formats {
switch format {
case "json":
// todo make the json tags hyperfine output compatible
jsonMap := map[string]any{"time_unit": timeUnit.String()[1:], "results": results}
jsonData, err := jsonify(jsonMap)
if err != nil {
Expand Down
43 changes: 40 additions & 3 deletions internal/plotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"time"

"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)

func VerifyPlotFormats(formats string) ([]string, error) {
validFormats := []string{"hist", "histogram", "box", "boxplot", "bar", "errorbar", "bubble"}
validFormats := []string{"hist", "histogram", "box", "boxplot", "bar", "errorbar"}
formatList := strings.Split(strings.ToLower(formats), ",")
for _, f := range formatList {
if !slices.Contains(validFormats, f) {
Expand Down Expand Up @@ -48,6 +50,41 @@ func histogram(results []*SpeedResult, timeUnit string) {
}
}

func Plot(plotFormats []string, results []*SpeedResult, timeUnit time.Duration) {
func barPlot(results []*SpeedResult, timeUnit string) {
p := plot.New()
p.Title.Text = "Bar Chart"
p.Y.Label.Text = fmt.Sprintf("Mean times (in %s)", timeUnit)
meanTimes := make(plotter.Values, len(results))
copy(meanTimes, MapFunc[[]*SpeedResult, []float64](func(sr *SpeedResult) float64 { return sr.AverageElapsed }, results))

w := vg.Points(20)
bars, err := plotter.NewBarChart(meanTimes, w)
if err != nil {
panic(err)
}
bars.LineStyle.Width = vg.Length(0)
bars.Color = plotutil.Color(0)

p.Add(bars)

p.NominalX(MapFunc[[]*SpeedResult, []string](func(r *SpeedResult) string { return r.Command }, results)...)

}
barWidth := max(3, len(results))
if err := p.Save(font.Length(barWidth)*vg.Inch, 3*vg.Inch, "barchart.png"); err != nil {
panic(err)
}
}

func Plot(plotFormats []string, results []*SpeedResult, timeUnit time.Duration) {
if slices.Contains(plotFormats, "all") {
plotFormats = []string{"histogram", "bar", "errorbar", "boxplot"}
}
for _, plotFormat := range plotFormats {
switch plotFormat {
case "hist", "histogram":
histogram(results, timeUnit.String()[1:])
case "bar":
barPlot(results, timeUnit.String()[1:])
}
}
}

0 comments on commit a34e1fa

Please sign in to comment.