Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #40 : Changes call .Plot on main.go to .PlotMany #43

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,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 @@ -142,6 +144,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
Loading