diff --git a/.gitignore b/.gitignore index 2e33342..4a7b0d9 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ *.png *.svg tmp +NotoSansSC.ttf diff --git a/chart_option.go b/chart_option.go index 41fda46..cb3bd3f 100644 --- a/chart_option.go +++ b/chart_option.go @@ -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() diff --git a/examples/chinese/main.go b/examples/chinese/main.go index bb7cc00..9068a08 100644 --- a/examples/chinese/main.go +++ b/examples/chinese/main.go @@ -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{ "星期一", diff --git a/theme.go b/theme.go index 31c3bf8..8068687 100644 --- a/theme.go +++ b/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 +}