Skip to content

Commit

Permalink
Want to make glot more object oriented
Browse files Browse the repository at this point in the history
  • Loading branch information
szaydel committed Feb 24, 2018
1 parent 06cb684 commit 7d63f3b
Show file tree
Hide file tree
Showing 10 changed files with 635 additions and 139 deletions.
25 changes: 20 additions & 5 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (plot *Plot) SetLogscale(axis string, base int) error {
// plot.AddPointGroup("Sample 1", "lines", []float64{2, 3, 4, 1})
// plot.SetTitle("Test Results")
// plot.SetYrange(-2,2)
func (plot *Plot) SetYrange(start int, end int) error {
func (plot *Plot) SetYrange(start, end int) error {
return plot.Cmd(fmt.Sprintf("set yrange [%d:%d]", start, end))
}

Expand Down Expand Up @@ -174,7 +174,22 @@ func (plot *Plot) SavePlot(filename string) (err error) {
if plot.nplots == 0 {
return &gnuplotError{fmt.Sprintf("This plot has 0 curves and therefore its a redundant plot and it can't be printed.")}
}
outputFormat := "set terminal " + plot.format
outputFormat := "set terminal " + plot.format.String()
fmt.Printf("outputFormat: %s\n", outputFormat)
plot.CheckedCmd(outputFormat)
outputFileCommand := "set output " + "'" + filename + "'"
fmt.Printf("outputFileCommand: %s\n", outputFileCommand)
plot.CheckedCmd(outputFileCommand)
plot.CheckedCmd("replot ")
return nil
}

func (plot *Plot) SaveHistogram(filename string) (err error) {
if plot.nplots == 0 {
return &gnuplotError{fmt.Sprintf("This plot has 0 curves and therefore its a redundant plot and it can't be printed.")}
}
plot.style = Histogram
outputFormat := "set terminal " + plot.format.String()
plot.CheckedCmd(outputFormat)
outputFileCommand := "set output" + "'" + filename + "'"
plot.CheckedCmd(outputFileCommand)
Expand All @@ -196,9 +211,9 @@ func (plot *Plot) SavePlot(filename string) (err error) {
// plot.SetFormat("pdf")
// plot.SavePlot("1.pdf")
// NOTE: png is default format for saving files.
func (plot *Plot) SetFormat(newformat string) error {
allowed := []string{
"png", "pdf"}
func (plot *Plot) SetFormat(newformat PlotFormat) error {
allowed := []PlotFormat{
Png, Pdf}
for _, s := range allowed {
if newformat == s {
plot.format = newformat
Expand Down
115 changes: 106 additions & 9 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,121 @@ package glot

import "testing"

func TestSetLabels(t *testing.T) {
var Args = PlotArgs{
Debug: true,
Persist: true,
}

func TestSetLabels3Dims(t *testing.T) {
dimensions := 3
persist := false
debug := false
plot, _ := NewPlot(dimensions, persist, debug)
plot, _ := NewPlot(dimensions, Args)
err := plot.SetLabels()
if err == nil {
t.Error("SetLabels raises error when empty string is passed")
}
}

func TestSetLabels2Dims(t *testing.T) {
dimensions := 2
plot, _ := NewPlot(dimensions, Args)
err := plot.SetLabels()
if err == nil {
t.Error("SetLabels raises error when empty string is passed")
}
}

func TestSetLabels1Dims(t *testing.T) {
dimensions := 1
plot, _ := NewPlot(dimensions, Args)
err := plot.SetLabels()
if err == nil {
t.Error("SetLabels raises error when empty string is passed")
}
}

func TestSetFormat(t *testing.T) {
func TestSetFormat3Dims(t *testing.T) {
dimensions := 3
persist := false
debug := false
plot, _ := NewPlot(dimensions, persist, debug)
err := plot.SetFormat("tls")

// Test unsupported formats
plot, _ := NewPlot(dimensions, Args)
err := plot.SetFormat(0)
if err == nil {
t.Error("SetLabels raises error when non-supported format is passed as an argument.")
}

// Test supported formats
plot, _ = NewPlot(dimensions, Args)
err = plot.SetFormat(Pdf)
if err != nil {
t.Error("SetLabels failed to set supported format (Pdf).")
}
err = plot.SetFormat(Png)
if err != nil {
t.Error("SetLabels failed to set supported format (Png).")
}
}

func TestSetFormat2Dims(t *testing.T) {
dimensions := 2

// Test unsupported formats
plot, _ := NewPlot(dimensions, Args)
err := plot.SetFormat(0)
if err == nil {
t.Error("SetLabels raises error when non-supported format is passed as an argument.")
}

// Test supported formats
plot, _ = NewPlot(dimensions, Args)
err = plot.SetFormat(Pdf)
if err != nil {
t.Error("SetLabels failed to set supported format (Pdf).")
}
err = plot.SetFormat(Png)
if err != nil {
t.Error("SetLabels failed to set supported format (Png).")
}
}

func TestSetFormat1Dims(t *testing.T) {
dimensions := 1

// Test unsupported formats
plot, _ := NewPlot(dimensions, Args)
err := plot.SetFormat(0)
if err == nil {
t.Error("SetLabels raises error when non-supported format is passed as an argument.")
}

// Test supported formats
plot, _ = NewPlot(dimensions, Args)
err = plot.SetFormat(Pdf)
if err != nil {
t.Error("SetLabels failed to set supported format (Pdf).")
}
err = plot.SetFormat(Png)
if err != nil {
t.Error("SetLabels failed to set supported format (Png).")
}
}

func TestLinesStyleBrewerQualitative1(t *testing.T) {
dimensions := 1

// Test unsupported formats
plot1, _ := NewPlot(dimensions, Args)
plot2, _ := NewPlot(dimensions, Args)
t1 := `
set multiplot layout 2,2 ;
plot sin(x) ls 1 ; plot sin(x/2) ls 2 ;
plot sin(x/4) ls 3 ; plot cos(x/2) ls 4
`
t2 := `
set multiplot layout 2,2 ;
plot sin(x) ls 5 ; plot sin(x/2) ls 6 ;
plot sin(x/4) ls 7 ; plot cos(x/2) ls 8
`
plot1.Cmd(t1)
plot2.Cmd(t2)

}
4 changes: 3 additions & 1 deletion core.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (plot *Plot) Cmd(format string, a ...interface{}) error {
if plot.debug {
//buf := new(bytes.Buffer)
//io.Copy(buf, plot.proc.handle.Stdout)
fmt.Printf("cmd> %v", cmd)
fmt.Printf("fmt> %v\n", format)
fmt.Printf("a> %v\n", a)
fmt.Printf("cmd> %v\n", cmd)
fmt.Printf("res> %v\n", n)
}
return err
Expand Down
33 changes: 32 additions & 1 deletion core_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package glot

import "testing"
import (
"math"
"testing"
)

func TestMin(t *testing.T) {
var v int
Expand All @@ -9,3 +12,31 @@ func TestMin(t *testing.T) {
t.Error("Expected 1, got ", v)
}
}

func TestCmd(t *testing.T) {
base2n := func(n float64) []float64 {
res := []float64{}
for i := 0.; i < n; i++ {
res = append(res, math.Pow(2, i))
}
return res
}
var testArgs = PlotArgs{
Debug: true,
Format: Pdf,
Persist: true,
Style: Points,
Command: "plot ",
}
var pg = &PointGroup{
name: "TestCmd",
dimensions: 1,
style: Points,
castedData: base2n(9),
}
p, err := NewPlot(1, testArgs)
if err != nil {
t.Errorf("NewPlot failed creation with: %v", err)
}
p.plotX(pg)
}
4 changes: 2 additions & 2 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Func3d func(x float64, y float64) float64
// pointsX :=> The x Value of the points to be plotted. y = func(x) is plotted on the curve.
// style :=> Style of the curve
// NOTE: Currently only float64 type is supported for this function
func (plot *Plot) AddFunc2d(name string, style string, x []float64, fct Func2d) error {
func (plot *Plot) AddFunc2d(name string, style PointStyle, x []float64, fct Func2d) error {
y := make([]float64, len(x))
for index := range x {
y[index] = fct(x[index])
Expand Down Expand Up @@ -67,7 +67,7 @@ func (plot *Plot) AddFunc2d(name string, style string, x []float64, fct Func2d)
// style :=> Style of the curve
// pointsX :=> The x Value of the points to be plotted. y = func(x) is plotted on the curve.
// NOTE: Currently only float64 type is supported for this function
func (plot *Plot) AddFunc3d(name string, style string, x []float64, y []float64, fct Func3d) error {
func (plot *Plot) AddFunc3d(name string, style PointStyle, x []float64, y []float64, fct Func3d) error {
if len(x) != len(y) {
return &gnuplotError{fmt.Sprintf("The length of the x-axis array and y-axis array are not same.")}
}
Expand Down
14 changes: 8 additions & 6 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"testing"
)

var funcTestArgs = PlotArgs{
Debug: true,
Persist: true,
}

func TestAddFunc3d(t *testing.T) {
dimensions := 3
persist := false
debug := false
plot, _ := NewPlot(dimensions, persist, debug)
plot, _ := NewPlot(dimensions, funcTestArgs)
fct := func(x, y float64) float64 { return x - y }
groupName := "Stright Line"
style := "lines"
groupName := "Straight Line"
pointsY := []float64{1, 2, 3}
pointsX := []float64{1, 2, 3, 4, 5}
err := plot.AddFunc3d(groupName, style, pointsX, pointsY, fct)
err := plot.AddFunc3d(groupName, Lines, pointsX, pointsY, fct)
if err == nil {
t.Error("TestAddFunc3d raises error when the size of X and Y arrays are not equal.")
}
Expand Down
Loading

0 comments on commit 7d63f3b

Please sign in to comment.