Draw draws the legend to the given draw.Canvas.
(c draw.Canvas)
| 94 | |
| 95 | // Draw draws the legend to the given draw.Canvas. |
| 96 | func (l *Legend) Draw(c draw.Canvas) { |
| 97 | iconx := c.Min.X |
| 98 | sty := l.TextStyle |
| 99 | em := sty.Rectangle(" ") |
| 100 | textx := iconx + l.ThumbnailWidth + em.Max.X |
| 101 | if !l.Left { |
| 102 | iconx = c.Max.X - l.ThumbnailWidth |
| 103 | textx = iconx - em.Max.X |
| 104 | sty.XAlign-- |
| 105 | } |
| 106 | textx += l.XOffs |
| 107 | iconx += l.XOffs |
| 108 | |
| 109 | descent := sty.FontExtents().Descent |
| 110 | enth := l.entryHeight() |
| 111 | y := c.Max.Y - enth - descent |
| 112 | if !l.Top { |
| 113 | y = c.Min.Y + (enth+l.Padding)*(vg.Length(len(l.entries))-1) |
| 114 | } |
| 115 | y += l.YOffs |
| 116 | |
| 117 | icon := &draw.Canvas{ |
| 118 | Canvas: c.Canvas, |
| 119 | Rectangle: vg.Rectangle{ |
| 120 | Min: vg.Point{X: iconx, Y: y}, |
| 121 | Max: vg.Point{X: iconx + l.ThumbnailWidth, Y: y + enth}, |
| 122 | }, |
| 123 | } |
| 124 | |
| 125 | if l.YPosition < draw.PosBottom || draw.PosTop < l.YPosition { |
| 126 | panic("plot: invalid vertical offset for the legend's entries") |
| 127 | } |
| 128 | yoff := vg.Length(l.YPosition-draw.PosBottom) / 2 |
| 129 | yoff += descent |
| 130 | |
| 131 | for _, e := range l.entries { |
| 132 | for _, t := range e.thumbs { |
| 133 | t.Thumbnail(icon) |
| 134 | } |
| 135 | yoffs := (enth - descent - sty.Rectangle(e.text).Max.Y) / 2 |
| 136 | yoffs += yoff |
| 137 | c.FillText(sty, vg.Point{X: textx, Y: icon.Min.Y + yoffs}, e.text) |
| 138 | icon.Min.Y -= enth + l.Padding |
| 139 | icon.Max.Y -= enth + l.Padding |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Rectangle returns the extent of the Legend. |
| 144 | func (l *Legend) Rectangle(c draw.Canvas) vg.Rectangle { |