diff --git a/bar_chart.go b/bar_chart.go index 2addd17..d798c07 100644 --- a/bar_chart.go +++ b/bar_chart.go @@ -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 diff --git a/chart_option.go b/chart_option.go index 447ef52..f3bf2cb 100644 --- a/chart_option.go +++ b/chart_option.go @@ -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 diff --git a/charts.go b/charts.go index f8c94a3..c7923f1 100644 --- a/charts.go +++ b/charts.go @@ -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 diff --git a/horizontal_bar_chart.go b/horizontal_bar_chart.go index 30a9b7d..8ffac44 100644 --- a/horizontal_bar_chart.go +++ b/horizontal_bar_chart.go @@ -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