From be19cd728ac8252ac8abf9b068149600bf34a648 Mon Sep 17 00:00:00 2001 From: vicanso Date: Tue, 14 Dec 2021 23:26:34 +0800 Subject: [PATCH] refactor: adjust bar series --- bar_series.go | 29 ++++++++++++++++++++++++----- base_series.go | 5 +++-- charts.go | 25 ++++++++++++++++++++----- series.go | 14 +++++++++----- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/bar_series.go b/bar_series.go index 9e965b9..a2274f7 100644 --- a/bar_series.go +++ b/bar_series.go @@ -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) } - } diff --git a/base_series.go b/base_series.go index 673c0a6..ac49d21 100644 --- a/base_series.go +++ b/base_series.go @@ -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. diff --git a/charts.go b/charts.go index 5d0e0c4..e0d2925 100644 --- a/charts.go +++ b/charts.go @@ -41,9 +41,24 @@ type ( Theme string XAxis XAxis // YAxis Axis - Series []Series - Title Title - Legend Legend + 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 { diff --git a/series.go b/series.go index f0caf36..538f929 100644 --- a/series.go +++ b/series.go @@ -62,12 +62,16 @@ func GetSeries(series []Series, tickPosition chart.TickPosition, theme string) [ style := chart.Style{ StrokeWidth: lineStrokeWidth, StrokeColor: getSeriesColor(theme, index), - DotColor: getSeriesColor(theme, index), - DotWidth: dotWith, + // TODO 调整为通过dot with color 生成 + 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{ Name: item.Name, XValues: item.XValues,