From 255dde81ad2cf9af74e8a4ceac920c8a4e0eaca5 Mon Sep 17 00:00:00 2001 From: Enrico Date: Fri, 26 Jul 2019 16:31:35 +0200 Subject: [PATCH 1/2] auto width BarChart setting Width=-1 --- _examples/bar_chart/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/_examples/bar_chart/main.go b/_examples/bar_chart/main.go index 693a300..ea15cee 100644 --- a/_examples/bar_chart/main.go +++ b/_examples/bar_chart/main.go @@ -19,6 +19,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, }, Height: 512, + Width: -1, BarWidth: 60, XAxis: chart.StyleShow(), YAxis: chart.YAxis{ From 5b9d15cd8b809c0e627cd21c347cf2e11ef86e54 Mon Sep 17 00:00:00 2001 From: Enrico Date: Fri, 26 Jul 2019 16:48:07 +0200 Subject: [PATCH 2/2] 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 {