From 7e4de64a0ddca62a275c47f8085b202cc92d1f05 Mon Sep 17 00:00:00 2001 From: vicanso Date: Thu, 26 May 2022 23:21:02 +0800 Subject: [PATCH] feat: support grid render function --- alias.go | 6 ++++ painter.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++- util.go | 18 ++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/alias.go b/alias.go index 3bacc67..d19c1f9 100644 --- a/alias.go +++ b/alias.go @@ -58,6 +58,12 @@ const ( PositionBottom = "bottom" ) +const ( + AlignLeft = "left" + AlignRight = "right" + AlignCenter = "center" +) + const ( OrientHorizontal = "horizontal" OrientVertical = "vertical" diff --git a/painter.go b/painter.go index 37c60bd..0120d68 100644 --- a/painter.go +++ b/painter.go @@ -68,6 +68,15 @@ type MultiTextOption struct { Align string } +type GridOption struct { + Column int + Row int + // 忽略不展示的column + IgnoreColumnLines []int + // 忽略不展示的row + IgnoreRowLines []int +} + // PainterPaddingOption sets the padding of draw painter func PainterPaddingOption(padding Box) PainterOption { return func(p *Painter) { @@ -275,6 +284,11 @@ func (p *Painter) LineTo(x, y int) *Painter { return p } +func (p *Painter) QuadCurveTo(cx, cy, x, y int) *Painter { + p.render.QuadCurveTo(cx+p.box.Left, cy+p.box.Top, x+p.box.Left, y+p.box.Top) + return p +} + func (p *Painter) Pin(x, y, width int) *Painter { r := float64(width) / 2 y -= width / 4 @@ -413,6 +427,27 @@ func (p *Painter) LineStroke(points []Point) *Painter { return p } +func (p *Painter) SmoothLineStroke(points []Point) *Painter { + prevX := 0 + prevY := 0 + // TODO 如何生成平滑的折线 + for index, point := range points { + x := point.X + y := point.Y + if index == 0 { + p.MoveTo(x, y) + } else { + cx := prevX + (x-prevX)/5 + cy := y + (y-prevY)/2 + p.QuadCurveTo(cx, cy, x, y) + } + prevX = x + prevY = y + } + p.Stroke() + return p +} + func (p *Painter) SetBackground(width, height int, color Color) *Painter { r := p.render s := chart.Style{ @@ -557,8 +592,12 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter { } count := len(opt.TextList) positionCenter := true - if opt.Position == PositionLeft { + if containsString([]string{ + PositionLeft, + PositionTop, + }, opt.Position) { positionCenter = false + count-- } width := p.Width() height := p.Height() @@ -594,3 +633,48 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter { } return p } + +func (p *Painter) Grid(opt GridOption) *Painter { + width := p.Width() + height := p.Height() + drawLines := func(values []int, ignoreIndexList []int, isVertical bool) { + for index, v := range values { + if containsInt(ignoreIndexList, index) { + continue + } + x0 := 0 + y0 := 0 + x1 := 0 + y1 := 0 + if isVertical { + + x0 = v + x1 = v + y1 = height + } else { + x1 = width + y0 = v + y1 = v + } + p.LineStroke([]Point{ + { + X: x0, + Y: y0, + }, + { + X: x1, + Y: y1, + }, + }) + } + } + if opt.Column > 0 { + values := autoDivide(width, opt.Column) + drawLines(values, opt.IgnoreColumnLines, true) + } + if opt.Row > 0 { + values := autoDivide(height, opt.Row) + drawLines(values, opt.IgnoreRowLines, false) + } + return p +} diff --git a/util.go b/util.go index 7306919..adfa9fd 100644 --- a/util.go +++ b/util.go @@ -43,6 +43,24 @@ func FalseFlag() *bool { return &f } +func containsInt(values []int, value int) bool { + for _, v := range values { + if v == value { + return true + } + } + return false +} + +func containsString(values []string, value string) bool { + for _, v := range values { + if v == value { + return true + } + } + return false +} + func ceilFloatToInt(value float64) int { i := int(value) if value == float64(i) {