This commit is contained in:
Will Charczuk 2016-08-05 21:25:56 -07:00
parent 54f3bd4d3a
commit 129cbad0fa
2 changed files with 79 additions and 3 deletions

View File

@ -309,7 +309,7 @@ func (bc BarChart) calculateEffectiveBarSpacing(canvasBox Box) int {
if totalWithBaseSpacing > canvasBox.Width() { if totalWithBaseSpacing > canvasBox.Width() {
lessBarWidths := canvasBox.Width() - (len(bc.Bars) * bc.GetBarWidth()) lessBarWidths := canvasBox.Width() - (len(bc.Bars) * bc.GetBarWidth())
if lessBarWidths > 0 { if lessBarWidths > 0 {
return int(math.Floor(float64(lessBarWidths) / float64(len(bc.Bars)))) return int(math.Ceil(float64(lessBarWidths) / float64(len(bc.Bars))))
} }
return 0 return 0
} }
@ -321,7 +321,7 @@ func (bc BarChart) calculateEffectiveBarWidth(canvasBox Box, spacing int) int {
if totalWithBaseWidth > canvasBox.Width() { if totalWithBaseWidth > canvasBox.Width() {
totalLessBarSpacings := canvasBox.Width() - (len(bc.Bars) * spacing) totalLessBarSpacings := canvasBox.Width() - (len(bc.Bars) * spacing)
if totalLessBarSpacings > 0 { if totalLessBarSpacings > 0 {
return int(math.Floor(float64(totalLessBarSpacings) / float64(len(bc.Bars)))) return int(math.Ceil(float64(totalLessBarSpacings) / float64(len(bc.Bars))))
} }
return 0 return 0
} }
@ -329,7 +329,7 @@ func (bc BarChart) calculateEffectiveBarWidth(canvasBox Box, spacing int) int {
} }
func (bc BarChart) calculateTotalBarWidth(barWidth, spacing int) int { func (bc BarChart) calculateTotalBarWidth(barWidth, spacing int) int {
return len(bc.Bars) * (bc.GetBarWidth() + spacing) return len(bc.Bars) * (barWidth + spacing)
} }
func (bc BarChart) calculateScaledTotalWidth(canvasBox Box) (width, spacing, total int) { func (bc BarChart) calculateScaledTotalWidth(canvasBox Box) (width, spacing, total int) {

View File

@ -172,3 +172,79 @@ func TestBarChartGetValueFormatters(t *testing.T) {
bc.YAxis.ValueFormatter = func(_ interface{}) string { return "test" } bc.YAxis.ValueFormatter = func(_ interface{}) string { return "test" }
assert.Equal("test", bc.getValueFormatters()(1234)) assert.Equal("test", bc.getValueFormatters()(1234))
} }
func TestBarChartGetAxesTicks(t *testing.T) {
assert := assert.New(t)
bc := BarChart{
Bars: []Value{
{Value: 1.0},
{Value: 2.0},
{Value: 3.0},
},
}
r, err := PNG(128, 128)
assert.Nil(err)
yr := bc.getRanges()
yf := bc.getValueFormatters()
ticks := bc.getAxesTicks(r, yr, yf)
assert.Empty(ticks)
bc.YAxis.Style.Show = true
ticks = bc.getAxesTicks(r, yr, yf)
assert.Len(ticks, 2)
}
func TestBarChartCalculateEffectiveBarSpacing(t *testing.T) {
assert := assert.New(t)
bc := BarChart{
Width: 1024,
BarWidth: 10,
Bars: []Value{
{Value: 1.0, Label: "One"},
{Value: 2.0, Label: "Two"},
{Value: 3.0, Label: "Three"},
{Value: 4.0, Label: "Four"},
{Value: 5.0, Label: "Five"},
},
}
spacing := bc.calculateEffectiveBarSpacing(bc.box())
assert.NotZero(spacing)
bc.BarWidth = 250
spacing = bc.calculateEffectiveBarSpacing(bc.box())
assert.Zero(spacing)
}
func TestBarChartCalculateEffectiveBarWidth(t *testing.T) {
assert := assert.New(t)
bc := BarChart{
Width: 1024,
BarWidth: 10,
Bars: []Value{
{Value: 1.0, Label: "One"},
{Value: 2.0, Label: "Two"},
{Value: 3.0, Label: "Three"},
{Value: 4.0, Label: "Four"},
{Value: 5.0, Label: "Five"},
},
}
spacing := bc.calculateEffectiveBarSpacing(bc.box())
assert.NotZero(spacing)
barWidth := bc.calculateEffectiveBarWidth(bc.box(), spacing)
assert.Equal(10, barWidth)
bc.BarWidth = 250
spacing = bc.calculateEffectiveBarSpacing(bc.box())
barWidth = bc.calculateEffectiveBarWidth(bc.box(), spacing)
assert.Equal(199, barWidth)
assert.Equal(1024, bc.calculateTotalBarWidth(barWidth, spacing))
}