Skip to content

Commit

Permalink
hplot{,vgop}: add support for Save("foo.json")
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Sep 8, 2023
1 parent e1cbaa6 commit 970796d
Show file tree
Hide file tree
Showing 5 changed files with 1,433 additions and 5 deletions.
10 changes: 7 additions & 3 deletions hplot/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"strings"

"go-hep.org/x/hep/hplot/vgop"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgeps"
Expand All @@ -32,7 +33,7 @@ type Drawer interface {
//
// Supported extensions are:
//
// .eps, .jpg, .jpeg, .pdf, .png, .svg, .tex, .tif and .tiff.
// .eps, .jpg, .jpeg, .json, .pdf, .png, .svg, .tex, .tif and .tiff.
//
// If w or h are <= 0, the value is chosen such that it follows the Golden Ratio.
// If w and h are <= 0, the values are chosen such that they follow the Golden Ratio
Expand Down Expand Up @@ -95,7 +96,7 @@ func Save(p Drawer, w, h vg.Length, fnames ...string) (err error) {
// WriterTo returns an io.WriterTo that will write the plots as
// the specified image format.
//
// # Supported formats are the same ones than hplot.Plot.WriterTo
// Supported formats are the same ones than hplot.Save.
//
// If w or h are <= 0, the value is chosen such that it follows the Golden Ratio.
// If w and h are <= 0, the values are chosen such that they follow the Golden Ratio
Expand All @@ -122,7 +123,7 @@ func WriterTo(p Drawer, w, h vg.Length, format string) (io.WriterTo, error) {
//
// Supported formats are:
//
// eps, jpg|jpeg, pdf, png, svg, tex and tif|tiff.
// eps, jpg|jpeg, json, pdf, png, svg, tex and tif|tiff.
func newFormattedCanvas(w, h vg.Length, format string, dpi float64) (vg.CanvasWriterTo, error) {
var c vg.CanvasWriterTo
switch format {
Expand All @@ -135,6 +136,9 @@ func newFormattedCanvas(w, h vg.Length, format string, dpi float64) (vg.CanvasWr
vgimg.UseWH(w, h),
)}

case "json":
c = vgop.NewJSON(vgop.WithSize(w, h))

case "pdf":
c = vgpdf.New(w, h)

Expand Down
2 changes: 1 addition & 1 deletion hplot/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (p *Plot) Add(ps ...plot.Plotter) {
//
// Supported extensions are:
//
// .eps, .jpg, .jpeg, .pdf, .png, .svg, .tex, .tif and .tiff.
// .eps, .jpg, .jpeg, .json, .pdf, .png, .svg, .tex, .tif and .tiff.
//
// If w or h are <= 0, the value is chosen such that it follows the Golden Ratio.
// If w and h are <= 0, the values are chosen such that they follow the Golden Ratio
Expand Down
2 changes: 1 addition & 1 deletion hplot/tiledplot.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (tp *TiledPlot) Draw(c draw.Canvas) {
// Save saves the plots to an image file.
// The file format is determined by the extension.
//
// Supported extensions are the same ones than hplot.Plot.Save.
// Supported extensions are the same ones than hplot.Save.
//
// If w or h are <= 0, the value is chosen such that it follows the Golden Ratio.
// If w and h are <= 0, the values are chosen such that they follow the Golden Ratio
Expand Down
Loading

0 comments on commit 970796d

Please sign in to comment.