refactor: adjust theme of chart

This commit is contained in:
vicanso 2022-02-01 10:08:59 +08:00
parent 910e2dc422
commit 3a9897f9ad
5 changed files with 79 additions and 44 deletions

View file

@ -59,7 +59,8 @@ type ChartOption struct {
BackgroundColor drawing.Color
}
func (o *ChartOption) FillDefault(t *Theme) {
func (o *ChartOption) FillDefault(theme string) {
t := NewTheme(theme)
f, _ := chart.GetDefaultFont()
if o.BackgroundColor.IsZero() {
o.BackgroundColor = t.GetBackgroundColor()
@ -91,6 +92,9 @@ func (o *ChartOption) FillDefault(t *Theme) {
if o.Legend.Style.FontColor.IsZero() {
o.Legend.Style.FontColor = t.GetTextColor()
}
if o.XAxis.Theme == "" {
o.XAxis.Theme = theme
}
}
func (o *ChartOption) getWidth() int {
@ -139,3 +143,60 @@ func (r Range) Values() []string {
}
return values
}
type basicRenderResult struct {
xRange *Range
yRange *Range
d *Draw
titleBox chart.Box
}
func chartBasicRender(opt *ChartOption) (*basicRenderResult, error) {
d, err := NewDraw(
DrawOption{
Parent: opt.Parent,
Width: opt.getWidth(),
Height: opt.getHeight(),
},
PaddingOption(opt.Padding),
)
if err != nil {
return nil, err
}
opt.FillDefault(opt.Theme)
if opt.Parent == nil {
d.setBackground(opt.getWidth(), opt.getHeight(), opt.BackgroundColor)
}
// 标题
titleBox, err := drawTitle(d, &opt.Title)
if err != nil {
return nil, err
}
_, err = NewLegend(d, opt.Legend).Render()
if err != nil {
return nil, err
}
// xAxis
xAxisHeight, xRange, err := drawXAxis(d, &opt.XAxis)
if err != nil {
return nil, err
}
// 暂时仅支持单一yaxis
yRange, err := drawYAxis(d, opt, xAxisHeight, chart.Box{
Top: titleBox.Height(),
})
if err != nil {
return nil, err
}
return &basicRenderResult{
xRange: xRange,
yRange: yRange,
d: d,
titleBox: titleBox,
}, nil
}