diff --git a/README.md b/README.md index 01eb11e..dbcd456 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,43 @@ Running this example would render the following graph: 0.00 ┼╯ ╰ ``` +### Colored graphs + +``` go +package main + +import ( + "fmt" + "github.com/guptarohit/asciigraph" +) + +func main() { + data := make([][]float64, 4) + + for i := 0; i < 4; i++ { + for x := -20; x <= 20; x++ { + v := math.NaN() + if r := 20 - i; x >= -r && x <= r { + v = math.Sqrt(math.Pow(float64(r), 2)-math.Pow(float64(x), 2)) / 2 + } + data[i] = append(data[i], v) + } + } + graph := asciigraph.PlotMany(data, asciigraph.Precision(0), asciigraph.SeriesColors( + asciigraph.Red, + asciigraph.Yellow, + asciigraph.Green, + asciigraph.Blue, + )) + + fmt.Println(graph) +} +``` + +Running this example would render the following graph: + +![colored_graph_image][] + ## Command line interface This package also brings a small utility for command line usage. @@ -81,20 +118,28 @@ This package also brings a small utility for command line usage. Usage of asciigraph: asciigraph [options] Options: + -ac axis color + y-axis color of the plot -b buffer data points buffer when realtime graph enabled, default equal to `width` -c caption caption for the graph + -cc caption color + caption color of the plot -f fps set fps to control how frequently graph to be rendered when realtime graph enabled (default 24) -h height height in text rows, 0 for auto-scaling + -lc label color + y-axis label color of the plot -o offset offset in columns, for the label (default 3) -p precision precision of data point labels along the y-axis (default 2) -r realtime enables realtime graph for data stream + -sc series color + series color of the plot -w width width in columns, 0 for auto-scaling asciigraph expects data points from stdin. Invalid values are logged to stderr. @@ -174,6 +219,7 @@ Feel free to make a pull request! :octocat: [Mentioned in Awesome Go]: https://awesome.re/mentioned-badge-flat.svg [6]: https://github.com/avelino/awesome-go#advanced-console-uis [image]: https://user-images.githubusercontent.com/7895001/41509956-b1b2b3d0-7279-11e8-9d19-d7dea17d5e44.png +[colored_graph_image]: https://user-images.githubusercontent.com/7895001/166443444-40ad8113-2c0f-46d7-9c75-1cf08435ce15.png [releases]: https://github.com/guptarohit/asciigraph/releases [asciichart]: https://github.com/kroitor/asciichart [asciinema]: https://asciinema.org/a/382383.svg diff --git a/asciigraph.go b/asciigraph.go index 61be621..394b66d 100644 --- a/asciigraph.go +++ b/asciigraph.go @@ -147,7 +147,7 @@ func PlotMany(data [][]float64, options ...Option) string { if !math.IsNaN(series[0]) { y0 = int(round(series[0]*ratio) - min2) plot[rows-y0][config.Offset-1].Text = "┼" // first value - plot[rows-y0][config.Offset-1].Color = color + plot[rows-y0][config.Offset-1].Color = config.AxisColor } for x := 0; x < len(series)-1; x++ { // plot the line diff --git a/asciigraph_test.go b/asciigraph_test.go index 9c27545..261646f 100644 --- a/asciigraph_test.go +++ b/asciigraph_test.go @@ -308,7 +308,7 @@ func TestPlotMany(t *testing.T) { { [][]float64{{0, 0}, {math.NaN(), 0}}, []Option{SeriesColors(Red)}, - " 0.00 \x1b[91m┼\x1b[0m╶"}, + " 0.00 ┼╶"}, { [][]float64{{0, 0}, {math.NaN(), 0}}, []Option{SeriesColors(Default, Red)}, @@ -319,7 +319,7 @@ func TestPlotMany(t *testing.T) { ` 2.00 ┤\x1b[91m╭╭\x1b[0m 1.00 ┤\x1b[91m││\x1b[0m - 0.00 \x1b[91m┼╯╯\x1b[0m`}, + 0.00 ┼\x1b[91m╯╯\x1b[0m`}, } for i := range cases { diff --git a/cmd/asciigraph/main.go b/cmd/asciigraph/main.go index 8f76130..22973f4 100644 --- a/cmd/asciigraph/main.go +++ b/cmd/asciigraph/main.go @@ -22,7 +22,10 @@ var ( enableRealTime bool realTimeDataBuffer int fps float64 = 24 - color asciigraph.AnsiColor + seriesColor asciigraph.AnsiColor + captionColor asciigraph.AnsiColor + axisColor asciigraph.AnsiColor + labelColor asciigraph.AnsiColor ) func main() { @@ -41,14 +44,42 @@ func main() { flag.BoolVar(&enableRealTime, "r", enableRealTime, "enables `realtime` graph for data stream") flag.IntVar(&realTimeDataBuffer, "b", realTimeDataBuffer, "data points `buffer` when realtime graph enabled, default equal to `width`") flag.Float64Var(&fps, "f", fps, "set `fps` to control how frequently graph to be rendered when realtime graph enabled") - flag.Func("l", "`color` of the data line", func(str string) error { + flag.Func("sc", "`series color` of the plot", func(str string) error { if c, ok := asciigraph.ColorNames[str]; !ok { - return errors.New("unrecognized color") + return errors.New("unrecognized seriesColor") } else { - color = c + seriesColor = c return nil } }) + + flag.Func("cc", "`caption color` of the plot", func(str string) error { + if c, ok := asciigraph.ColorNames[str]; !ok { + return errors.New("unrecognized seriesColor") + } else { + captionColor = c + return nil + } + }) + + flag.Func("ac", "y-`axis color` of the plot", func(str string) error { + if c, ok := asciigraph.ColorNames[str]; !ok { + return errors.New("unrecognized seriesColor") + } else { + axisColor = c + return nil + } + }) + + flag.Func("lc", "y-axis `label color` of the plot", func(str string) error { + if c, ok := asciigraph.ColorNames[str]; !ok { + return errors.New("unrecognized seriesColor") + } else { + labelColor = c + return nil + } + }) + flag.Parse() data := make([]float64, 0, 64) @@ -84,7 +115,10 @@ func main() { asciigraph.Offset(int(offset)), asciigraph.Precision(precision), asciigraph.Caption(caption), - asciigraph.SeriesColors(color), + asciigraph.SeriesColors(seriesColor), + asciigraph.CaptionColor(captionColor), + asciigraph.AxisColor(axisColor), + asciigraph.LabelColor(labelColor), ) asciigraph.Clear() fmt.Println(plot) @@ -107,7 +141,10 @@ func main() { asciigraph.Offset(int(offset)), asciigraph.Precision(precision), asciigraph.Caption(caption), - asciigraph.SeriesColors(color), + asciigraph.SeriesColors(seriesColor), + asciigraph.CaptionColor(captionColor), + asciigraph.AxisColor(axisColor), + asciigraph.LabelColor(labelColor), ) fmt.Println(plot)