fix: fix font setting for title, #15
This commit is contained in:
parent
1713bc283f
commit
e095223705
4 changed files with 44 additions and 4 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -16,3 +16,4 @@
|
|||
*.png
|
||||
*.svg
|
||||
tmp
|
||||
NotoSansSC.ttf
|
||||
|
|
|
|||
|
|
@ -259,6 +259,9 @@ func (o *ChartOption) fillDefault() {
|
|||
|
||||
if o.font == nil {
|
||||
o.font, _ = chart.GetDefaultFont()
|
||||
} else {
|
||||
// 如果指定了字体,则设置主题的字体
|
||||
t.SetFont(o.font)
|
||||
}
|
||||
if o.BackgroundColor.IsZero() {
|
||||
o.BackgroundColor = t.GetBackgroundColor()
|
||||
|
|
|
|||
|
|
@ -25,7 +25,8 @@ func writeFile(buf []byte) error {
|
|||
|
||||
func main() {
|
||||
// 字体文件需要自行下载
|
||||
buf, err := ioutil.ReadFile("../NotoSansSC.ttf")
|
||||
// https://github.com/googlefonts/noto-cjk
|
||||
buf, err := ioutil.ReadFile("./NotoSansSC.ttf")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
@ -83,7 +84,7 @@ func main() {
|
|||
}
|
||||
p, err := charts.LineRender(
|
||||
values,
|
||||
charts.TitleTextOptionFunc("Line"),
|
||||
charts.TitleTextOptionFunc("测试"),
|
||||
charts.FontFamilyOptionFunc("noto"),
|
||||
charts.XAxisDataOptionFunc([]string{
|
||||
"星期一",
|
||||
|
|
|
|||
39
theme.go
39
theme.go
|
|
@ -36,12 +36,19 @@ const ThemeAnt = "ant"
|
|||
type ColorPalette interface {
|
||||
IsDark() bool
|
||||
GetAxisStrokeColor() Color
|
||||
SetAxisStrokeColor(Color)
|
||||
GetAxisSplitLineColor() Color
|
||||
SetAxisSplitLineColor(Color)
|
||||
GetSeriesColor(int) Color
|
||||
SetSeriesColor([]Color)
|
||||
GetBackgroundColor() Color
|
||||
SetBackgroundColor(Color)
|
||||
GetTextColor() Color
|
||||
SetTextColor(Color)
|
||||
GetFontSize() float64
|
||||
SetFontSize(float64)
|
||||
GetFont() *truetype.Font
|
||||
SetFont(*truetype.Font)
|
||||
}
|
||||
|
||||
type themeColorPalette struct {
|
||||
|
|
@ -64,7 +71,7 @@ type ThemeOption struct {
|
|||
SeriesColors []Color
|
||||
}
|
||||
|
||||
var palettes = map[string]ColorPalette{}
|
||||
var palettes = map[string]*themeColorPalette{}
|
||||
|
||||
const defaultFontSize = 12.0
|
||||
|
||||
|
|
@ -241,7 +248,8 @@ func NewTheme(name string) ColorPalette {
|
|||
if !ok {
|
||||
p = palettes[ThemeLight]
|
||||
}
|
||||
return p
|
||||
clone := *p
|
||||
return &clone
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) IsDark() bool {
|
||||
|
|
@ -252,23 +260,42 @@ func (t *themeColorPalette) GetAxisStrokeColor() Color {
|
|||
return t.axisStrokeColor
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) SetAxisStrokeColor(c Color) {
|
||||
t.axisStrokeColor = c
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) GetAxisSplitLineColor() Color {
|
||||
return t.axisSplitLineColor
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) SetAxisSplitLineColor(c Color) {
|
||||
t.axisSplitLineColor = c
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) GetSeriesColor(index int) Color {
|
||||
colors := t.seriesColors
|
||||
return colors[index%len(colors)]
|
||||
}
|
||||
func (t *themeColorPalette) SetSeriesColor(colors []Color) {
|
||||
t.seriesColors = colors
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) GetBackgroundColor() Color {
|
||||
return t.backgroundColor
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) SetBackgroundColor(c Color) {
|
||||
t.backgroundColor = c
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) GetTextColor() Color {
|
||||
return t.textColor
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) SetTextColor(c Color) {
|
||||
t.textColor = c
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) GetFontSize() float64 {
|
||||
if t.fontSize != 0 {
|
||||
return t.fontSize
|
||||
|
|
@ -276,6 +303,10 @@ func (t *themeColorPalette) GetFontSize() float64 {
|
|||
return defaultFontSize
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) SetFontSize(fontSize float64) {
|
||||
t.fontSize = fontSize
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) GetFont() *truetype.Font {
|
||||
if t.font != nil {
|
||||
return t.font
|
||||
|
|
@ -283,3 +314,7 @@ func (t *themeColorPalette) GetFont() *truetype.Font {
|
|||
f, _ := chart.GetDefaultFont()
|
||||
return f
|
||||
}
|
||||
|
||||
func (t *themeColorPalette) SetFont(f *truetype.Font) {
|
||||
t.font = f
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue