-
Notifications
You must be signed in to change notification settings - Fork 3
/
sparklines.go
84 lines (71 loc) · 1.58 KB
/
sparklines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package elements
import (
"encoding/json"
"fmt"
"sort"
"github.com/satori/go.uuid"
)
const (
//Sparkline Graph types
SparklineLine = iota
SparklineBar
SparklineTristate
SparklineDiscrete
SparklineBullet
SparklinePie
SparklineBox
)
//Sparkline produce the sparkline element
type Sparkline struct {
Base
Type string
Values []int
Options map[string]string
}
//SetOption set a option value for a key
func (sl *Sparkline) SetOption(key string, value string) {
if sl.Data == nil {
sl.Options = make(map[string]string)
}
sl.Options[key] = value
}
//RemoveOption delete a value in Data map
func (sl *Sparkline) RemoveOption(key string) {
delete(sl.Options, key)
}
//String return string tag for Sparkline
func (spark Sparkline) String() string {
if spark.ID == "" {
u1 := uuid.NewV4().String()[0:8]
spark.ID = u1
}
ret := fmt.Sprintf("<span id='%s'>...</span>", spark.ID)
svalues := "[]"
if spark.Values != nil {
bs, err := json.Marshal(spark.Values)
if err == nil {
svalues = string(bs)
}
}
soptions := "{}"
if spark.Options != nil {
var keys []string
for key, _ := range spark.Options {
keys = append(keys, key)
}
sort.Strings(keys)
soptions = "{"
for _, key := range keys {
soptions += key + ":'" + spark.Options[key] + "',"
}
soptions += "}"
}
ret += "<script>"
ret += "loadScript('"
ret += "https://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.min.js"
ret += "', function() {"
ret += fmt.Sprintf("$('#%s').sparkline(%s,%s);", spark.ID, svalues, soptions)
ret += "});"
ret += "</script>"
return ret
}