feat: support setting bar width for bar chart, #24
This commit is contained in:
parent
1f5b9d513e
commit
0a80e7056f
3 changed files with 14 additions and 5 deletions
10
bar_chart.go
10
bar_chart.go
|
|
@ -59,7 +59,8 @@ type BarChartOption struct {
|
||||||
// The option of title
|
// The option of title
|
||||||
Title TitleOption
|
Title TitleOption
|
||||||
// The legend option
|
// The legend option
|
||||||
Legend LegendOption
|
Legend LegendOption
|
||||||
|
BarWidth int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
|
func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
|
||||||
|
|
@ -86,7 +87,12 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
|
||||||
}
|
}
|
||||||
seriesCount := len(seriesList)
|
seriesCount := len(seriesList)
|
||||||
// 总的宽度-两个margin-(总数-1)的barMargin
|
// 总的宽度-两个margin-(总数-1)的barMargin
|
||||||
barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / len(seriesList)
|
barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / seriesCount
|
||||||
|
if opt.BarWidth > 0 && opt.BarWidth < barWidth {
|
||||||
|
barWidth = opt.BarWidth
|
||||||
|
// 重新计算margin
|
||||||
|
margin = (width - len(seriesList)*barWidth - barMargin*(seriesCount-1)) / 2
|
||||||
|
}
|
||||||
barMaxHeight := seriesPainter.Height()
|
barMaxHeight := seriesPainter.Height()
|
||||||
theme := opt.Theme
|
theme := opt.Theme
|
||||||
seriesNames := seriesList.Names()
|
seriesNames := seriesList.Names()
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,8 @@ type ChartOption struct {
|
||||||
SymbolShow *bool
|
SymbolShow *bool
|
||||||
// The stroke width of line chart
|
// The stroke width of line chart
|
||||||
LineStrokeWidth float64
|
LineStrokeWidth float64
|
||||||
|
// The bar with of bar chart
|
||||||
|
BarWidth int
|
||||||
// Fill the area of line chart
|
// Fill the area of line chart
|
||||||
FillArea bool
|
FillArea bool
|
||||||
// The child charts
|
// The child charts
|
||||||
|
|
|
||||||
|
|
@ -354,9 +354,10 @@ func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {
|
||||||
if len(barSeriesList) != 0 {
|
if len(barSeriesList) != 0 {
|
||||||
handler.Add(func() error {
|
handler.Add(func() error {
|
||||||
_, err := NewBarChart(p, BarChartOption{
|
_, err := NewBarChart(p, BarChartOption{
|
||||||
Theme: opt.theme,
|
Theme: opt.theme,
|
||||||
Font: opt.font,
|
Font: opt.font,
|
||||||
XAxis: opt.XAxis,
|
XAxis: opt.XAxis,
|
||||||
|
BarWidth: opt.BarWidth,
|
||||||
}).render(renderResult, barSeriesList)
|
}).render(renderResult, barSeriesList)
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue