From a008ebe30e0177dcf1fe6d479e538504c34998ef Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Fri, 29 Jul 2016 18:40:43 -0700 Subject: [PATCH] tweaks. --- draw.go | 2 +- examples/pie_chart/main.go | 3 +-- pie_chart.go | 20 +++++++++++++++++--- style.go | 6 +++--- text.go | 6 +++++- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/draw.go b/draw.go index de61c1d..21d58df 100644 --- a/draw.go +++ b/draw.go @@ -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: diff --git a/examples/pie_chart/main.go b/examples/pie_chart/main.go index e4b3967..782bac1 100644 --- a/examples/pie_chart/main.go +++ b/examples/pie_chart/main.go @@ -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, diff --git a/pie_chart.go b/pie_chart.go index d67bac6..720f61d 100644 --- a/pie_chart.go +++ b/pie_chart.go @@ -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) diff --git a/style.go b/style.go index 78fca60..a9eb1c5 100644 --- a/style.go +++ b/style.go @@ -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 } diff --git a/text.go b/text.go index 6e45c1c..f1d73d2 100644 --- a/text.go +++ b/text.go @@ -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} }