From 2772798122db690839980358611619eff670ccb1 Mon Sep 17 00:00:00 2001 From: vicanso Date: Tue, 28 Dec 2021 23:37:15 +0800 Subject: [PATCH] feat: support font setting for charts --- charts.go | 31 +++++++++++++++++++++++++++++++ charts_test.go | 13 +++++++++++++ echarts.go | 7 +++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/charts.go b/charts.go index f5fcb1f..3cef373 100644 --- a/charts.go +++ b/charts.go @@ -26,7 +26,9 @@ import ( "bytes" "errors" "io" + "sync" + "github.com/golang/freetype/truetype" "github.com/wcharczuk/go-chart/v2" ) @@ -66,9 +68,36 @@ type ( Legend Legend TickPosition chart.TickPosition Log chart.Logger + Font *truetype.Font } ) +var fonts = sync.Map{} +var ErrFontNotExists = errors.New("font is not exists") + +// InstallFont installs the font for charts +func InstallFont(fontFamily string, data []byte) error { + font, err := truetype.Parse(data) + if err != nil { + return err + } + fonts.Store(fontFamily, font) + return nil +} + +// GetFont returns the font of font family +func GetFont(fontFamily string) (*truetype.Font, error) { + value, ok := fonts.Load(fontFamily) + if !ok { + return nil, ErrFontNotExists + } + f, ok := value.(*truetype.Font) + if !ok { + return nil, ErrFontNotExists + } + return f, nil +} + type Graph interface { Render(rp chart.RendererProvider, w io.Writer) error } @@ -136,6 +165,7 @@ func newPieChart(opt Options) *chart.PieChart { } } return &chart.PieChart{ + Font: opt.Font, Background: opt.getBackground(), Title: opt.Title.Text, TitleStyle: opt.Title.Style, @@ -178,6 +208,7 @@ func newChart(opt Options) *chart.Chart { } c := &chart.Chart{ + Font: opt.Font, Log: opt.Log, Background: opt.getBackground(), ColorPalette: &ThemeColorPalette{ diff --git a/charts_test.go b/charts_test.go index 3173868..98a7288 100644 --- a/charts_test.go +++ b/charts_test.go @@ -28,8 +28,21 @@ import ( "github.com/stretchr/testify/assert" "github.com/wcharczuk/go-chart/v2" + "github.com/wcharczuk/go-chart/v2/roboto" ) +func TestFont(t *testing.T) { + assert := assert.New(t) + + fontFamily := "roboto" + err := InstallFont(fontFamily, roboto.Roboto) + assert.Nil(err) + + font, err := GetFont(fontFamily) + assert.Nil(err) + assert.NotNil(font) +} + func TestChartsOptions(t *testing.T) { assert := assert.New(t) diff --git a/echarts.go b/echarts.go index bd5a950..a55da5a 100644 --- a/echarts.go +++ b/echarts.go @@ -184,8 +184,7 @@ type ECharsOptions struct { // 暂不支持(go-chart默认title只能居中) TextAlign string `json:"textAlign"` TextStyle struct { - Color string `json:"color"` - // TODO 字体支持 + Color string `json:"color"` FontFamily string `json:"fontFamily"` FontSize float64 `json:"fontSize"` Height float64 `json:"height"` @@ -288,6 +287,10 @@ func (e *ECharsOptions) ToOptions() Options { FontSize: titleTextStyle.FontSize, }, } + if e.Title.TextStyle.FontFamily != "" { + // 如果获取字体失败忽略 + o.Font, _ = GetFont(e.Title.TextStyle.FontFamily) + } if titleTextStyle.FontSize != 0 && titleTextStyle.Height > titleTextStyle.FontSize { padding := int(titleTextStyle.Height-titleTextStyle.FontSize) / 2