summaryrefslogtreecommitdiffstats
path: root/termui/linegraph.go
diff options
context:
space:
mode:
Diffstat (limited to 'termui/linegraph.go')
-rw-r--r--termui/linegraph.go21
1 files changed, 12 insertions, 9 deletions
diff --git a/termui/linegraph.go b/termui/linegraph.go
index 1aebeb3..4b01691 100644
--- a/termui/linegraph.go
+++ b/termui/linegraph.go
@@ -7,7 +7,7 @@ import (
drawille "github.com/cjbassi/drawille-go"
)
-// LineGraph implements a graph of data points.
+// LineGraph implements a line graph of data points.
type LineGraph struct {
*Block
Data map[string][]float64
@@ -27,17 +27,19 @@ func NewLineGraph() *LineGraph {
}
}
-// renderPoints plots and interpolates data points.
+// Buffer implements Bufferer interface.
func (lc *LineGraph) Buffer() *Buffer {
buf := lc.Block.Buffer()
+ // we render each data point on to the canvas then copy over the braille to the buffer at the end
+ // fyi braille characters have 2x4 dots for each character
c := drawille.NewCanvas()
- // used to keep track of colors but not write them to the buffer until the end
+ // used to keep track of the braille colors until the end when we render the braille to the buffer
colors := make([][]Color, lc.X+2)
for i := range colors {
colors[i] = make([]Color, lc.Y+2)
}
- // Sort the series so that overlapping data will overlap the same way each time
+ // sort the series so that overlapping data will overlap the same way each time
seriesList := make([]string, len(lc.Data))
i := 0
for seriesName := range lc.Data {
@@ -46,7 +48,7 @@ func (lc *LineGraph) Buffer() *Buffer {
}
sort.Strings(seriesList)
- // draw lines in reverse order so the first one is on top
+ // draw lines in reverse order so that the first color defined in the colorscheme is on top
for i := len(seriesList) - 1; i >= 0; i-- {
seriesName := seriesList[i]
seriesData := lc.Data[seriesName]
@@ -61,13 +63,12 @@ func (lc *LineGraph) Buffer() *Buffer {
for i := len(seriesData) - 1; i >= 0; i-- {
x := ((lc.X + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * 5)
y := ((lc.Y + 1) * 4) - 1 - int((float64((lc.Y)*4)-1)*(seriesData[i]/100))
- // stop rendering at the left-most wall
- if x < 0 {
+ if x < 0 { // stop rendering at the left-most wall
break
}
if lastY == -1 { // if this is the first point
c.Set(x, y)
- colors[x/2][y/4] = seriesLineColor // divide by 2 and 4 due to 2x4 dots in braille characters
+ colors[x/2][y/4] = seriesLineColor
} else {
c.DrawLine(lastX, lastY, x, y)
for _, p := range drawille.Line(lastX, lastY, x, y) {
@@ -91,14 +92,16 @@ func (lc *LineGraph) Buffer() *Buffer {
}
}
+ // renders key ontop
for j, seriesName := range seriesList {
+ // sorts lines again
seriesData := lc.Data[seriesName]
seriesLineColor, ok := lc.LineColor[seriesName]
if !ok {
seriesLineColor = lc.DefaultLineColor
}
- // Render key ontop, but let braille be drawn between words
+ // render key ontop, but let braille be drawn over space characters
str := fmt.Sprintf("%s %3.0f%%", seriesName, seriesData[len(seriesData)-1])
for k, char := range str {
if char != ' ' {