fix: fix font setting for title, #15

This commit is contained in:
vicanso 2022-07-27 20:27:49 +08:00
parent 1713bc283f
commit e095223705
4 changed files with 44 additions and 4 deletions

1
.gitignore vendored
View file

@ -16,3 +16,4 @@
*.png *.png
*.svg *.svg
tmp tmp
NotoSansSC.ttf

View file

@ -259,6 +259,9 @@ func (o *ChartOption) fillDefault() {
if o.font == nil { if o.font == nil {
o.font, _ = chart.GetDefaultFont() o.font, _ = chart.GetDefaultFont()
} else {
// 如果指定了字体,则设置主题的字体
t.SetFont(o.font)
} }
if o.BackgroundColor.IsZero() { if o.BackgroundColor.IsZero() {
o.BackgroundColor = t.GetBackgroundColor() o.BackgroundColor = t.GetBackgroundColor()

View file

@ -25,7 +25,8 @@ func writeFile(buf []byte) error {
func main() { func main() {
// 字体文件需要自行下载 // 字体文件需要自行下载
buf, err := ioutil.ReadFile("../NotoSansSC.ttf") // https://github.com/googlefonts/noto-cjk
buf, err := ioutil.ReadFile("./NotoSansSC.ttf")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -83,7 +84,7 @@ func main() {
} }
p, err := charts.LineRender( p, err := charts.LineRender(
values, values,
charts.TitleTextOptionFunc("Line"), charts.TitleTextOptionFunc("测试"),
charts.FontFamilyOptionFunc("noto"), charts.FontFamilyOptionFunc("noto"),
charts.XAxisDataOptionFunc([]string{ charts.XAxisDataOptionFunc([]string{
"星期一", "星期一",

View file

@ -36,12 +36,19 @@ const ThemeAnt = "ant"
type ColorPalette interface { type ColorPalette interface {
IsDark() bool IsDark() bool
GetAxisStrokeColor() Color GetAxisStrokeColor() Color
SetAxisStrokeColor(Color)
GetAxisSplitLineColor() Color GetAxisSplitLineColor() Color
SetAxisSplitLineColor(Color)
GetSeriesColor(int) Color GetSeriesColor(int) Color
SetSeriesColor([]Color)
GetBackgroundColor() Color GetBackgroundColor() Color
SetBackgroundColor(Color)
GetTextColor() Color GetTextColor() Color
SetTextColor(Color)
GetFontSize() float64 GetFontSize() float64
SetFontSize(float64)
GetFont() *truetype.Font GetFont() *truetype.Font
SetFont(*truetype.Font)
} }
type themeColorPalette struct { type themeColorPalette struct {
@ -64,7 +71,7 @@ type ThemeOption struct {
SeriesColors []Color SeriesColors []Color
} }
var palettes = map[string]ColorPalette{} var palettes = map[string]*themeColorPalette{}
const defaultFontSize = 12.0 const defaultFontSize = 12.0
@ -241,7 +248,8 @@ func NewTheme(name string) ColorPalette {
if !ok { if !ok {
p = palettes[ThemeLight] p = palettes[ThemeLight]
} }
return p clone := *p
return &clone
} }
func (t *themeColorPalette) IsDark() bool { func (t *themeColorPalette) IsDark() bool {
@ -252,23 +260,42 @@ func (t *themeColorPalette) GetAxisStrokeColor() Color {
return t.axisStrokeColor return t.axisStrokeColor
} }
func (t *themeColorPalette) SetAxisStrokeColor(c Color) {
t.axisStrokeColor = c
}
func (t *themeColorPalette) GetAxisSplitLineColor() Color { func (t *themeColorPalette) GetAxisSplitLineColor() Color {
return t.axisSplitLineColor return t.axisSplitLineColor
} }
func (t *themeColorPalette) SetAxisSplitLineColor(c Color) {
t.axisSplitLineColor = c
}
func (t *themeColorPalette) GetSeriesColor(index int) Color { func (t *themeColorPalette) GetSeriesColor(index int) Color {
colors := t.seriesColors colors := t.seriesColors
return colors[index%len(colors)] return colors[index%len(colors)]
} }
func (t *themeColorPalette) SetSeriesColor(colors []Color) {
t.seriesColors = colors
}
func (t *themeColorPalette) GetBackgroundColor() Color { func (t *themeColorPalette) GetBackgroundColor() Color {
return t.backgroundColor return t.backgroundColor
} }
func (t *themeColorPalette) SetBackgroundColor(c Color) {
t.backgroundColor = c
}
func (t *themeColorPalette) GetTextColor() Color { func (t *themeColorPalette) GetTextColor() Color {
return t.textColor return t.textColor
} }
func (t *themeColorPalette) SetTextColor(c Color) {
t.textColor = c
}
func (t *themeColorPalette) GetFontSize() float64 { func (t *themeColorPalette) GetFontSize() float64 {
if t.fontSize != 0 { if t.fontSize != 0 {
return t.fontSize return t.fontSize
@ -276,6 +303,10 @@ func (t *themeColorPalette) GetFontSize() float64 {
return defaultFontSize return defaultFontSize
} }
func (t *themeColorPalette) SetFontSize(fontSize float64) {
t.fontSize = fontSize
}
func (t *themeColorPalette) GetFont() *truetype.Font { func (t *themeColorPalette) GetFont() *truetype.Font {
if t.font != nil { if t.font != nil {
return t.font return t.font
@ -283,3 +314,7 @@ func (t *themeColorPalette) GetFont() *truetype.Font {
f, _ := chart.GetDefaultFont() f, _ := chart.GetDefaultFont()
return f return f
} }
func (t *themeColorPalette) SetFont(f *truetype.Font) {
t.font = f
}