diff --git a/chart.go b/chart.go index 33ed71b..8478757 100644 --- a/chart.go +++ b/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 +} diff --git a/line_chart.go b/line_chart.go index 3e04514..331ebcc 100644 --- a/line_chart.go +++ b/line_chart.go @@ -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 { diff --git a/theme.go b/theme.go index c47a5d8..5aa4bf9 100644 --- a/theme.go +++ b/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 } diff --git a/xaxis.go b/xaxis.go index c814074..6839329 100644 --- a/xaxis.go +++ b/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, diff --git a/yaxis.go b/yaxis.go index 171cfec..6cef270 100644 --- a/yaxis.go +++ b/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{