Skip to content

Commit

Permalink
plotter: fix handling of x/y-offsets in Labels glyph boxes
Browse files Browse the repository at this point in the history
This CL fixes the handling of the X/Y-offsets of plotter.Labels: they
were previously ignored in the glyph box computation.
This CL also adds a test that exercizes offsetted labels and their glyph box.

Fixes #710.
  • Loading branch information
sbinet committed Jul 1, 2021
1 parent eacbd7b commit a58aa90
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
9 changes: 5 additions & 4 deletions plotter/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,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
}
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
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.

0 comments on commit a58aa90

Please sign in to comment.