This example creates a some standalone legends with borders around them.
()
| 39 | |
| 40 | // This example creates a some standalone legends with borders around them. |
| 41 | func ExampleLegend_standalone() { |
| 42 | c := vgimg.New(vg.Points(120), vg.Points(100)) |
| 43 | dc := draw.New(c) |
| 44 | |
| 45 | // These example thumbnailers could be replaced with any of Plotters |
| 46 | // in the plotter subpackage. |
| 47 | red := exampleThumbnailer{Color: color.NRGBA{R: 255, A: 255}} |
| 48 | green := exampleThumbnailer{Color: color.NRGBA{G: 255, A: 255}} |
| 49 | blue := exampleThumbnailer{Color: color.NRGBA{B: 255, A: 255}} |
| 50 | |
| 51 | l := plot.NewLegend() |
| 52 | l.Add("red", red) |
| 53 | l.Add("green", green) |
| 54 | l.Add("blue", blue) |
| 55 | l.Padding = vg.Millimeter |
| 56 | l.YPosition = draw.PosCenter |
| 57 | |
| 58 | // purpleRectangle draws a purple rectangle around the given Legend. |
| 59 | purpleRectangle := func(l plot.Legend) { |
| 60 | r := l.Rectangle(dc) |
| 61 | dc.StrokeLines(draw.LineStyle{ |
| 62 | Color: color.NRGBA{R: 255, B: 255, A: 255}, |
| 63 | Width: vg.Points(1), |
| 64 | }, []vg.Point{ |
| 65 | {X: r.Min.X, Y: r.Min.Y}, {X: r.Min.X, Y: r.Max.Y}, {X: r.Max.X, Y: r.Max.Y}, |
| 66 | {X: r.Max.X, Y: r.Min.Y}, {X: r.Min.X, Y: r.Min.Y}, |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | l.Draw(dc) |
| 71 | purpleRectangle(l) |
| 72 | |
| 73 | l.Left = true |
| 74 | l.Draw(dc) |
| 75 | purpleRectangle(l) |
| 76 | |
| 77 | l.Top = true |
| 78 | l.Draw(dc) |
| 79 | purpleRectangle(l) |
| 80 | |
| 81 | l.Left = false |
| 82 | l.Draw(dc) |
| 83 | purpleRectangle(l) |
| 84 | |
| 85 | w, err := os.Create("testdata/legend_standalone.png") |
| 86 | if err != nil { |
| 87 | panic(err) |
| 88 | } |
| 89 | defer w.Close() |
| 90 | |
| 91 | png := vgimg.PngCanvas{Canvas: c} |
| 92 | if _, err := png.WriteTo(w); err != nil { |
| 93 | panic(err) |
| 94 | } |
| 95 | } |