// WriterTo returns an io.WriterTo that will write the plot as // the specified image format. // // Supported formats are: // // eps, jpg|jpeg, pdf, png, svg, and tif|tiff. func (p *Plot) WriterTo(w, h vg.Length, format string) (io.WriterTo, error) { var c interface { vg.CanvasSizer io.WriterTo } switch format { case "eps": c = vgeps.New(w, h) case "jpg", "jpeg": c = vgimg.JpegCanvas{Canvas: vgimg.New(w, h)} case "pdf": c = vgpdf.New(w, h) case "png": c = vgimg.PngCanvas{Canvas: vgimg.New(w, h)} case "svg": c = vgsvg.New(w, h) case "tif", "tiff": c = vgimg.TiffCanvas{Canvas: vgimg.New(w, h)} default: return nil, fmt.Errorf("unsupported format: %q", format) } p.Draw(draw.New(c)) return c, nil }
func TestIssue179(t *testing.T) { scatter, err := plotter.NewScatter(plotter.XYs{{1, 1}, {0, 1}, {0, 0}}) if err != nil { log.Fatal(err) } p, err := plot.New() if err != nil { log.Fatal(err) } p.Add(scatter) p.HideAxes() c := vgimg.JpegCanvas{Canvas: vgimg.New(5.08*vg.Centimeter, 5.08*vg.Centimeter)} p.Draw(draw.New(c)) b := bytes.NewBuffer([]byte{}) if _, err = c.WriteTo(b); err != nil { t.Error(err) } f, err := os.Open(filepath.Join("testdata", "issue179.jpg")) if err != nil { t.Error(err) } defer f.Close() want, err := ioutil.ReadAll(f) if err != nil { t.Error(err) } if !bytes.Equal(b.Bytes(), want) { t.Error("Image mismatch") } }