Skip to content

Commit

Permalink
Merge pull request #43 from KarlHeitmann/issue_40-cli_calls_plot_many
Browse files Browse the repository at this point in the history
Added support for plotting multiple series from CLI

Issue #40
  • Loading branch information
guptarohit authored Mar 30, 2024
2 parents 9d11390 + e24643c commit 28aa5ed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Options:
caption for the graph
-cc caption color
caption color of the plot
-d delimiter
data delimiter for splitting data points in the input stream (default ",")
-f fps
set fps to control how frequently graph to be rendered when realtime graph enabled (default 24)
-h height
Expand All @@ -195,6 +197,8 @@ Options:
enables realtime graph for data stream
-sc series color
series color of the plot
-sn number of series
number of series (columns) in the input data (default 1)
-ub upper bound
upper bound set the maximum value for the vertical axis (ignored if series contains larger values) (default -Inf)
-w width
Expand Down
52 changes: 36 additions & 16 deletions cmd/asciigraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"os"
"strconv"
"strings"
"time"

"github.com/guptarohit/asciigraph"
Expand All @@ -27,8 +28,10 @@ var (
captionColor asciigraph.AnsiColor
axisColor asciigraph.AnsiColor
labelColor asciigraph.AnsiColor
lowerBound = math.Inf(1)
upperBound = math.Inf(-1)
lowerBound = math.Inf(1)
upperBound = math.Inf(-1)
delimiter = ","
seriesNum uint = 1
)

func main() {
Expand Down Expand Up @@ -84,37 +87,54 @@ func main() {
})
flag.Float64Var(&lowerBound, "lb", lowerBound, "`lower bound` set the minimum value for the vertical axis (ignored if series contains lower values)")
flag.Float64Var(&upperBound, "ub", upperBound, "`upper bound` set the maximum value for the vertical axis (ignored if series contains larger values)")
flag.StringVar(&delimiter, "d", delimiter, "data `delimiter` for splitting data points in the input stream")
flag.UintVar(&seriesNum, "sn", seriesNum, "`number of series` (columns) in the input data")

flag.Parse()

data := make([]float64, 0, 64)
series := make([][]float64, seriesNum)

if realTimeDataBuffer == 0 {
if enableRealTime && realTimeDataBuffer == 0 {
realTimeDataBuffer = int(width)
}

s := bufio.NewScanner(os.Stdin)
s.Split(bufio.ScanWords)
s.Split(bufio.ScanLines)

nextFlushTime := time.Now()

flushInterval := time.Duration(float64(time.Second) / fps)

for s.Scan() {
word := s.Text()
p, err := strconv.ParseFloat(word, 64)
if err != nil {
log.Printf("ignore %q: cannot parse value", word)
continue
line := s.Text()
points := strings.Split(line, delimiter)

if uint(len(points)) < seriesNum {
log.Fatal("number of series in the input data stream is less than the specified series number")
} else if uint(len(points)) > seriesNum {
points = points[:seriesNum]
}

for i, point := range points {
p, err := strconv.ParseFloat(strings.TrimSpace(point), 64)
if err != nil {
log.Printf("ignore %q: cannot parse value", point)
p = math.NaN()
}
series[i] = append(series[i], p)

}
data = append(data, p)
if enableRealTime {
if realTimeDataBuffer > 0 && len(data) > realTimeDataBuffer {
data = data[len(data)-realTimeDataBuffer:]
if realTimeDataBuffer > 0 && len(series[0]) > realTimeDataBuffer {
for i := range series {
seriesLength := len(series[i])
series[i] = series[i][seriesLength-realTimeDataBuffer:]
}
}

if currentTime := time.Now(); currentTime.After(nextFlushTime) || currentTime.Equal(nextFlushTime) {
plot := asciigraph.Plot(data,
seriesCopy := append([][]float64(nil), series...)
plot := asciigraph.PlotMany(seriesCopy,
asciigraph.Height(int(height)),
asciigraph.Width(int(width)),
asciigraph.Offset(int(offset)),
Expand All @@ -138,11 +158,11 @@ func main() {
log.Fatal(err)
}

if len(data) == 0 {
if len(series) == 0 {
log.Fatal("no data")
}

plot := asciigraph.Plot(data,
plot := asciigraph.PlotMany(series,
asciigraph.Height(int(height)),
asciigraph.Width(int(width)),
asciigraph.Offset(int(offset)),
Expand Down

0 comments on commit 28aa5ed

Please sign in to comment.