feat: support padding option for chart
This commit is contained in:
parent
c0170bf250
commit
9db6bcab1b
3 changed files with 25 additions and 6 deletions
14
charts.go
14
charts.go
|
|
@ -51,6 +51,7 @@ type (
|
||||||
Padding chart.Box
|
Padding chart.Box
|
||||||
}
|
}
|
||||||
Options struct {
|
Options struct {
|
||||||
|
Padding chart.Box
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
Theme string
|
Theme string
|
||||||
|
|
@ -112,6 +113,9 @@ func New(opt Options) (Graph, error) {
|
||||||
if height <= 0 {
|
if height <= 0 {
|
||||||
height = DefaultChartHeight
|
height = DefaultChartHeight
|
||||||
}
|
}
|
||||||
|
bg := chart.Style{
|
||||||
|
Padding: opt.Padding,
|
||||||
|
}
|
||||||
if opt.Series[0].Type == SeriesPie {
|
if opt.Series[0].Type == SeriesPie {
|
||||||
values := make(chart.Values, len(opt.Series))
|
values := make(chart.Values, len(opt.Series))
|
||||||
for index, item := range opt.Series {
|
for index, item := range opt.Series {
|
||||||
|
|
@ -121,13 +125,16 @@ func New(opt Options) (Graph, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g := &chart.PieChart{
|
g := &chart.PieChart{
|
||||||
|
Background: bg,
|
||||||
Title: opt.Title.Text,
|
Title: opt.Title.Text,
|
||||||
TitleStyle: opt.Title.Style,
|
TitleStyle: opt.Title.Style,
|
||||||
Width: width,
|
Width: width,
|
||||||
Height: height,
|
Height: height,
|
||||||
Values: values,
|
Values: values,
|
||||||
ColorPalette: &ThemeColorPalette{
|
ColorPalette: &PieThemeColorPalette{
|
||||||
Theme: opt.Theme,
|
ThemeColorPalette: ThemeColorPalette{
|
||||||
|
Theme: opt.Theme,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return g, nil
|
return g, nil
|
||||||
|
|
@ -158,7 +165,8 @@ func New(opt Options) (Graph, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
g := &chart.Chart{
|
g := &chart.Chart{
|
||||||
Log: opt.Log,
|
Log: opt.Log,
|
||||||
|
Background: bg,
|
||||||
ColorPalette: &ThemeColorPalette{
|
ColorPalette: &ThemeColorPalette{
|
||||||
Theme: opt.Theme,
|
Theme: opt.Theme,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -160,8 +160,9 @@ func (ex *EChartsXAxis) UnmarshalJSON(data []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ECharsOptions struct {
|
type ECharsOptions struct {
|
||||||
Theme string `json:"theme"`
|
Theme string `json:"theme"`
|
||||||
Title struct {
|
Padding EChartsPadding `json:"padding"`
|
||||||
|
Title struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
// 暂不支持(go-chart默认title只能居中)
|
// 暂不支持(go-chart默认title只能居中)
|
||||||
TextAlign string `json:"textAlign"`
|
TextAlign string `json:"textAlign"`
|
||||||
|
|
@ -238,8 +239,10 @@ func convertEChartsSeries(e *ECharsOptions) ([]Series, chart.TickPosition) {
|
||||||
|
|
||||||
func (e *ECharsOptions) ToOptions() Options {
|
func (e *ECharsOptions) ToOptions() Options {
|
||||||
o := Options{
|
o := Options{
|
||||||
Theme: e.Theme,
|
Theme: e.Theme,
|
||||||
|
Padding: e.Padding.box,
|
||||||
}
|
}
|
||||||
|
|
||||||
titleTextStyle := e.Title.TextStyle
|
titleTextStyle := e.Title.TextStyle
|
||||||
o.Title = Title{
|
o.Title = Title{
|
||||||
Text: e.Title.Text,
|
Text: e.Title.Text,
|
||||||
|
|
|
||||||
8
theme.go
8
theme.go
|
|
@ -133,6 +133,14 @@ type ThemeColorPalette struct {
|
||||||
Theme string
|
Theme string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PieThemeColorPalette struct {
|
||||||
|
ThemeColorPalette
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tp PieThemeColorPalette) TextColor() drawing.Color {
|
||||||
|
return getTextColor("")
|
||||||
|
}
|
||||||
|
|
||||||
func (tp ThemeColorPalette) BackgroundColor() drawing.Color {
|
func (tp ThemeColorPalette) BackgroundColor() drawing.Color {
|
||||||
return getBackgroundColor(tp.Theme)
|
return getBackgroundColor(tp.Theme)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue