feat: support padding option for chart

This commit is contained in:
vicanso 2021-12-18 12:01:16 +08:00
parent c0170bf250
commit 9db6bcab1b
3 changed files with 25 additions and 6 deletions

View file

@ -51,6 +51,7 @@ type (
Padding chart.Box
}
Options struct {
Padding chart.Box
Width int
Height int
Theme string
@ -112,6 +113,9 @@ func New(opt Options) (Graph, error) {
if height <= 0 {
height = DefaultChartHeight
}
bg := chart.Style{
Padding: opt.Padding,
}
if opt.Series[0].Type == SeriesPie {
values := make(chart.Values, len(opt.Series))
for index, item := range opt.Series {
@ -121,13 +125,16 @@ func New(opt Options) (Graph, error) {
}
}
g := &chart.PieChart{
Background: bg,
Title: opt.Title.Text,
TitleStyle: opt.Title.Style,
Width: width,
Height: height,
Values: values,
ColorPalette: &ThemeColorPalette{
Theme: opt.Theme,
ColorPalette: &PieThemeColorPalette{
ThemeColorPalette: ThemeColorPalette{
Theme: opt.Theme,
},
},
}
return g, nil
@ -158,7 +165,8 @@ func New(opt Options) (Graph, error) {
}
g := &chart.Chart{
Log: opt.Log,
Log: opt.Log,
Background: bg,
ColorPalette: &ThemeColorPalette{
Theme: opt.Theme,
},

View file

@ -160,8 +160,9 @@ func (ex *EChartsXAxis) UnmarshalJSON(data []byte) error {
}
type ECharsOptions struct {
Theme string `json:"theme"`
Title struct {
Theme string `json:"theme"`
Padding EChartsPadding `json:"padding"`
Title struct {
Text string `json:"text"`
// 暂不支持(go-chart默认title只能居中)
TextAlign string `json:"textAlign"`
@ -238,8 +239,10 @@ func convertEChartsSeries(e *ECharsOptions) ([]Series, chart.TickPosition) {
func (e *ECharsOptions) ToOptions() Options {
o := Options{
Theme: e.Theme,
Theme: e.Theme,
Padding: e.Padding.box,
}
titleTextStyle := e.Title.TextStyle
o.Title = Title{
Text: e.Title.Text,

View file

@ -133,6 +133,14 @@ type ThemeColorPalette struct {
Theme string
}
type PieThemeColorPalette struct {
ThemeColorPalette
}
func (tp PieThemeColorPalette) TextColor() drawing.Color {
return getTextColor("")
}
func (tp ThemeColorPalette) BackgroundColor() drawing.Color {
return getBackgroundColor(tp.Theme)
}