-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for colored legend (#47)
* Add support of legends for colored graphs --------- Co-authored-by: Rohit Gupta <rohitgtech+git@gmail.com>
- Loading branch information
1 parent
e398e0f
commit 27408ca
Showing
6 changed files
with
187 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/guptarohit/asciigraph" | ||
) | ||
|
||
func main() { | ||
data := make([][]float64, 6) | ||
|
||
// concentric semi-circles | ||
for i := 0; i < 6; i++ { | ||
for x := -40; x <= 40; x++ { | ||
v := math.NaN() | ||
if r := 40 - 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.Orange, | ||
asciigraph.Yellow, | ||
asciigraph.Green, | ||
asciigraph.Blue, | ||
asciigraph.Purple, | ||
), asciigraph.SeriesLegends( | ||
"Red", | ||
"Orange", | ||
"Yellow", | ||
"Green", | ||
"Blue", | ||
"Purple", | ||
), | ||
asciigraph.Caption("Rainbow with color legends")) | ||
|
||
fmt.Println(graph) | ||
// Output: | ||
// 20 ┤ | ||
// 20 ┤ ╭───────╭╮───────╮ | ||
// 19 ┤ ╭──╭───╭───────╭╮───────╮───╮──╮ | ||
// 18 ┤ ╭─╭──╭─╭───╭───────╭╮───────╮───╮─╮──╮─╮ | ||
// 17 ┤ ╭─╭─╭─╭─╭──╭──────────╯╰──────────╮──╮─╮─╮─╮─╮ | ||
// 16 ┤ ╭─╭─╭╭─╭─╭────╯ ╰────╮─╮─╮╮─╮─╮ | ||
// 15 ┤ ╭╭─╭─╭╭─╭──╯ ╰──╮─╮╮─╮─╮╮ | ||
// 14 ┤ ╭╭─╭╭─╭╭──╯ ╰──╮╮─╮╮─╮╮ | ||
// 13 ┤ ╭─╭╭╭─╭╭─╯ ╰─╮╮─╮╮╮─╮ | ||
// 12 ┤ ╭╭╭─╭╭╭─╯ ╰─╮╮╮─╮╮╮ | ||
// 11 ┤ ╭─╭╭╭╭╭─╯ ╰─╮╮╮╮╮─╮ | ||
// 10 ┤ ╭╭─╭╭╭╭╯ ╰╮╮╮╮─╮╮ | ||
// 9 ┤ ╭╭╯╭╭╭╭╯ ╰╮╮╮╮╰╮╮ | ||
// 8 ┤ ╭╭╯╭╭╭╭╯ ╰╮╮╮╮╰╮╮ | ||
// 7 ┤ ││╭╭╭╭╯ ╰╮╮╮╮││ | ||
// 6 ┤ ╭╭╭╭╭╭╯ ╰╮╮╮╮╮╮ | ||
// 5 ┤ ││││││ ││││││ | ||
// 4 ┤╭╭╭╭╭╭╯ ╰╮╮╮╮╮╮ | ||
// 3 ┤││││││ ││││││ | ||
// 2 ┤││││││ ││││││ | ||
// 1 ┤││││││ ││││││ | ||
// 0 ┼╶╶╶╶╶╯ ╰╴╴╴╴╴ | ||
// Rainbow with color legends | ||
// | ||
// ■ Red ■ Orange ■ Yellow ■ Green ■ Blue ■ Purple | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package asciigraph | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"unicode/utf8" | ||
) | ||
|
||
// Create legend item as a colored box and text | ||
func createLegendItem(text string, color AnsiColor) (string, int) { | ||
return fmt.Sprintf( | ||
"%s■%s %s", | ||
color.String(), | ||
Default.String(), | ||
text, | ||
), | ||
// Can't use len() because of AnsiColor, add 2 for box and space | ||
utf8.RuneCountInString(text) + 2 | ||
} | ||
|
||
// Add legend for each series added to the graph | ||
func addLegends(lines *bytes.Buffer, config *config, lenMax int, leftPad int) { | ||
lines.WriteString("\n\n") | ||
lines.WriteString(strings.Repeat(" ", leftPad)) | ||
|
||
var legendsText string | ||
var legendsTextLen int | ||
rightPad := 3 | ||
for i, text := range config.SeriesLegends { | ||
item, itemLen := createLegendItem(text, config.SeriesColors[i]) | ||
legendsText += item | ||
legendsTextLen += itemLen | ||
|
||
if i < len(config.SeriesLegends)-1 { | ||
legendsText += strings.Repeat(" ", rightPad) | ||
legendsTextLen += rightPad | ||
} | ||
} | ||
|
||
if legendsTextLen < lenMax { | ||
lines.WriteString(strings.Repeat(" ", (lenMax-legendsTextLen)/2)) | ||
} | ||
lines.WriteString(legendsText) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters