| 113 | } |
| 114 | |
| 115 | func Example_inMemoryCanvas() { |
| 116 | p := plot.New() |
| 117 | p.Title.Text = "sin(x)" |
| 118 | p.X.Label.Text = "x" |
| 119 | p.Y.Label.Text = "f(x)" |
| 120 | |
| 121 | p.X.Min = -2 * math.Pi |
| 122 | p.X.Max = +2 * math.Pi |
| 123 | |
| 124 | fct := plotter.NewFunction(func(x float64) float64 { |
| 125 | return math.Sin(x) |
| 126 | }) |
| 127 | fct.Color = color.RGBA{R: 255, A: 255} |
| 128 | |
| 129 | p.Add(fct, plotter.NewGrid()) |
| 130 | |
| 131 | c := vgimg.New(10*vg.Centimeter, 5*vg.Centimeter) |
| 132 | p.Draw(draw.New(c)) |
| 133 | |
| 134 | // Save image. |
| 135 | f, err := os.Create("testdata/sine.png") |
| 136 | if err != nil { |
| 137 | log.Fatalf("could not create output image file: %+v", err) |
| 138 | } |
| 139 | defer f.Close() |
| 140 | |
| 141 | err = png.Encode(f, c.Image()) |
| 142 | if err != nil { |
| 143 | log.Fatalf("could not encode image to PNG: %+v", err) |
| 144 | } |
| 145 | |
| 146 | err = f.Close() |
| 147 | if err != nil { |
| 148 | log.Fatalf("could not close output image file: %+v", err) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func Example_writerToCanvas() { |
| 153 | p := plot.New() |