Skip to content
Open
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
15 changes: 15 additions & 0 deletions plotter/barchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package plotter

import (
"errors"
"fmt"
"image/color"
"math"

Expand Down Expand Up @@ -48,6 +49,10 @@ type BarChart struct {
// stackedOn is the bar chart upon which
// this bar chart is stacked.
stackedOn *BarChart

// ShowValue determines whether the value of the bars should be
// shown above it or not.
ShowValue bool
}

// NewBarChart returns a new bar chart with a single bar for each value.
Expand Down Expand Up @@ -119,6 +124,8 @@ func (b *BarChart) Plot(c draw.Canvas, plt *plot.Plot) {
bottom := b.stackedOn.BarHeight(i)
valMin := trVal(bottom)
valMax := trVal(bottom + ht)
labelX := catMin
labelY := valMax

var pts []vg.Point
var poly []vg.Point
Expand All @@ -138,6 +145,8 @@ func (b *BarChart) Plot(c draw.Canvas, plt *plot.Plot) {
{valMax, catMin},
}
poly = c.ClipPolygonX(pts)
labelX = valMax
labelY = catMin
}
c.FillPolygon(b.Color, poly)

Expand All @@ -150,6 +159,12 @@ func (b *BarChart) Plot(c draw.Canvas, plt *plot.Plot) {
outline = c.ClipLinesX(pts)
}
c.StrokeLines(b.LineStyle, outline...)
if b.ShowValue {
// Display the value of each bar above it
barLabel := fmt.Sprintf("%.2f", ht)
ft, _ := vg.MakeFont(plot.DefaultFont, 10)
c.FillText(draw.TextStyle{Color: color.Black, Font: ft}, vg.Point{X: labelX, Y: labelY}, barLabel)
}
}
}

Expand Down