fix: fix series render of horizontal bar, #31

This commit is contained in:
vicanso 2022-11-03 21:31:53 +08:00
parent a88e607bfc
commit bdcc871ab1
4 changed files with 25 additions and 6 deletions

View file

@ -75,6 +75,7 @@ type AxisOption struct {
SplitLineShow bool SplitLineShow bool
// The color of split line // The color of split line
SplitLineColor Color SplitLineColor Color
Unit int
} }
func (a *axisPainter) Render() (Box, error) { func (a *axisPainter) Render() (Box, error) {
@ -159,12 +160,16 @@ func (a *axisPainter) Render() (Box, error) {
// 根据文本宽度计算较为符合的展示项 // 根据文本宽度计算较为符合的展示项
fitTextCount := ceilFloatToInt(float64(top.Width()) / textFillWidth) fitTextCount := ceilFloatToInt(float64(top.Width()) / textFillWidth)
unit := ceilFloatToInt(float64(dataCount) / float64(fitTextCount)) unit := opt.Unit
if unit <= 0 {
unit = ceilFloatToInt(float64(dataCount) / float64(fitTextCount))
unit = chart.MaxInt(unit, opt.SplitNumber) unit = chart.MaxInt(unit, opt.SplitNumber)
// 偶数 // 偶数
if unit%2 == 0 && dataCount%(unit+1) == 0 { if unit%2 == 0 && dataCount%(unit+1) == 0 {
unit++ unit++
} }
}
width := 0 width := 0
height := 0 height := 0

View file

@ -186,6 +186,10 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
if len(opt.YAxisOptions) > index { if len(opt.YAxisOptions) > index {
yAxisOption = opt.YAxisOptions[index] yAxisOption = opt.YAxisOptions[index]
} }
divideCount := yAxisOption.DivideCount
if divideCount <= 0 {
divideCount = defaultAxisDivideCount
}
max, min := opt.SeriesList.GetMaxMin(index) max, min := opt.SeriesList.GetMaxMin(index)
r := NewRange(AxisRangeOption{ r := NewRange(AxisRangeOption{
Painter: p, Painter: p,
@ -194,7 +198,7 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
// 高度需要减去x轴的高度 // 高度需要减去x轴的高度
Size: rangeHeight, Size: rangeHeight,
// 分隔数量 // 分隔数量
DivideCount: defaultAxisDivideCount, DivideCount: divideCount,
}) })
if yAxisOption.Min != nil && *yAxisOption.Min <= min { if yAxisOption.Min != nil && *yAxisOption.Min <= min {
r.min = *yAxisOption.Min r.min = *yAxisOption.Min
@ -346,6 +350,10 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {
}, },
} }
} }
if len(horizontalBarSeriesList) != 0 {
renderOpt.YAxisOptions[0].DivideCount = len(renderOpt.YAxisOptions[0].Data)
renderOpt.YAxisOptions[0].Unit = 1
}
renderResult, err := defaultRender(p, renderOpt) renderResult, err := defaultRender(p, renderOpt)
if err != nil { if err != nil {

View file

@ -26,6 +26,7 @@ func writeFile(buf []byte) error {
func main() { func main() {
values := [][]float64{ values := [][]float64{
{ {
8203,
18203, 18203,
23489, 23489,
29034, 29034,
@ -34,6 +35,7 @@ func main() {
630230, 630230,
}, },
{ {
9325,
19325, 19325,
23438, 23438,
31000, 31000,
@ -56,6 +58,7 @@ func main() {
"2012", "2012",
}), }),
charts.YAxisDataOptionFunc([]string{ charts.YAxisDataOptionFunc([]string{
"UN",
"Brazil", "Brazil",
"Indonesia", "Indonesia",
"USA", "USA",

View file

@ -47,6 +47,8 @@ type YAxisOption struct {
Color Color Color Color
// The flag for show axis, set this to *false will hide axis // The flag for show axis, set this to *false will hide axis
Show *bool Show *bool
DivideCount int
Unit int
isCategoryAxis bool isCategoryAxis bool
} }
@ -87,6 +89,7 @@ func (opt *YAxisOption) ToAxisOption(p *Painter) AxisOption {
SplitLineShow: true, SplitLineShow: true,
SplitLineColor: theme.GetAxisSplitLineColor(), SplitLineColor: theme.GetAxisSplitLineColor(),
Show: opt.Show, Show: opt.Show,
Unit: opt.Unit,
} }
if !opt.Color.IsZero() { if !opt.Color.IsZero() {
axisOpt.FontColor = opt.Color axisOpt.FontColor = opt.Color