mirror of
https://github.com/navidys/tvxwidgets.git
synced 2025-05-01 22:18:14 +08:00
bar chart initial commit
This commit is contained in:
parent
e08fdd7e12
commit
01198cf725
20
barchart.go
20
barchart.go
@ -75,18 +75,18 @@ func (c *BarChart) Draw(screen tcell.Screen) {
|
||||
}
|
||||
// draw graph y-axis
|
||||
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
|
||||
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)
|
||||
screen.SetContent(x+maxValLenght-1, xAxisStartY, '0', nil, style)
|
||||
tview.PrintJoinedSemigraphics(screen, x+maxValLenght, xAxisStartY, tview.BoxDrawingsLightVerticalAndRight, style)
|
||||
tview.PrintJoinedSemigraphics(screen, x+maxValLenght-1, xAxisStartY, '0', style)
|
||||
|
||||
mxValRune := []rune(maxValueSr)
|
||||
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
|
||||
@ -100,14 +100,14 @@ func (c *BarChart) Draw(screen tcell.Screen) {
|
||||
// set labels
|
||||
r := []rune(item.label)
|
||||
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
|
||||
bStyle := style.Foreground(item.color)
|
||||
barHeight := c.getHeight(valueMaxHeight, item.value)
|
||||
for k := 0; k < barHeight; k++ {
|
||||
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)
|
||||
vRune := []rune(vSt)
|
||||
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
|
||||
@ -135,6 +135,10 @@ func (c *BarChart) SetBorder(status bool) {
|
||||
c.Box.SetBorder(status)
|
||||
}
|
||||
|
||||
func (c *BarChart) GetRect() (int, int, int, int) {
|
||||
return c.Box.GetRect()
|
||||
}
|
||||
|
||||
// SetRect sets rect for this primitive.
|
||||
func (c *BarChart) SetRect(x, y, width, height int) {
|
||||
c.Box.SetRect(x, y, width, height)
|
||||
|
@ -12,31 +12,18 @@ import (
|
||||
|
||||
func main() {
|
||||
app := tview.NewApplication()
|
||||
flex := tview.NewFlex().SetDirection(tview.FlexRow)
|
||||
|
||||
barGraph := tgraph.NewBarChart()
|
||||
barGraph.SetBorder(true)
|
||||
barGraph.SetTitle("System Resource Usage")
|
||||
// display system metric usage
|
||||
barGraph.AddBar("cpu", 80, tcell.ColorBlue)
|
||||
barGraph.AddBar("mem", 20, tcell.ColorRed)
|
||||
barGraph.AddBar("swap", 40, tcell.ColorGreen)
|
||||
barGraph.AddBar("disk", 40, tcell.ColorOrange)
|
||||
barGraph.AddBar("cpu", 8, tcell.ColorBlue)
|
||||
barGraph.AddBar("mem", 2, tcell.ColorRed)
|
||||
barGraph.AddBar("swap", 4, tcell.ColorGreen)
|
||||
barGraph.AddBar("disk", 4, tcell.ColorOrange)
|
||||
barGraph.SetMaxValue(100)
|
||||
|
||||
bar2 := tgraph.NewBarChart()
|
||||
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)
|
||||
barGraph.SetRect(10, 10, 50, 20)
|
||||
|
||||
update := func() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
@ -45,7 +32,7 @@ func main() {
|
||||
select {
|
||||
case <-tick.C:
|
||||
rangeLower := 0
|
||||
rangeUpper := 100
|
||||
rangeUpper := 10
|
||||
randomNum := rangeLower + rand.Intn(rangeUpper-rangeLower+1)
|
||||
barGraph.SetBarValue("cpu", randomNum)
|
||||
randomNum = rangeLower + rand.Intn(rangeUpper-rangeLower+1)
|
||||
@ -60,7 +47,7 @@ func main() {
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user