github.com/wtfutil/wtf@v0.43.0/modules/bargraph/widget.go (about)

     1  package bargraph
     2  
     3  /**************
     4  This is a demo bargraph that just populates some random date/val data
     5  */
     6  
     7  import (
     8  	"math/rand"
     9  	"time"
    10  
    11  	"github.com/rivo/tview"
    12  
    13  	"github.com/wtfutil/wtf/view"
    14  )
    15  
    16  // Widget define wtf widget to register widget later
    17  type Widget struct {
    18  	view.BarGraph
    19  
    20  	tviewApp *tview.Application
    21  }
    22  
    23  // NewWidget Make new instance of widget
    24  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
    25  	widget := Widget{
    26  		BarGraph: view.NewBarGraph(tviewApp, redrawChan, "Sample Bar Graph", settings.Common),
    27  
    28  		tviewApp: tviewApp,
    29  	}
    30  
    31  	widget.View.SetWrap(true)
    32  	widget.View.SetWordWrap(true)
    33  
    34  	return &widget
    35  }
    36  
    37  /* -------------------- Exported Functions -------------------- */
    38  
    39  // MakeGraph - Load the dead drop stats
    40  func MakeGraph(widget *Widget) {
    41  
    42  	//this could come from config
    43  	const lineCount = 8
    44  	var stats [lineCount]view.Bar
    45  
    46  	barTime := time.Now()
    47  	for i := 0; i < lineCount; i++ {
    48  		barTime = barTime.Add(time.Minute)
    49  
    50  		bar := view.Bar{
    51  			Label:      barTime.Format("15:04"),
    52  			Percent:    rand.Intn(100-5) + 5,
    53  			LabelColor: "red",
    54  		}
    55  
    56  		stats[i] = bar
    57  	}
    58  
    59  	widget.BarGraph.BuildBars(stats[:])
    60  
    61  }
    62  
    63  // Refresh & update after interval time
    64  func (widget *Widget) Refresh() {
    65  
    66  	if widget.Disabled() {
    67  		return
    68  	}
    69  
    70  	widget.View.Clear()
    71  	MakeGraph(widget)
    72  }