From bdcc871ab194dcaeeae1d934cc088bcffdff5fd2 Mon Sep 17 00:00:00 2001 From: vicanso Date: Thu, 3 Nov 2022 21:31:53 +0800 Subject: [PATCH] fix: fix series render of horizontal bar, #31 --- axis.go | 15 ++++++++++----- charts.go | 10 +++++++++- examples/horizontal_bar_chart/main.go | 3 +++ yaxis.go | 3 +++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/axis.go b/axis.go index 3c0484c..578813c 100644 --- a/axis.go +++ b/axis.go @@ -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,11 +160,15 @@ func (a *axisPainter) Render() (Box, error) { // 根据文本宽度计算较为符合的展示项 fitTextCount := ceilFloatToInt(float64(top.Width()) / textFillWidth) - unit := ceilFloatToInt(float64(dataCount) / float64(fitTextCount)) - unit = chart.MaxInt(unit, opt.SplitNumber) - // 偶数 - if unit%2 == 0 && dataCount%(unit+1) == 0 { - unit++ + 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 diff --git a/charts.go b/charts.go index b66437c..d6745d3 100644 --- a/charts.go +++ b/charts.go @@ -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 { diff --git a/examples/horizontal_bar_chart/main.go b/examples/horizontal_bar_chart/main.go index 8b996b6..a0f5bda 100644 --- a/examples/horizontal_bar_chart/main.go +++ b/examples/horizontal_bar_chart/main.go @@ -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", diff --git a/yaxis.go b/yaxis.go index eb9034c..bece2cc 100644 --- a/yaxis.go +++ b/yaxis.go @@ -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