refactor: add label for pie
This commit is contained in:
parent
eb45c6479e
commit
dfba1ceafc
2 changed files with 27 additions and 5 deletions
5
chart.go
5
chart.go
|
|
@ -27,6 +27,7 @@ import (
|
|||
"math"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/wcharczuk/go-chart/v2"
|
||||
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||
)
|
||||
|
|
@ -55,6 +56,7 @@ type Series struct {
|
|||
}
|
||||
|
||||
type ChartOption struct {
|
||||
Font *truetype.Font
|
||||
Theme string
|
||||
Title TitleOption
|
||||
Legend LegendOption
|
||||
|
|
@ -71,6 +73,7 @@ type ChartOption struct {
|
|||
func (o *ChartOption) FillDefault(theme string) {
|
||||
t := NewTheme(theme)
|
||||
f, _ := chart.GetDefaultFont()
|
||||
o.Font = f
|
||||
if o.BackgroundColor.IsZero() {
|
||||
o.BackgroundColor = t.GetBackgroundColor()
|
||||
}
|
||||
|
|
@ -93,7 +96,7 @@ func (o *ChartOption) FillDefault(theme string) {
|
|||
}
|
||||
o.Legend.Theme = t
|
||||
if o.Legend.Style.FontSize == 0 {
|
||||
o.Legend.Style.FontSize = 8
|
||||
o.Legend.Style.FontSize =10
|
||||
}
|
||||
if o.Legend.Style.Font == nil {
|
||||
o.Legend.Style.Font = f
|
||||
|
|
|
|||
27
pie_chart.go
27
pie_chart.go
|
|
@ -25,6 +25,7 @@ package charts
|
|||
import (
|
||||
"math"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/wcharczuk/go-chart/v2"
|
||||
)
|
||||
|
||||
|
|
@ -78,11 +79,13 @@ func pieChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) {
|
|||
} else {
|
||||
currentValue := float64(0)
|
||||
for index, v := range values {
|
||||
getPieStyle(theme, index).WriteToRenderer(r)
|
||||
pieStyle := getPieStyle(theme, index)
|
||||
pieStyle.WriteToRenderer(r)
|
||||
d.moveTo(cx, cy)
|
||||
start := chart.PercentToRadians(currentValue/total) - math.Pi/2
|
||||
currentValue += v
|
||||
delta := chart.PercentToRadians(v / total)
|
||||
percent := (v / total)
|
||||
delta := chart.PercentToRadians(percent)
|
||||
d.arcTo(cx, cy, radius, radius, start, delta)
|
||||
d.lineTo(cx, cy)
|
||||
r.Close()
|
||||
|
|
@ -102,9 +105,25 @@ func pieChartRender(opt ChartOption, result *basicRenderResult) (*Draw, error) {
|
|||
offset *= -1
|
||||
}
|
||||
d.moveTo(endx, endy)
|
||||
d.lineTo(endx+offset, endy)
|
||||
endx += offset
|
||||
d.lineTo(endx, endy)
|
||||
r.Stroke()
|
||||
// TODO label show
|
||||
textStyle := chart.Style{
|
||||
FontColor: theme.GetTextColor(),
|
||||
FontSize: 10,
|
||||
Font: opt.Font,
|
||||
}
|
||||
textStyle.GetTextOptions().WriteToRenderer(r)
|
||||
text := humanize.FtoaWithDigits(percent*100, 2) + "%"
|
||||
textBox := r.MeasureText(text)
|
||||
textMargin := 3
|
||||
x := endx + textMargin
|
||||
y := endy + textBox.Height()>>1 - 1
|
||||
if offset < 0 {
|
||||
textWidth := textBox.Width()
|
||||
x = endx - textWidth - textMargin
|
||||
}
|
||||
d.text(text, x, y)
|
||||
}
|
||||
}
|
||||
return result.d, nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue