feat: support font setting for charts

This commit is contained in:
vicanso 2021-12-28 23:37:15 +08:00
parent 297cbabcec
commit 2772798122
3 changed files with 49 additions and 2 deletions

View file

@ -26,7 +26,9 @@ import (
"bytes" "bytes"
"errors" "errors"
"io" "io"
"sync"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
) )
@ -66,9 +68,36 @@ type (
Legend Legend Legend Legend
TickPosition chart.TickPosition TickPosition chart.TickPosition
Log chart.Logger 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 { type Graph interface {
Render(rp chart.RendererProvider, w io.Writer) error Render(rp chart.RendererProvider, w io.Writer) error
} }
@ -136,6 +165,7 @@ func newPieChart(opt Options) *chart.PieChart {
} }
} }
return &chart.PieChart{ return &chart.PieChart{
Font: opt.Font,
Background: opt.getBackground(), Background: opt.getBackground(),
Title: opt.Title.Text, Title: opt.Title.Text,
TitleStyle: opt.Title.Style, TitleStyle: opt.Title.Style,
@ -178,6 +208,7 @@ func newChart(opt Options) *chart.Chart {
} }
c := &chart.Chart{ c := &chart.Chart{
Font: opt.Font,
Log: opt.Log, Log: opt.Log,
Background: opt.getBackground(), Background: opt.getBackground(),
ColorPalette: &ThemeColorPalette{ ColorPalette: &ThemeColorPalette{

View file

@ -28,8 +28,21 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/wcharczuk/go-chart/v2" "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) { func TestChartsOptions(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View file

@ -184,8 +184,7 @@ type ECharsOptions struct {
// 暂不支持(go-chart默认title只能居中) // 暂不支持(go-chart默认title只能居中)
TextAlign string `json:"textAlign"` TextAlign string `json:"textAlign"`
TextStyle struct { TextStyle struct {
Color string `json:"color"` Color string `json:"color"`
// TODO 字体支持
FontFamily string `json:"fontFamily"` FontFamily string `json:"fontFamily"`
FontSize float64 `json:"fontSize"` FontSize float64 `json:"fontSize"`
Height float64 `json:"height"` Height float64 `json:"height"`
@ -288,6 +287,10 @@ func (e *ECharsOptions) ToOptions() Options {
FontSize: titleTextStyle.FontSize, FontSize: titleTextStyle.FontSize,
}, },
} }
if e.Title.TextStyle.FontFamily != "" {
// 如果获取字体失败忽略
o.Font, _ = GetFont(e.Title.TextStyle.FontFamily)
}
if titleTextStyle.FontSize != 0 && titleTextStyle.Height > titleTextStyle.FontSize { if titleTextStyle.FontSize != 0 && titleTextStyle.Height > titleTextStyle.FontSize {
padding := int(titleTextStyle.Height-titleTextStyle.FontSize) / 2 padding := int(titleTextStyle.Height-titleTextStyle.FontSize) / 2