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
// The color of split line
SplitLineColor Color
Unit int
}
func (a *axisPainter) Render() (Box, error) {
@ -159,12 +160,16 @@ func (a *axisPainter) Render() (Box, error) {
// 根据文本宽度计算较为符合的展示项
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)
// 偶数
if unit%2 == 0 && dataCount%(unit+1) == 0 {
unit++
}
}
width := 0
height := 0

View file

@ -186,6 +186,10 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
if len(opt.YAxisOptions) > index {
yAxisOption = opt.YAxisOptions[index]
}
divideCount := yAxisOption.DivideCount
if divideCount <= 0 {
divideCount = defaultAxisDivideCount
}
max, min := opt.SeriesList.GetMaxMin(index)
r := NewRange(AxisRangeOption{
Painter: p,
@ -194,7 +198,7 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
// 高度需要减去x轴的高度
Size: rangeHeight,
// 分隔数量
DivideCount: defaultAxisDivideCount,
DivideCount: divideCount,
})
if yAxisOption.Min != nil && *yAxisOption.Min <= 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)
if err != nil {

View file

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

View file

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