| 150 | } |
| 151 | |
| 152 | func Example_writerToCanvas() { |
| 153 | p := plot.New() |
| 154 | p.Title.Text = "cos(x)" |
| 155 | p.X.Label.Text = "x" |
| 156 | p.Y.Label.Text = "f(x)" |
| 157 | |
| 158 | p.X.Min = -2 * math.Pi |
| 159 | p.X.Max = +2 * math.Pi |
| 160 | |
| 161 | fct := plotter.NewFunction(func(x float64) float64 { |
| 162 | return math.Cos(x) |
| 163 | }) |
| 164 | fct.Color = color.RGBA{B: 255, A: 255} |
| 165 | |
| 166 | p.Add(fct, plotter.NewGrid()) |
| 167 | |
| 168 | c := vgimg.PngCanvas{ |
| 169 | Canvas: vgimg.New(10*vg.Centimeter, 5*vg.Centimeter), |
| 170 | } |
| 171 | p.Draw(draw.New(c)) |
| 172 | |
| 173 | // Save image. |
| 174 | f, err := os.Create("testdata/cosine.png") |
| 175 | if err != nil { |
| 176 | log.Fatalf("could not create output image file: %+v", err) |
| 177 | } |
| 178 | defer f.Close() |
| 179 | |
| 180 | _, err = c.WriteTo(f) |
| 181 | if err != nil { |
| 182 | log.Fatalf("could not encode image to PNG: %+v", err) |
| 183 | } |
| 184 | |
| 185 | err = f.Close() |
| 186 | if err != nil { |
| 187 | log.Fatalf("could not close output image file: %+v", err) |
| 188 | } |
| 189 | } |