diff --git a/plotter/barchart.go b/plotter/barchart.go index c5ce931a..9c4d0d7d 100644 --- a/plotter/barchart.go +++ b/plotter/barchart.go @@ -6,6 +6,7 @@ package plotter import ( "errors" + "fmt" "image/color" "math" @@ -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. @@ -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 @@ -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) @@ -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) + } } }