This commit is contained in:
Will Charczuk 2016-07-29 18:40:43 -07:00
parent cbc0002d2a
commit a008ebe30e
5 changed files with 27 additions and 10 deletions

View file

@ -245,7 +245,7 @@ func (d draw) TextWithin(r Renderer, text string, box Box, style Style) {
lineBox := r.MeasureText(line)
switch style.GetTextHorizontalAlign() {
case TextHorizontalAlignCenter:
tx = box.Left + ((lineBox.Width() - box.Left) >> 1)
tx = box.Left + ((box.Width() - lineBox.Width()) >> 1)
case TextHorizontalAlignRight:
tx = box.Right - lineBox.Width()
default:

View file

@ -12,8 +12,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
pie := chart.PieChart{
Title: "test\nchart",
TitleStyle: chart.Style{
Show: true,
FontSize: 32,
Show: true,
},
Width: 512,
Height: 512,

View file

@ -242,15 +242,29 @@ func (pc PieChart) styleDefaultsElements() Style {
func (pc PieChart) styleDefaultsTitle() Style {
return pc.TitleStyle.InheritFrom(Style{
FontColor: DefaultTextColor,
FontColor: ColorWhite,
Font: pc.GetFont(),
FontSize: 24.0,
FontSize: pc.getTitleFontSize(),
TextHorizontalAlign: TextHorizontalAlignCenter,
TextVerticalAlign: TextVerticalAlignTop,
TextWrap: TextWrapNone,
TextWrap: TextWrapWord,
})
}
func (pc PieChart) getTitleFontSize() float64 {
effectiveDimension := Math.MinInt(pc.GetWidth(), pc.GetHeight())
if effectiveDimension >= 2048 {
return 48
} else if effectiveDimension >= 1024 {
return 24
} else if effectiveDimension >= 512 {
return 18
} else if effectiveDimension >= 256 {
return 12
}
return 10
}
// Box returns the chart bounds as a box.
func (pc PieChart) Box() Box {
dpr := pc.Background.Padding.GetRight(DefaultBackgroundPadding.Right)

View file

@ -196,7 +196,7 @@ func (s Style) GetTextHorizontalAlign(defaults ...textHorizontalAlign) textHoriz
if len(defaults) > 0 {
return defaults[0]
}
return TextHorizontalAlignLeft
return TextHorizontalAlignUnset
}
return s.TextHorizontalAlign
}
@ -207,7 +207,7 @@ func (s Style) GetTextVerticalAlign(defaults ...textVerticalAlign) textVerticalA
if len(defaults) > 0 {
return defaults[0]
}
return TextVerticalAlignBaseline
return TextVerticalAlignUnset
}
return s.TextVerticalAlign
}
@ -218,7 +218,7 @@ func (s Style) GetTextWrap(defaults ...textWrap) textWrap {
if len(defaults) > 0 {
return defaults[0]
}
return TextWrapWord
return TextWrapUnset
}
return s.TextWrap
}

View file

@ -1,6 +1,9 @@
package chart
import "strings"
import (
"fmt"
"strings"
)
// TextHorizontalAlign is an enum for the horizontal alignment options.
type textHorizontalAlign int
@ -71,6 +74,7 @@ func (t text) WrapFit(r Renderer, value string, width int, style Style) []string
case TextWrapWord:
return t.WrapFitWord(r, value, width, style)
}
fmt.Printf("text wrap: %#v\n", style.TextWrap)
return []string{value}
}