fixed spacing issues.

This commit is contained in:
Will Charczuk 2016-07-29 23:13:33 -07:00
parent e5cc7f9e9c
commit 8b6afae778
2 changed files with 21 additions and 12 deletions

View File

@ -40,6 +40,14 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
{Value: 1, Label: "Gray"}, {Value: 1, Label: "Gray"},
}, },
}, },
{
Name: "Test 2",
Values: []chart.Value{
{Value: 10, Label: "Blue"},
{Value: 5, Label: "Green"},
{Value: 1, Label: "Gray"},
},
},
}, },
} }

View File

@ -128,19 +128,25 @@ func (sbc StackedBarChart) drawBars(r Renderer, canvasBox Box) {
xoffset := canvasBox.Left xoffset := canvasBox.Left
for _, bar := range sbc.Bars { for _, bar := range sbc.Bars {
sbc.drawBar(r, canvasBox, xoffset, bar) sbc.drawBar(r, canvasBox, xoffset, bar)
xoffset += sbc.GetBarSpacing() xoffset += (sbc.GetBarSpacing() + bar.GetWidth())
} }
} }
func (sbc StackedBarChart) drawBar(r Renderer, canvasBox Box, xoffset int, bar StackedBar) int { func (sbc StackedBarChart) drawBar(r Renderer, canvasBox Box, xoffset int, bar StackedBar) int {
bxl := xoffset + Math.AbsInt(bar.GetWidth()>>1-sbc.GetBarSpacing()>>1) barSpacing2 := sbc.GetBarSpacing() >> 1
bxl := xoffset + barSpacing2
bxr := bxl + bar.GetWidth() bxr := bxl + bar.GetWidth()
normalizedBarComponents := Values(bar.Values).Normalize() normalizedBarComponents := Values(bar.Values).Normalize()
yoffset := canvasBox.Top yoffset := canvasBox.Top
for index, bv := range normalizedBarComponents { for index, bv := range normalizedBarComponents {
barHeight := int(math.Ceil(bv.Value * float64(canvasBox.Height()))) barHeight := int(math.Ceil(bv.Value * float64(canvasBox.Height())))
barBox := Box{Top: yoffset, Left: bxl, Right: bxr, Bottom: Math.MinInt(yoffset+barHeight, canvasBox.Bottom-DefaultStrokeWidth)} barBox := Box{
Top: yoffset,
Left: bxl,
Right: bxr,
Bottom: Math.MinInt(yoffset+barHeight, canvasBox.Bottom-DefaultStrokeWidth),
}
Draw.Box(r, barBox, bv.Style.InheritFrom(sbc.styleDefaultsStackedBarValue(index))) Draw.Box(r, barBox, bv.Style.InheritFrom(sbc.styleDefaultsStackedBarValue(index)))
yoffset += barHeight yoffset += barHeight
} }
@ -164,12 +170,10 @@ func (sbc StackedBarChart) drawXAxis(r Renderer, canvasBox Box) {
cursor := canvasBox.Left cursor := canvasBox.Left
for _, bar := range sbc.Bars { for _, bar := range sbc.Bars {
spacing := (sbc.GetBarSpacing() >> 1)
barLabelBox := Box{ barLabelBox := Box{
Top: canvasBox.Bottom + DefaultXAxisMargin, Top: canvasBox.Bottom + DefaultXAxisMargin,
Left: cursor, Left: cursor,
Right: cursor + bar.GetWidth() + spacing, Right: cursor + bar.GetWidth() + sbc.GetBarSpacing(),
Bottom: sbc.GetHeight(), Bottom: sbc.GetHeight(),
} }
if len(bar.Name) > 0 { if len(bar.Name) > 0 {
@ -179,7 +183,7 @@ func (sbc StackedBarChart) drawXAxis(r Renderer, canvasBox Box) {
r.MoveTo(barLabelBox.Right, canvasBox.Bottom) r.MoveTo(barLabelBox.Right, canvasBox.Bottom)
r.LineTo(barLabelBox.Right, canvasBox.Bottom+DefaultVerticalTickHeight) r.LineTo(barLabelBox.Right, canvasBox.Bottom+DefaultVerticalTickHeight)
r.Stroke() r.Stroke()
cursor += bar.GetWidth() + spacing cursor += bar.GetWidth() + sbc.GetBarSpacing()
} }
} }
} }
@ -226,11 +230,8 @@ func (sbc StackedBarChart) getDefaultCanvasBox() Box {
func (sbc StackedBarChart) getAdjustedCanvasBox(canvasBox Box) Box { func (sbc StackedBarChart) getAdjustedCanvasBox(canvasBox Box) Box {
var totalWidth int var totalWidth int
for index, bar := range sbc.Bars { for _, bar := range sbc.Bars {
totalWidth += bar.GetWidth() totalWidth += bar.GetWidth() + sbc.GetBarSpacing()
if index < len(sbc.Bars)-1 {
totalWidth += sbc.GetBarSpacing()
}
} }
return Box{ return Box{