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 { if opt.BarWidth > 0 && opt.BarWidth < barWidth {
barWidth = opt.BarWidth barWidth = opt.BarWidth
// 重新计算margin // 重新计算margin
margin = (width - len(seriesList)*barWidth - barMargin*(seriesCount-1)) / 2 margin = (width - seriesCount*barWidth - barMargin*(seriesCount-1)) / 2
} }
barMaxHeight := seriesPainter.Height() barMaxHeight := seriesPainter.Height()
theme := opt.Theme theme := opt.Theme

View file

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

View file

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

View file

@ -49,6 +49,7 @@ type HorizontalBarChartOption struct {
Title TitleOption Title TitleOption
// The legend option // The legend option
Legend LegendOption Legend LegendOption
BarHeight int
} }
// NewHorizontalBarChart returns a horizontal bar chart renderer // NewHorizontalBarChart returns a horizontal bar chart renderer
@ -82,7 +83,11 @@ func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList Seri
} }
seriesCount := len(seriesList) seriesCount := len(seriesList)
// 总的高度-两个margin-(总数-1)的barMargin // 总的高度-两个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 theme := opt.Theme