refactor: adjust bar series

This commit is contained in:
vicanso 2021-12-14 23:26:34 +08:00
parent f3009b965f
commit be19cd728a
4 changed files with 56 additions and 17 deletions

View file

@ -52,19 +52,39 @@ func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange
cb := canvasBox.Bottom cb := canvasBox.Bottom
cl := canvasBox.Left cl := canvasBox.Left
columnWidth := canvasBox.Width() / bs.Len()
// 块间隔
columnMargin := columnWidth / 10
minColumnMargin := 2
if columnMargin < minColumnMargin {
columnMargin = minColumnMargin
}
margin := bs.Margin margin := bs.Margin
if margin <= 0 { if margin <= 0 {
margin = defaultBarMargin margin = defaultBarMargin
} }
barWidth := bs.BarWidth // 如果margin大于column margin
if barWidth <= 0 { if margin > columnMargin {
barWidth = canvasBox.Width() / (bs.Len() * bs.Count) margin = columnMargin
}
allBarMarginWidth := (bs.Count - 1) * margin
barWidth := ((columnWidth - 2*columnMargin) - allBarMarginWidth) / bs.Count
if bs.BarWidth > 0 && bs.BarWidth < barWidth {
barWidth = bs.BarWidth
// 重新计息columnMargin
columnMargin = (columnWidth - allBarMarginWidth - (bs.Count * barWidth)) / 2
} }
for i := 0; i < bs.Len(); i++ { for i := 0; i < bs.Len(); i++ {
vx, vy := bs.GetValues(i) vx, vy := bs.GetValues(i)
x := cl + xrange.Translate(vx) + bs.Index*(margin+barWidth) + bs.Offset x := cl + xrange.Translate(vx)
// 由于bar是居中展示因此需要往前移一个显示块
x += (-columnWidth + columnMargin)
// 计算是第几个bar位置右偏
x += bs.Index * (margin + barWidth)
y := cb - yrange.Translate(vy) y := cb - yrange.Translate(vy)
chart.Draw.Box(r, chart.Box{ chart.Draw.Box(r, chart.Box{
@ -74,5 +94,4 @@ func (bs BarSeries) Render(r chart.Renderer, canvasBox chart.Box, xrange, yrange
Bottom: canvasBox.Bottom - 1, Bottom: canvasBox.Bottom - 1,
}, style) }, style)
} }
} }

View file

@ -79,10 +79,11 @@ func (cs BaseSeries) GetValues(index int) (float64, float64) {
// GetFirstValues gets the first x,y values. // GetFirstValues gets the first x,y values.
func (cs BaseSeries) GetFirstValues() (float64, float64) { func (cs BaseSeries) GetFirstValues() (float64, float64) {
index := 0
if cs.TickPosition == chart.TickPositionBetweenTicks { if cs.TickPosition == chart.TickPositionBetweenTicks {
return cs.XValues[1], cs.YValues[1] index++
} }
return cs.XValues[0], cs.YValues[0] return cs.XValues[index], cs.YValues[index]
} }
// GetLastValues gets the last x,y values. // GetLastValues gets the last x,y values.

View file

@ -41,9 +41,24 @@ type (
Theme string Theme string
XAxis XAxis XAxis XAxis
// YAxis Axis // YAxis Axis
Series []Series Series []Series
Title Title Title Title
Legend Legend Legend Legend
TickPosition chart.TickPosition
}
ECharOption struct {
Title struct {
Text string
TextStyle struct {
Color string
FontFamily string
}
}
XAxis struct {
Type string
BoundaryGap bool
Data []string
}
} }
) )
@ -69,8 +84,7 @@ func ToSVG(c *chart.Chart) ([]byte, error) {
return render(c, chart.SVG) return render(c, chart.SVG)
} }
func New(opt Option) *chart.Chart { func New(opt Option) *chart.Chart {
tickPosition := chart.TickPositionBetweenTicks tickPosition := opt.TickPosition
// tickPosition = chart.TickPositionUnset
xAxis, xValues := GetXAxisAndValues(opt.XAxis, tickPosition, opt.Theme) xAxis, xValues := GetXAxisAndValues(opt.XAxis, tickPosition, opt.Theme)
@ -92,6 +106,7 @@ func New(opt Option) *chart.Chart {
YAxis: GetYAxis(opt.Theme), YAxis: GetYAxis(opt.Theme),
Series: GetSeries(opt.Series, tickPosition, opt.Theme), Series: GetSeries(opt.Series, tickPosition, opt.Theme),
} }
// TODO 校验xAxis与yAxis的数量是否一致
// 设置secondary的样式 // 设置secondary的样式
c.YAxisSecondary.Style = c.YAxis.Style c.YAxisSecondary.Style = c.YAxis.Style
if legendSize != 0 { if legendSize != 0 {

View file

@ -62,12 +62,16 @@ func GetSeries(series []Series, tickPosition chart.TickPosition, theme string) [
style := chart.Style{ style := chart.Style{
StrokeWidth: lineStrokeWidth, StrokeWidth: lineStrokeWidth,
StrokeColor: getSeriesColor(theme, index), StrokeColor: getSeriesColor(theme, index),
DotColor: getSeriesColor(theme, index), // TODO 调整为通过dot with color 生成
DotWidth: dotWith, DotColor: getSeriesColor(theme, index),
DotWidth: dotWith,
}
// 如果居中,需要多增加一个点
if tickPosition == chart.TickPositionBetweenTicks {
item.Data = append([]float64{
0.0,
}, item.Data...)
} }
item.Data = append([]float64{
0.0,
}, item.Data...)
baseSeries := BaseSeries{ baseSeries := BaseSeries{
Name: item.Name, Name: item.Name,
XValues: item.XValues, XValues: item.XValues,