bar chart initial commit

This commit is contained in:
Navid Yaghoobi 2021-12-20 21:02:43 +11:00
parent e08fdd7e12
commit 01198cf725
2 changed files with 19 additions and 28 deletions

View File

@ -75,18 +75,18 @@ func (c *BarChart) Draw(screen tcell.Screen) {
} }
// draw graph y-axis // draw graph y-axis
for i := borderPadding; i+y < height; i++ { for i := borderPadding; i+y < height; i++ {
screen.SetContent(x+maxValLenght, y+i, tview.Borders.Vertical, nil, style) tview.PrintJoinedSemigraphics(screen, x+maxValLenght, y+i, tview.Borders.Vertical, style)
} }
// draw graph x-axix // draw graph x-axix
for i := maxValLenght; i+x < width-borderPadding; i++ { for i := maxValLenght; i+x < width-borderPadding; i++ {
screen.SetContent(x+i, xAxisStartY, tview.Borders.Horizontal, nil, style) tview.PrintJoinedSemigraphics(screen, x+i, xAxisStartY, tview.Borders.Horizontal, style)
} }
screen.SetContent(x+maxValLenght, xAxisStartY, tview.BoxDrawingsLightVerticalAndRight, nil, style) tview.PrintJoinedSemigraphics(screen, x+maxValLenght, xAxisStartY, tview.BoxDrawingsLightVerticalAndRight, style)
screen.SetContent(x+maxValLenght-1, xAxisStartY, '0', nil, style) tview.PrintJoinedSemigraphics(screen, x+maxValLenght-1, xAxisStartY, '0', style)
mxValRune := []rune(maxValueSr) mxValRune := []rune(maxValueSr)
for i := 0; i < len(mxValRune); i++ { for i := 0; i < len(mxValRune); i++ {
tview.Print(screen, string(mxValRune[i]), x+borderPadding+i, maxValY, 1, 0, tcell.ColorWhite) tview.PrintJoinedSemigraphics(screen, x+borderPadding+i, maxValY, mxValRune[i], style)
} }
// draw bars // draw bars
@ -100,14 +100,14 @@ func (c *BarChart) Draw(screen tcell.Screen) {
// set labels // set labels
r := []rune(item.label) r := []rune(item.label)
for j := 0; j < len(r); j++ { for j := 0; j < len(r); j++ {
screen.SetContent(startX+j, labelY, r[j], nil, style) tview.PrintJoinedSemigraphics(screen, startX+j, labelY, r[j], style)
} }
// bar style // bar style
bStyle := style.Foreground(item.color) bStyle := style.Foreground(item.color)
barHeight := c.getHeight(valueMaxHeight, item.value) barHeight := c.getHeight(valueMaxHeight, item.value)
for k := 0; k < barHeight; k++ { for k := 0; k < barHeight; k++ {
for l := 0; l < c.barWidth; l++ { for l := 0; l < c.barWidth; l++ {
screen.SetContent(startX+l, barStartY-k, '\u2588', nil, bStyle) tview.PrintJoinedSemigraphics(screen, startX+l, barStartY-k, '\u2588', bStyle)
} }
} }
@ -115,7 +115,7 @@ func (c *BarChart) Draw(screen tcell.Screen) {
vSt := fmt.Sprintf("%d", item.value) vSt := fmt.Sprintf("%d", item.value)
vRune := []rune(vSt) vRune := []rune(vSt)
for i := 0; i < len(vRune); i++ { for i := 0; i < len(vRune); i++ {
screen.SetContent(startX+i, barStartY-barHeight, vRune[i], nil, bStyle) tview.PrintJoinedSemigraphics(screen, startX+i, barStartY-barHeight, vRune[i], bStyle)
} }
// calculate next startX for next bar // calculate next startX for next bar
@ -135,6 +135,10 @@ func (c *BarChart) SetBorder(status bool) {
c.Box.SetBorder(status) c.Box.SetBorder(status)
} }
func (c *BarChart) GetRect() (int, int, int, int) {
return c.Box.GetRect()
}
// SetRect sets rect for this primitive. // SetRect sets rect for this primitive.
func (c *BarChart) SetRect(x, y, width, height int) { func (c *BarChart) SetRect(x, y, width, height int) {
c.Box.SetRect(x, y, width, height) c.Box.SetRect(x, y, width, height)

View File

@ -12,31 +12,18 @@ import (
func main() { func main() {
app := tview.NewApplication() app := tview.NewApplication()
flex := tview.NewFlex().SetDirection(tview.FlexRow)
barGraph := tgraph.NewBarChart() barGraph := tgraph.NewBarChart()
barGraph.SetBorder(true) barGraph.SetBorder(true)
barGraph.SetTitle("System Resource Usage") barGraph.SetTitle("System Resource Usage")
// display system metric usage // display system metric usage
barGraph.AddBar("cpu", 80, tcell.ColorBlue) barGraph.AddBar("cpu", 8, tcell.ColorBlue)
barGraph.AddBar("mem", 20, tcell.ColorRed) barGraph.AddBar("mem", 2, tcell.ColorRed)
barGraph.AddBar("swap", 40, tcell.ColorGreen) barGraph.AddBar("swap", 4, tcell.ColorGreen)
barGraph.AddBar("disk", 40, tcell.ColorOrange) barGraph.AddBar("disk", 4, tcell.ColorOrange)
barGraph.SetMaxValue(100) barGraph.SetMaxValue(100)
bar2 := tgraph.NewBarChart() barGraph.SetRect(10, 10, 50, 20)
bar2.SetBorder(true)
bar2.SetTitle("System Resource Usage")
// display system metric usage
bar2.AddBar("cpu", 80, tcell.ColorBlue)
bar2.AddBar("mem", 20, tcell.ColorRed)
bar2.AddBar("swap", 40, tcell.ColorGreen)
bar2.AddBar("disk", 40, tcell.ColorOrange)
bar2.SetMaxValue(80)
flex.AddItem(barGraph, 20, 1, false)
flex.SetRect(0, 0, 50, 50)
update := func() { update := func() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
@ -45,7 +32,7 @@ func main() {
select { select {
case <-tick.C: case <-tick.C:
rangeLower := 0 rangeLower := 0
rangeUpper := 100 rangeUpper := 10
randomNum := rangeLower + rand.Intn(rangeUpper-rangeLower+1) randomNum := rangeLower + rand.Intn(rangeUpper-rangeLower+1)
barGraph.SetBarValue("cpu", randomNum) barGraph.SetBarValue("cpu", randomNum)
randomNum = rangeLower + rand.Intn(rangeUpper-rangeLower+1) randomNum = rangeLower + rand.Intn(rangeUpper-rangeLower+1)
@ -60,7 +47,7 @@ func main() {
} }
go update() go update()
if err := app.SetRoot(flex, false).EnableMouse(true).Run(); err != nil { if err := app.SetRoot(barGraph, false).EnableMouse(true).Run(); err != nil {
panic(err) panic(err)
} }
} }