feat: pie chart render function

This commit is contained in:
vicanso 2022-02-03 21:00:01 +08:00
parent 445a781b04
commit eb45c6479e
7 changed files with 164 additions and 24 deletions

View file

@ -27,10 +27,9 @@ import (
) )
func barChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) { func barChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) {
d := result.d
bd, err := NewDraw(DrawOption{ d, err := NewDraw(DrawOption{
Parent: d, Parent: result.d,
}, PaddingOption(chart.Box{ }, PaddingOption(chart.Box{
Top: result.titleBox.Height(), Top: result.titleBox.Height(),
Left: YAxisWidth, Left: YAxisWidth,
@ -65,7 +64,7 @@ func barChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) {
h := int(yRange.getHeight(item.Value)) h := int(yRange.getHeight(item.Value))
bd.Bar(chart.Box{ d.Bar(chart.Box{
Top: barMaxHeight - h, Top: barMaxHeight - h,
Left: x, Left: x,
Right: x + barWidth, Right: x + barWidth,
@ -76,5 +75,5 @@ func barChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) {
} }
} }
return d, nil return result.d, nil
} }

View file

@ -23,6 +23,7 @@
package charts package charts
import ( import (
"errors"
"math" "math"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
@ -168,21 +169,37 @@ type basicRenderResult struct {
} }
func ChartRender(opt ChartOption) (*Draw, error) { func ChartRender(opt ChartOption) (*Draw, error) {
result, err := chartBasicRender(&opt) if len(opt.SeriesList) == 0 {
if err != nil { return nil, errors.New("series can not be nil")
return nil, err
} }
lineSeries := make([]Series, 0) lineSeries := make([]Series, 0)
barSeries := make([]Series, 0) barSeries := make([]Series, 0)
isPieChart := false
for index, item := range opt.SeriesList { for index, item := range opt.SeriesList {
item.index = index item.index = index
switch item.Type { switch item.Type {
case ChartTypePie:
isPieChart = true
case ChartTypeBar: case ChartTypeBar:
barSeries = append(barSeries, item) barSeries = append(barSeries, item)
default: default:
lineSeries = append(lineSeries, item) lineSeries = append(lineSeries, item)
} }
} }
// 如果指定了pie则以pie的形式处理不支持多类型图表
// pie不需要axis
if isPieChart {
opt.XAxis.Hidden = true
opt.YAxis.Hidden = true
}
result, err := chartBasicRender(&opt)
if err != nil {
return nil, err
}
if isPieChart {
return pieChartRender(opt, result)
}
if len(barSeries) != 0 { if len(barSeries) != 0 {
o := opt o := opt
o.SeriesList = barSeries o.SeriesList = barSeries
@ -231,18 +248,26 @@ func chartBasicRender(opt *ChartOption) (*basicRenderResult, error) {
return nil, err return nil, err
} }
// xAxis xAxisHeight := 0
xAxisHeight, xRange, err := drawXAxis(d, &opt.XAxis) var xRange *Range
if err != nil {
return nil, err if !opt.XAxis.Hidden {
// xAxis
xAxisHeight, xRange, err = drawXAxis(d, &opt.XAxis)
if err != nil {
return nil, err
}
} }
// 暂时仅支持单一yaxis // 暂时仅支持单一yaxis
yRange, err := drawYAxis(d, opt, xAxisHeight, chart.Box{ var yRange *Range
Top: titleBox.Height(), if !opt.YAxis.Hidden {
}) yRange, err = drawYAxis(d, opt, xAxisHeight, chart.Box{
if err != nil { Top: titleBox.Height(),
return nil, err })
if err != nil {
return nil, err
}
} }
return &basicRenderResult{ return &basicRenderResult{
xRange: xRange, xRange: xRange,

View file

@ -138,6 +138,10 @@ func (d *Draw) moveTo(x, y int) {
d.Render.MoveTo(x+d.Box.Left, y+d.Box.Top) d.Render.MoveTo(x+d.Box.Left, y+d.Box.Top)
} }
func (d *Draw) arcTo(cx, cy int, rx, ry, startAngle, delta float64) {
d.Render.ArcTo(cx+d.Box.Left, cy+d.Box.Top, rx, ry, startAngle, delta)
}
func (d *Draw) lineTo(x, y int) { func (d *Draw) lineTo(x, y int) {
d.Render.LineTo(x+d.Box.Left, y+d.Box.Top) d.Render.LineTo(x+d.Box.Left, y+d.Box.Top)
} }

View file

@ -29,11 +29,10 @@ import (
func lineChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) { func lineChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) {
d := result.d
theme := NewTheme(opt.Theme) theme := NewTheme(opt.Theme)
sd, err := NewDraw(DrawOption{ d, err := NewDraw(DrawOption{
Parent: d, Parent: result.d,
}, PaddingOption(chart.Box{ }, PaddingOption(chart.Box{
Top: result.titleBox.Height(), Top: result.titleBox.Height(),
Left: YAxisWidth, Left: YAxisWidth,
@ -60,7 +59,7 @@ func lineChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error)
if theme.IsDark() { if theme.IsDark() {
dotFillColor = seriesColor dotFillColor = seriesColor
} }
sd.Line(points, LineStyle{ d.Line(points, LineStyle{
StrokeColor: seriesColor, StrokeColor: seriesColor,
StrokeWidth: 2, StrokeWidth: 2,
DotColor: seriesColor, DotColor: seriesColor,
@ -70,5 +69,5 @@ func lineChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error)
} }
} }
return d, nil return result.d, nil
} }

111
pie_chart.go Normal file
View file

@ -0,0 +1,111 @@
// MIT License
// Copyright (c) 2022 Tree Xie
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package charts
import (
"math"
"github.com/wcharczuk/go-chart/v2"
)
const defaultRadiusPercent = 0.4
func getPieStyle(theme *Theme, index int) chart.Style {
seriesColor := theme.GetSeriesColor(index)
return chart.Style{
StrokeColor: seriesColor,
StrokeWidth: 1,
FillColor: seriesColor,
}
}
func pieChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) {
d, err := NewDraw(DrawOption{
Parent: result.d,
}, PaddingOption(chart.Box{
Top: result.titleBox.Height(),
}))
if err != nil {
return nil, err
}
values := make([]float64, len(opt.SeriesList))
total := float64(0)
for index, series := range opt.SeriesList {
value := float64(0)
for _, item := range series.Data {
value += item.Value
}
values[index] = value
total += value
}
r := d.Render
theme := NewTheme(opt.Theme)
box := d.Box
cx := box.Width() >> 1
cy := box.Height() >> 1
diameter := chart.MinInt(box.Width(), box.Height())
radius := float64(diameter) * defaultRadiusPercent
labelRadius := radius + 20
if len(values) == 1 {
getPieStyle(theme, 0).WriteToRenderer(r)
d.moveTo(cx, cy)
d.circle(radius, cx, cy)
} else {
currentValue := float64(0)
for index, v := range values {
getPieStyle(theme, index).WriteToRenderer(r)
d.moveTo(cx, cy)
start := chart.PercentToRadians(currentValue/total) - math.Pi/2
currentValue += v
delta := chart.PercentToRadians(v / total)
d.arcTo(cx, cy, radius, radius, start, delta)
d.lineTo(cx, cy)
r.Close()
r.FillStroke()
// label的角度为饼块中间
angle := start + delta/2
startx := cx + int(radius*math.Cos(angle))
starty := cy + int(radius*math.Sin(angle))
endx := cx + int(labelRadius*math.Cos(angle))
endy := cy + int(labelRadius*math.Sin(angle))
d.moveTo(startx, starty)
d.lineTo(endx, endy)
offset := 30
if endx < cx {
offset *= -1
}
d.moveTo(endx, endy)
d.lineTo(endx+offset, endy)
r.Stroke()
// TODO label show
}
}
return result.d, nil
}

View file

@ -28,6 +28,7 @@ type XAxisOption struct {
BoundaryGap *bool BoundaryGap *bool
Data []string Data []string
Theme string Theme string
Hidden bool
// TODO split number // TODO split number
} }

View file

@ -27,8 +27,9 @@ import (
) )
type YAxisOption struct { type YAxisOption struct {
Min *float64 Min *float64
Max *float64 Max *float64
Hidden bool
} }
const YAxisWidth = 40 const YAxisWidth = 40