Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto width barchart #123

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions _examples/bar_chart/main.go
Expand Up @@ -19,6 +19,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
},
},
Height: 512,
Width: -1,
BarWidth: 60,
XAxis: chart.StyleShow(),
YAxis: chart.YAxis{
Expand Down
15 changes: 15 additions & 0 deletions bar_chart.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down