feat: support rounded rect for bar chart
This commit is contained in:
parent
5a69c3e5a3
commit
9b7634c2c2
5 changed files with 156 additions and 8 deletions
27
bar_chart.go
27
bar_chart.go
|
|
@ -142,14 +142,25 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
|
||||||
}
|
}
|
||||||
top := barMaxHeight - h
|
top := barMaxHeight - h
|
||||||
|
|
||||||
seriesPainter.OverrideDrawingStyle(Style{
|
if series.RoundRadius <= 0 {
|
||||||
FillColor: fillColor,
|
seriesPainter.OverrideDrawingStyle(Style{
|
||||||
}).Rect(chart.Box{
|
FillColor: fillColor,
|
||||||
Top: top,
|
}).Rect(chart.Box{
|
||||||
Left: x,
|
Top: top,
|
||||||
Right: x + barWidth,
|
Left: x,
|
||||||
Bottom: barMaxHeight - 1,
|
Right: x + barWidth,
|
||||||
})
|
Bottom: barMaxHeight - 1,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
seriesPainter.OverrideDrawingStyle(Style{
|
||||||
|
FillColor: fillColor,
|
||||||
|
}).RoundedRect(chart.Box{
|
||||||
|
Top: top,
|
||||||
|
Left: x,
|
||||||
|
Right: x + barWidth,
|
||||||
|
Bottom: barMaxHeight - 1,
|
||||||
|
}, series.RoundRadius)
|
||||||
|
}
|
||||||
// 用于生成marker point
|
// 用于生成marker point
|
||||||
points[j] = Point{
|
points[j] = Point{
|
||||||
// 居中的位置
|
// 居中的位置
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
42
painter.go
42
painter.go
|
|
@ -803,6 +803,48 @@ func (p *Painter) Rect(box Box) *Painter {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Painter) RoundedRect(box Box, radius int) *Painter {
|
||||||
|
r := (box.Right - box.Left) / 2
|
||||||
|
if radius > r {
|
||||||
|
radius = r
|
||||||
|
}
|
||||||
|
rx := float64(radius)
|
||||||
|
ry := float64(radius)
|
||||||
|
p.MoveTo(box.Left+radius, box.Top)
|
||||||
|
p.LineTo(box.Right-radius, box.Top)
|
||||||
|
|
||||||
|
cx := box.Right - radius
|
||||||
|
cy := box.Top + radius
|
||||||
|
// right top
|
||||||
|
p.ArcTo(cx, cy, rx, ry, -math.Pi/2, math.Pi/2)
|
||||||
|
|
||||||
|
p.LineTo(box.Right, box.Bottom-radius)
|
||||||
|
|
||||||
|
// right bottom
|
||||||
|
cx = box.Right - radius
|
||||||
|
cy = box.Bottom - radius
|
||||||
|
p.ArcTo(cx, cy, rx, ry, 0.0, math.Pi/2)
|
||||||
|
|
||||||
|
p.LineTo(box.Left+radius, box.Bottom)
|
||||||
|
|
||||||
|
// left bottom
|
||||||
|
cx = box.Left + radius
|
||||||
|
cy = box.Bottom - radius
|
||||||
|
p.ArcTo(cx, cy, rx, ry, math.Pi/2, math.Pi/2)
|
||||||
|
|
||||||
|
p.LineTo(box.Left, box.Top+radius)
|
||||||
|
|
||||||
|
// left top
|
||||||
|
cx = box.Left + radius
|
||||||
|
cy = box.Top + radius
|
||||||
|
p.ArcTo(cx, cy, rx, ry, math.Pi, math.Pi/2)
|
||||||
|
|
||||||
|
p.Close()
|
||||||
|
p.FillStroke()
|
||||||
|
p.Fill()
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Painter) LegendLineDot(box Box) *Painter {
|
func (p *Painter) LegendLineDot(box Box) *Painter {
|
||||||
width := box.Width()
|
width := box.Width()
|
||||||
height := box.Height()
|
height := box.Height()
|
||||||
|
|
|
||||||
|
|
@ -343,6 +343,29 @@ func TestPainter(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRoundedRect(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
p, err := NewPainter(PainterOptions{
|
||||||
|
Width: 400,
|
||||||
|
Height: 300,
|
||||||
|
Type: ChartOutputSVG,
|
||||||
|
})
|
||||||
|
assert.Nil(err)
|
||||||
|
p.OverrideDrawingStyle(Style{
|
||||||
|
FillColor: drawing.ColorWhite,
|
||||||
|
StrokeWidth: 1,
|
||||||
|
StrokeColor: drawing.ColorWhite,
|
||||||
|
}).RoundedRect(Box{
|
||||||
|
Left: 10,
|
||||||
|
Right: 30,
|
||||||
|
Bottom: 150,
|
||||||
|
Top: 10,
|
||||||
|
}, 5)
|
||||||
|
buf, err := p.Bytes()
|
||||||
|
assert.Nil(err)
|
||||||
|
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 15 10\nL 25 10\nL 25 10\nA 5 5 90.00 0 1 30 15\nL 30 145\nL 30 145\nA 5 5 90.00 0 1 25 150\nL 15 150\nL 15 150\nA 5 5 90.00 0 1 10 145\nL 10 15\nL 10 15\nA 5 5 90.00 0 1 15 10\nZ\" style=\"stroke-width:1;stroke:rgba(255,255,255,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(255,255,255,1.0);fill:rgba(255,255,255,1.0)\"/></svg>", string(buf))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPainterTextFit(t *testing.T) {
|
func TestPainterTextFit(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
p, err := NewPainter(PainterOptions{
|
p, err := NewPainter(PainterOptions{
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,8 @@ type Series struct {
|
||||||
Name string
|
Name string
|
||||||
// Radius for Pie chart, e.g.: 40%, default is "40%"
|
// Radius for Pie chart, e.g.: 40%, default is "40%"
|
||||||
Radius string
|
Radius string
|
||||||
|
// Round for bar chart
|
||||||
|
RoundRadius int
|
||||||
// Mark point for series
|
// Mark point for series
|
||||||
MarkPoint SeriesMarkPoint
|
MarkPoint SeriesMarkPoint
|
||||||
// Make line for series
|
// Make line for series
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue