arafatkatze / glot

Glot is a plotting library for Golang built on top of gnuplot.
MIT License
398 stars 19 forks source link

Looser requirements for setting style of pointgroup #7

Open szaydel opened 6 years ago

szaydel commented 6 years ago

I thought along the lines of making things easier with sensible defaults, perhaps passing an empty string for style could result in a a sensible default being chosen for you. In other words, if I do something like this and my points object is a []uint64{} slice, I should be able to see a histogram automatically.

plot.AddPointGroup(pointGroupName, "", points)

I had something along these lines in mind...

diff --git i/pointgroup.go w/pointgroup.go
index dce5d91..7589e20 100644
--- i/pointgroup.go
+++ w/pointgroup.go
@@ -2,6 +2,7 @@ package glot

 import (
        "fmt"
+       "strings"
 )

 // A PointGroup refers to a set of points that need to plotted.
@@ -41,13 +42,33 @@ func (plot *Plot) AddPointGroup(name string, style string, data interface{}) (er
                "boxes", "lp"}
        curve.style = defaultStyle
        discovered := 0
-       for _, s := range allowed {
-               if s == style {
-                       curve.style = style
-                       err = nil
+
+       // If the style value is an empty string and there's only a single
+       // dimension, assume histogram by default.
+       if strings.Compare(style, "") == 0 {
+               if plot.dimensions == 1 {
+                       curve.style = "histogram"
+                       discovered = 1
+               } else if plot.dimensions == 2 || plot.dimensions == 3 {
+                       curve.style = "points"
                        discovered = 1
+               } else if plot.dimensions == 0 {
+                       return &gnuplotError{
+                               fmt.Sprintf("Wrong number of dimensions in this plot."),
+                       }
+               }
+       } else {
+               for _, s := range allowed {
+                       if s == style {
+                               curve.style = style
+                               // Default value at initialization is nil, so
+                               // this is probably unnecessary code.
+                               err = nil
+                               discovered = 1
+                       }
                }
        }
+
        switch data.(type) {
        case [][]float64:
                if plot.dimensions != len(data.([][]float64)) {

Regards, Sam.

shubham7saxena commented 6 years ago

@szaydel may I know why do you want to assume histogram by default in case of an empty style?

szaydel commented 6 years ago

@shubham7saxena, in general, if you only have a single dimension, i.e. X only, there are few better options, and what you will notice is that a histogram is the most commonly used tool, when you only have a single series.

arafatkatze commented 6 years ago

@szaydel Thanks, That is a good idea.
I think that having a default would be great(either histogram/line/points).
Will Add it.

szaydel commented 6 years ago

@Arafatk, sounds good!