feat: support between ticks

This commit is contained in:
vicanso 2021-12-13 23:46:10 +08:00
parent 402141c484
commit f3009b965f
7 changed files with 175 additions and 29 deletions

View file

@ -22,14 +22,26 @@
package charts
import "github.com/wcharczuk/go-chart/v2"
import (
"github.com/wcharczuk/go-chart/v2"
)
const defaultBarMargin = 10
type BarSeries struct {
BaseSeries
Count int
Index int
// 间隔
Margin int
// 偏移量
Offset int
// 宽度
BarWidth int
}
func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange chart.Range, defaults chart.Style) {
if bs.Len() == 0 {
if bs.Len() == 0 || bs.Count <= 0 {
return
}
style := bs.Style.InheritFrom(defaults)
@ -40,18 +52,25 @@ func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange
cb := canvasBox.Bottom
cl := canvasBox.Left
margin := bs.Margin
if margin <= 0 {
margin = defaultBarMargin
}
barWidth := bs.BarWidth
if barWidth <= 0 {
barWidth = canvasBox.Width() / (bs.Len() * bs.Count)
}
for i := 0; i < bs.Len(); i++ {
vx, vy := bs.GetValues(i)
x := cl + xrange.Translate(vx)
x := cl + xrange.Translate(vx) + bs.Index*(margin+barWidth) + bs.Offset
y := cb - yrange.Translate(vy)
chart.Draw.Box(r, chart.Box{
Left: x,
Top: y,
// TODO 计算宽度
Right: x + 10,
Left: x,
Top: y,
Right: x + barWidth,
Bottom: canvasBox.Bottom - 1,
}, style)
}