refactor: adjust theme of chart
This commit is contained in:
parent
910e2dc422
commit
3a9897f9ad
5 changed files with 79 additions and 44 deletions
63
chart.go
63
chart.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,60 +32,25 @@ type LineChartOption struct {
|
|||
}
|
||||
|
||||
func NewLineChart(opt LineChartOption) (*Draw, error) {
|
||||
d, err := NewDraw(
|
||||
DrawOption{
|
||||
Parent: opt.Parent,
|
||||
Width: opt.getWidth(),
|
||||
Height: opt.getHeight(),
|
||||
},
|
||||
PaddingOption(opt.Padding),
|
||||
)
|
||||
result, err := chartBasicRender(&opt.ChartOption)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
theme := Theme{
|
||||
mode: opt.Theme,
|
||||
}
|
||||
opt.FillDefault(&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, &theme)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 暂时仅支持单一yaxis
|
||||
yRange, err := drawYAxis(d, &opt.ChartOption, &theme, xAxisHeight, chart.Box{
|
||||
Top: titleBox.Height(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
d := result.d
|
||||
theme := NewTheme(opt.Theme)
|
||||
|
||||
sd, err := NewDraw(DrawOption{
|
||||
Parent: d,
|
||||
}, PaddingOption(chart.Box{
|
||||
Top: titleBox.Height(),
|
||||
Top: result.titleBox.Height(),
|
||||
Left: YAxisWidth,
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
yRange := result.yRange
|
||||
xRange := result.xRange
|
||||
for i, series := range opt.SeriesList {
|
||||
points := make([]Point, 0)
|
||||
for j, item := range series.Data {
|
||||
|
|
|
|||
6
theme.go
6
theme.go
|
|
@ -31,6 +31,12 @@ type Theme struct {
|
|||
mode string
|
||||
}
|
||||
|
||||
func NewTheme(mode string) *Theme {
|
||||
return &Theme{
|
||||
mode: mode,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Theme) IsDark() bool {
|
||||
return t.mode == ThemeDark
|
||||
}
|
||||
|
|
|
|||
4
xaxis.go
4
xaxis.go
|
|
@ -27,11 +27,12 @@ import "github.com/wcharczuk/go-chart/v2"
|
|||
type XAxisOption struct {
|
||||
BoundaryGap *bool
|
||||
Data []string
|
||||
Theme string
|
||||
// TODO split number
|
||||
}
|
||||
|
||||
// drawXAxis draws x axis, and returns the height, range of if.
|
||||
func drawXAxis(p *Draw, opt *XAxisOption, theme *Theme) (int, *Range, error) {
|
||||
func drawXAxis(p *Draw, opt *XAxisOption) (int, *Range, error) {
|
||||
dXAxis, err := NewDraw(
|
||||
DrawOption{
|
||||
Parent: p,
|
||||
|
|
@ -43,6 +44,7 @@ func drawXAxis(p *Draw, opt *XAxisOption, theme *Theme) (int, *Range, error) {
|
|||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
theme := NewTheme(opt.Theme)
|
||||
data := NewAxisDataListFromStringList(opt.Data)
|
||||
style := AxisStyle{
|
||||
BoundaryGap: opt.BoundaryGap,
|
||||
|
|
|
|||
3
yaxis.go
3
yaxis.go
|
|
@ -28,7 +28,8 @@ import (
|
|||
|
||||
const YAxisWidth = 40
|
||||
|
||||
func drawYAxis(p *Draw, opt *ChartOption, theme *Theme, xAxisHeight int, padding chart.Box) (*Range, error) {
|
||||
func drawYAxis(p *Draw, opt *ChartOption, xAxisHeight int, padding chart.Box) (*Range, error) {
|
||||
theme := NewTheme(opt.Theme)
|
||||
yRange := opt.getYRange(0)
|
||||
data := NewAxisDataListFromStringList(yRange.Values())
|
||||
style := AxisStyle{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue