From 5b9d15cd8b809c0e627cd21c347cf2e11ef86e54 Mon Sep 17 00:00:00 2001
From: Enrico <enrico.bison@spindoxlabs.it>
Date: Fri, 26 Jul 2019 16:48:07 +0200
Subject: [PATCH] algo autowidth

---
 bar_chart.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/bar_chart.go b/bar_chart.go
index 45ddcc7..5442084 100644
--- a/bar_chart.go
+++ b/bar_chart.go
@@ -61,6 +61,8 @@ func (bc BarChart) GetFont() *truetype.Font {
 func (bc BarChart) GetWidth() int {
 	if bc.Width == 0 {
 		return DefaultChartWidth
+	} else if bc.Width == -1 {
+		return bc.autoWidth()
 	}
 	return bc.Width
 }
@@ -89,6 +91,19 @@ func (bc BarChart) GetBarWidth() int {
 	return bc.BarWidth
 }
 
+func (bc BarChart) autoWidth() (totalWidth int) {
+
+	totalWidth = 0
+	barWidth := bc.GetBarWidth()
+	barSpacing := bc.GetBarSpacing()
+
+	for _, _ = range bc.Bars {
+		totalWidth += barWidth + barSpacing
+	}
+
+	return
+}
+
 // Render renders the chart with the given renderer to the given io.Writer.
 func (bc BarChart) Render(rp RendererProvider, w io.Writer) error {
 	if len(bc.Bars) == 0 {