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
cl := canvasBox.Left
columnWidth := canvasBox.Width() / bs.Len()
// 块间隔
columnMargin := columnWidth / 10
minColumnMargin := 2
if columnMargin < minColumnMargin {
columnMargin = minColumnMargin
}
margin := bs.Margin
if margin <= 0 {
margin = defaultBarMargin
}
barWidth := bs.BarWidth
if barWidth <= 0 {
barWidth = canvasBox.Width() / (bs.Len() * bs.Count)
// 如果margin大于column margin
if margin > columnMargin {
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++ {
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)
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,
}, style)
}
}

View file

@ -79,10 +79,11 @@ func (cs BaseSeries) GetValues(index int) (float64, float64) {
// GetFirstValues gets the first x,y values.
func (cs BaseSeries) GetFirstValues() (float64, float64) {
index := 0
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.

View file

@ -44,6 +44,21 @@ type (
Series []Series
Title Title
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)
}
func New(opt Option) *chart.Chart {
tickPosition := chart.TickPositionBetweenTicks
// tickPosition = chart.TickPositionUnset
tickPosition := opt.TickPosition
xAxis, xValues := GetXAxisAndValues(opt.XAxis, tickPosition, opt.Theme)
@ -92,6 +106,7 @@ func New(opt Option) *chart.Chart {
YAxis: GetYAxis(opt.Theme),
Series: GetSeries(opt.Series, tickPosition, opt.Theme),
}
// TODO 校验xAxis与yAxis的数量是否一致
// 设置secondary的样式
c.YAxisSecondary.Style = c.YAxis.Style
if legendSize != 0 {

View file

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