aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gizak/termui/sparkline.go
blob: 312ad9563da9c4cbafb264ff55e8c7009b421fda (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2016 Zack Guo <gizak@icloud.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.

package termui

// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers.
/*
  data := []int{4, 2, 1, 6, 3, 9, 1, 4, 2, 15, 14, 9, 8, 6, 10, 13, 15, 12, 10, 5, 3, 6, 1}
  spl := termui.NewSparkline()
  spl.Data = data
  spl.Title = "Sparkline 0"
  spl.LineColor = termui.ColorGreen
*/
type Sparkline struct {
    Data          []int
    Height        int
    Title         string
    TitleColor    Attribute
    LineColor     Attribute
    displayHeight int
    scale         float32
    max           int
}

// Sparklines is a renderable widget which groups together the given sparklines.
/*
  spls := termui.NewSparklines(spl0,spl1,spl2) //...
  spls.Height = 2
  spls.Width = 20
*/
type Sparklines struct {
    Block
    Lines        []Sparkline
    displayLines int
    displayWidth int
}

var sparks = []rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}

// Add appends a given Sparkline to s *Sparklines.
func (s *Sparklines) Add(sl Sparkline) {
    s.Lines = append(s.Lines, sl)
}

// NewSparkline returns a unrenderable single sparkline that intended to be added into Sparklines.
func NewSparkline() Sparkline {
    return Sparkline{
        Height:     1,
        TitleColor: ThemeAttr("sparkline.title.fg"),
        LineColor:  ThemeAttr("sparkline.line.fg")}
}

// NewSparklines return a new *Spaklines with given Sparkline(s), you can always add a new Sparkline later.
func NewSparklines(ss ...Sparkline) *Sparklines {
    s := &Sparklines{Block: *NewBlock(), Lines: ss}
    return s
}

func (sl *Sparklines) update() {
    for i, v := range sl.Lines {
        if v.Title == "" {
            sl.Lines[i].displayHeight = v.Height
        } else {
            sl.Lines[i].displayHeight = v.Height + 1
        }
    }
    sl.displayWidth = sl.innerArea.Dx()

    // get how many lines gotta display
    h := 0
    sl.displayLines = 0
    for _, v := range sl.Lines {
        if h+v.displayHeight <= sl.innerArea.Dy() {
            sl.displayLines++
        } else {
            break
        }
        h += v.displayHeight
    }

    for i := 0; i < sl.displayLines; i++ {
        data := sl.Lines[i].Data

        max := 0
        for _, v := range data {
            if max < v {
                max = v
            }
        }
        sl.Lines[i].max = max
        if max != 0 {
            sl.Lines[i].scale = float32(8*sl.Lines[i].Height) / float32(max)
        } else { // when all negative
            sl.Lines[i].scale = 0
        }
    }
}

// Buffer implements Bufferer interface.
func (sl *Sparklines) Buffer() Buffer {
    buf := sl.Block.Buffer()
    sl.update()

    oftY := 0
    for i := 0; i < sl.displayLines; i++ {
        l := sl.Lines[i]
        data := l.Data

        if len(data) > sl.innerArea.Dx() {
            data = data[len(data)-sl.innerArea.Dx():]
        }

        if l.Title != "" {
            rs := trimStr2Runes(l.Title, sl.innerArea.Dx())
            oftX := 0
            for _, v := range rs {
                w := charWidth(v)
                c := Cell{
                    Ch: v,
                    Fg: l.TitleColor,
                    Bg: sl.Bg,
                }
                x := sl.innerArea.Min.X + oftX
                y := sl.innerArea.Min.Y + oftY
                buf.Set(x, y, c)
                oftX += w
            }
        }

        for j, v := range data {
            // display height of the data point, zero when data is negative
            h := int(float32(v)*l.scale + 0.5)
            if v < 0 {
                h = 0
            }

            barCnt := h / 8
            barMod := h % 8
            for jj := 0; jj < barCnt; jj++ {
                c := Cell{
                    Ch: ' ', // => sparks[7]
                    Bg: l.LineColor,
                }
                x := sl.innerArea.Min.X + j
                y := sl.innerArea.Min.Y + oftY + l.Height - jj

                //p.Bg = sl.BgColor
                buf.Set(x, y, c)
            }
            if barMod != 0 {
                c := Cell{
                    Ch: sparks[barMod-1],
                    Fg: l.LineColor,
                    Bg: sl.Bg,
                }
                x := sl.innerArea.Min.X + j
                y := sl.innerArea.Min.Y + oftY + l.Height - barCnt
                buf.Set(x, y, c)
            }
        }

        oftY += l.displayHeight
    }

    return buf
}