refactor: default render function for axis
This commit is contained in:
parent
c4045cfbbe
commit
72e11e49b1
3 changed files with 97 additions and 49 deletions
84
charts.go
84
charts.go
|
|
@ -27,16 +27,92 @@ type Renderer interface {
|
|||
}
|
||||
|
||||
type defaultRenderOption struct {
|
||||
Theme ColorPalette
|
||||
Padding Box
|
||||
Theme ColorPalette
|
||||
Padding Box
|
||||
SeriesList SeriesList
|
||||
// The y axis option
|
||||
YAxisOptions []YAxisOption
|
||||
// The x axis option
|
||||
XAxis XAxisOption
|
||||
}
|
||||
|
||||
func defaultRender(p *Painter, opt defaultRenderOption) *Painter {
|
||||
type defaultRenderResult struct {
|
||||
axisRanges map[int]axisRange
|
||||
p *Painter
|
||||
}
|
||||
|
||||
func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, error) {
|
||||
p.SetBackground(p.Width(), p.Height(), opt.Theme.GetBackgroundColor())
|
||||
if !opt.Padding.IsZero() {
|
||||
p = p.Child(PainterPaddingOption(opt.Padding))
|
||||
}
|
||||
return p
|
||||
result := defaultRenderResult{
|
||||
axisRanges: make(map[int]axisRange),
|
||||
}
|
||||
|
||||
// 计算图表对应的轴有哪些
|
||||
axisIndexList := make([]int, 0)
|
||||
for _, series := range opt.SeriesList {
|
||||
if containsInt(axisIndexList, series.AxisIndex) {
|
||||
continue
|
||||
}
|
||||
axisIndexList = append(axisIndexList, series.index)
|
||||
}
|
||||
// 高度需要减去x轴的高度
|
||||
rangeHeight := p.Height() - defaultXAxisHeight
|
||||
rangeWidth := 0
|
||||
|
||||
// 计算对应的axis range
|
||||
for _, index := range axisIndexList {
|
||||
max, min := opt.SeriesList.GetMaxMin(index)
|
||||
r := NewRange(AxisRangeOption{
|
||||
Min: min,
|
||||
Max: max,
|
||||
// 高度需要减去x轴的高度
|
||||
Size: rangeHeight,
|
||||
// 分隔数量
|
||||
DivideCount: defaultAxisDivideCount,
|
||||
})
|
||||
result.axisRanges[index] = r
|
||||
yAxisOption := YAxisOption{}
|
||||
if len(opt.YAxisOptions) > index {
|
||||
yAxisOption = opt.YAxisOptions[index]
|
||||
}
|
||||
if yAxisOption.Theme == nil {
|
||||
yAxisOption.Theme = opt.Theme
|
||||
}
|
||||
yAxisOption.Data = r.Values()
|
||||
reverseStringSlice(yAxisOption.Data)
|
||||
// TODO生成其它位置既yAxis
|
||||
yAxis := NewLeftYAxis(p, yAxisOption)
|
||||
yAxisBox, err := yAxis.Render()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rangeWidth += yAxisBox.Width()
|
||||
}
|
||||
|
||||
if opt.XAxis.Theme == nil {
|
||||
opt.XAxis.Theme = opt.Theme
|
||||
}
|
||||
xAxis := NewBottomXAxis(p.Child(PainterPaddingOption(Box{
|
||||
Left: rangeWidth,
|
||||
})), opt.XAxis)
|
||||
_, err := xAxis.Render()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// // 生成Y轴
|
||||
// for _, yAxisOption := range opt.YAxisOptions {
|
||||
|
||||
// }
|
||||
|
||||
result.p = p.Child(PainterPaddingOption(Box{
|
||||
Bottom: rangeHeight,
|
||||
Left: rangeWidth,
|
||||
}))
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func doRender(renderers ...Renderer) error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue