From 46da50f7fd7efa0987a69d48dbd8bd3995a49ddb Mon Sep 17 00:00:00 2001 From: agzuniverse Date: Thu, 12 Mar 2020 01:08:20 +0530 Subject: [PATCH 1/3] Show value of the barchart on top of each bar This improves the readability of the bars significantly. --- plotter/barchart.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plotter/barchart.go b/plotter/barchart.go index c5ce931a..8231eb0b 100644 --- a/plotter/barchart.go +++ b/plotter/barchart.go @@ -6,6 +6,7 @@ package plotter import ( "errors" + "fmt" "image/color" "math" @@ -119,6 +120,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 +141,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 +155,10 @@ func (b *BarChart) Plot(c draw.Canvas, plt *plot.Plot) { outline = c.ClipLinesX(pts) } c.StrokeLines(b.LineStyle, outline...) + // Display the value of each bar above it + barLabel := fmt.Sprintf("%f", ht) + ft, _ := vg.MakeFont(plot.DefaultFont, 10) + c.FillText(draw.TextStyle{Color: color.Black, Font: ft}, vg.Point{X: labelX, Y: labelY}, barLabel) } } From fc5524a58bd65b665c6fd2e2ea88cff39393b495 Mon Sep 17 00:00:00 2001 From: agzuniverse Date: Thu, 12 Mar 2020 01:12:36 +0530 Subject: [PATCH 2/3] Add a ShowValue option to BarChart struct The value label is displayed on top of bar charts only when the user has set this to true. --- plotter/barchart.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/plotter/barchart.go b/plotter/barchart.go index 8231eb0b..5b1cdbac 100644 --- a/plotter/barchart.go +++ b/plotter/barchart.go @@ -49,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. @@ -155,10 +159,12 @@ func (b *BarChart) Plot(c draw.Canvas, plt *plot.Plot) { outline = c.ClipLinesX(pts) } c.StrokeLines(b.LineStyle, outline...) - // Display the value of each bar above it - barLabel := fmt.Sprintf("%f", ht) - ft, _ := vg.MakeFont(plot.DefaultFont, 10) - c.FillText(draw.TextStyle{Color: color.Black, Font: ft}, vg.Point{X: labelX, Y: labelY}, barLabel) + if b.ShowValue { + // Display the value of each bar above it + barLabel := fmt.Sprintf("%f", ht) + ft, _ := vg.MakeFont(plot.DefaultFont, 10) + c.FillText(draw.TextStyle{Color: color.Black, Font: ft}, vg.Point{X: labelX, Y: labelY}, barLabel) + } } } From d5ee20b787afea5836545f29915a9113a73e1397 Mon Sep 17 00:00:00 2001 From: agzuniverse Date: Thu, 12 Mar 2020 01:17:13 +0530 Subject: [PATCH 3/3] Cut off the bar chart labels at 2 places after decimal Showing the entire float64 value is not practical and overflows badly in most situations. --- plotter/barchart.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotter/barchart.go b/plotter/barchart.go index 5b1cdbac..9c4d0d7d 100644 --- a/plotter/barchart.go +++ b/plotter/barchart.go @@ -161,7 +161,7 @@ func (b *BarChart) Plot(c draw.Canvas, plt *plot.Plot) { c.StrokeLines(b.LineStyle, outline...) if b.ShowValue { // Display the value of each bar above it - barLabel := fmt.Sprintf("%f", ht) + 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) }