Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plotter: fix handling of x/y-offsets in Labels glyph boxes #711

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions plotter/boxplot.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ func (b *BoxPlot) OutsideLabels(labels Labeller) (*Labels, error) {
if err != nil {
return nil, err
}
ls.XOffset += b.GlyphStyle.Radius / 2
ls.YOffset += b.GlyphStyle.Radius / 2
off := 0.5 * b.GlyphStyle.Radius
ls.Offset = ls.Offset.Add(vg.Point{X: off, Y: off})
return ls, nil
}

Expand Down Expand Up @@ -421,8 +421,8 @@ func (b *horizBoxPlot) OutsideLabels(labels Labeller) (*Labels, error) {
if err != nil {
return nil, err
}
ls.XOffset += b.GlyphStyle.Radius / 2
ls.YOffset += b.GlyphStyle.Radius / 2
off := 0.5 * b.GlyphStyle.Radius
ls.Offset = ls.Offset.Add(vg.Point{X: off, Y: off})
return ls, nil
}

Expand Down
17 changes: 8 additions & 9 deletions plotter/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ type Labels struct {
// can have a different text style.
TextStyle []text.Style

// XOffset and YOffset are added directly to the final
// label X and Y location respectively.
XOffset, YOffset vg.Length
// Offset is added directly to the final label location.
Offset vg.Point
}

// NewLabels returns a new Labels using the DefaultFont and
Expand Down Expand Up @@ -80,8 +79,7 @@ func (l *Labels) Plot(c draw.Canvas, p *plot.Plot) {
if !c.Contains(pt) {
continue
}
pt.X += l.XOffset
pt.Y += l.YOffset
pt = pt.Add(l.Offset)
c.FillText(l.TextStyle[i], pt, label)
}
}
Expand All @@ -97,10 +95,11 @@ func (l *Labels) DataRange() (xmin, xmax, ymin, ymax float64) {
func (l *Labels) GlyphBoxes(p *plot.Plot) []plot.GlyphBox {
bs := make([]plot.GlyphBox, len(l.Labels))
for i, label := range l.Labels {
bs[i].X = p.X.Norm(l.XYs[i].X)
bs[i].Y = p.Y.Norm(l.XYs[i].Y)
sty := l.TextStyle[i]
bs[i].Rectangle = sty.Rectangle(label)
bs[i] = plot.GlyphBox{
X: p.X.Norm(l.XYs[i].X),
Y: p.Y.Norm(l.XYs[i].Y),
Rectangle: l.TextStyle[i].Rectangle(label).Add(l.Offset),
}
}
return bs
}
Comment on lines 95 to 105
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you made it happen :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Team work; I wouldn't have looked without prompting.

Expand Down
3 changes: 1 addition & 2 deletions plotter/labels_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func ExampleLabels() {
{X: -5, Y: 15},
},
Labels: []string{"A", "B", "C", "D"},
},
)
})
if err != nil {
log.Fatalf("could not creates labels plotter: %+v", err)
}
Expand Down
32 changes: 31 additions & 1 deletion plotter/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@ func TestLabelsWithGlyphBoxes(t *testing.T) {
sty.YAlign = draw.YBottom
}

// label with positive x/y-offsets
loffp, err := plotter.NewLabels(plotter.XYLabels{
XYs: []plotter.XY{{X: left}},
Labels: []string{"Bg"},
})
if err != nil {
t.Fatalf("could not creates labels plotter: %+v", err)
}
for i := range loffp.TextStyle {
sty := &loffp.TextStyle[i]
sty.Font.Size = vg.Length(fontSize)
sty.Color = color.RGBA{G: 255, A: 255}
}
loffp.Offset = vg.Point{X: 75, Y: 75}

// label with negative x/y-offsets
loffm, err := plotter.NewLabels(plotter.XYLabels{
XYs: []plotter.XY{{X: left}},
Labels: []string{"Bg"},
})
if err != nil {
t.Fatalf("could not creates labels plotter: %+v", err)
}
for i := range loffm.TextStyle {
sty := &loffm.TextStyle[i]
sty.Font.Size = vg.Length(fontSize)
sty.Color = color.RGBA{B: 255, A: 255}
}
loffm.Offset = vg.Point{X: -40, Y: -40}

m5 := plotter.NewFunction(func(float64) float64 { return -0.5 })
m5.LineStyle.Color = color.RGBA{R: 255, A: 255}

Expand All @@ -111,7 +141,7 @@ func TestLabelsWithGlyphBoxes(t *testing.T) {
p5 := plotter.NewFunction(func(float64) float64 { return +0.5 })
p5.LineStyle.Color = color.RGBA{B: 255, A: 255}

p.Add(labels, lred, m5, l0, p5)
p.Add(labels, lred, m5, l0, p5, loffp, loffm)
p.Add(plotter.NewGrid())
p.Add(plotter.NewGlyphBoxes())

Expand Down
8 changes: 4 additions & 4 deletions plotter/quartile.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func (b *QuartPlot) OutsideLabels(labels Labeller) (*Labels, error) {
if err != nil {
return nil, err
}
ls.XOffset += b.MedianStyle.Radius / 2
ls.YOffset += b.MedianStyle.Radius / 2
off := 0.5 * b.MedianStyle.Radius
ls.Offset = ls.Offset.Add(vg.Point{X: off, Y: off})
return ls, nil
}

Expand Down Expand Up @@ -269,8 +269,8 @@ func (b *horizQuartPlot) OutsideLabels(labels Labeller) (*Labels, error) {
if err != nil {
return nil, err
}
ls.XOffset += b.MedianStyle.Radius / 2
ls.YOffset += b.MedianStyle.Radius / 2
off := 0.5 * b.MedianStyle.Radius
ls.Offset = ls.Offset.Add(vg.Point{X: off, Y: off})
return ls, nil
}

Expand Down
Binary file modified plotter/testdata/horizontalBoxPlot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/labels_glyphboxes_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified plotter/testdata/verticalBoxPlot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions vg/geom.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func (r Rectangle) Size() Point {
}
}

// Add returns the rectangle r translated by p.
func (r Rectangle) Add(p Point) Rectangle {
return Rectangle{
Min: r.Min.Add(p),
Max: r.Max.Add(p),
}
}

// Path returns the path of a Rect specified by its
// upper left corner, width and height.
func (r Rectangle) Path() (p Path) {
Expand Down