feat: support bar height for horizontal bar chart

This commit is contained in:
vicanso 2022-09-29 20:20:54 +08:00
parent 0a80e7056f
commit 6652ece0fe
4 changed files with 11 additions and 3 deletions

View file

@ -91,7 +91,7 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
if opt.BarWidth > 0 && opt.BarWidth < barWidth {
barWidth = opt.BarWidth
// 重新计算margin
margin = (width - len(seriesList)*barWidth - barMargin*(seriesCount-1)) / 2
margin = (width - seriesCount*barWidth - barMargin*(seriesCount-1)) / 2
}
barMaxHeight := seriesPainter.Height()
theme := opt.Theme

View file

@ -68,6 +68,8 @@ type ChartOption struct {
LineStrokeWidth float64
// The bar with of bar chart
BarWidth int
// The bar height of horizontal bar chart
BarHeight int
// Fill the area of line chart
FillArea bool
// The child charts

View file

@ -369,6 +369,7 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {
_, err := NewHorizontalBarChart(p, HorizontalBarChartOption{
Theme: opt.theme,
Font: opt.font,
BarHeight: opt.BarHeight,
YAxisOptions: opt.YAxisOptions,
}).render(renderResult, horizontalBarSeriesList)
return err

View file

@ -48,7 +48,8 @@ type HorizontalBarChartOption struct {
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
Legend LegendOption
BarHeight int
}
// NewHorizontalBarChart returns a horizontal bar chart renderer
@ -82,7 +83,11 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
}
seriesCount := len(seriesList)
// 总的高度-两个margin-(总数-1)的barMargin
barHeight := (height - 2*margin - barMargin*(seriesCount-1)) / len(seriesList)
barHeight := (height - 2*margin - barMargin*(seriesCount-1)) / seriesCount
if opt.BarHeight > 0 && opt.BarHeight < barHeight {
barHeight = opt.BarHeight
margin = (height - seriesCount*barHeight - barMargin*(seriesCount-1)) / 2
}
theme := opt.Theme