test: add test for bar series

This commit is contained in:
vicanso 2021-12-20 23:16:31 +08:00
parent bf25dad141
commit acc758cb9a
3 changed files with 202 additions and 18 deletions

View file

@ -47,29 +47,26 @@ type BarSeries struct {
CustomStyles []BarSeriesCustomStyle
}
type barSeriesWidthValues struct {
columnWidth int
columnMargin int
margin int
barWidth int
}
func (bs BarSeries) GetBarStyle(index, pointIndex int) chart.Style {
// 指定样式
for _, item := range bs.CustomStyles {
if item.Index == index && item.PointIndex == pointIndex {
return item.Style
}
}
// 其它非指定样式
return chart.Style{}
}
func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange chart.Range, defaults chart.Style) {
if bs.Len() == 0 || bs.Count <= 0 {
return
}
style := bs.Style.InheritFrom(defaults)
style.FillColor = style.StrokeColor
if !style.ShouldDrawStroke() {
return
}
cb := canvasBox.Bottom
cl := canvasBox.Left
columnWidth := canvasBox.Width() / bs.Len()
func (bs BarSeries) getWidthValues(width int) barSeriesWidthValues {
columnWidth := width / bs.Len()
// 块间隔
columnMargin := columnWidth / 10
minColumnMargin := 2
@ -92,6 +89,27 @@ func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange
// 重新计息columnMargin
columnMargin = (columnWidth - allBarMarginWidth - (bs.Count * barWidth)) / 2
}
return barSeriesWidthValues{
columnWidth: columnWidth,
columnMargin: columnMargin,
margin: margin,
barWidth: barWidth,
}
}
func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange chart.Range, defaults chart.Style) {
if bs.Len() == 0 || bs.Count <= 0 {
return
}
style := bs.Style.InheritFrom(defaults)
style.FillColor = style.StrokeColor
if !style.ShouldDrawStroke() {
return
}
cb := canvasBox.Bottom
cl := canvasBox.Left
widthValues := bs.getWidthValues(canvasBox.Width())
for i := 0; i < bs.Len(); i++ {
vx, vy := bs.GetValues(i)
@ -104,15 +122,15 @@ func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange
x := cl + xrange.Translate(vx)
// 由于bar是居中展示因此需要往前移一个显示块
x += (-columnWidth + columnMargin)
x += (-widthValues.columnWidth + widthValues.columnMargin)
// 计算是第几个bar位置右偏
x += bs.Index * (margin + barWidth)
x += bs.Index * (widthValues.margin + widthValues.barWidth)
y := cb - yrange.Translate(vy)
chart.Draw.Box(r, chart.Box{
Left: x,
Top: y,
Right: x + barWidth,
Right: x + widthValues.barWidth,
Bottom: canvasBox.Bottom - 1,
}, cloneStyle)
}