termui/_examples/linechart.go

73 lines
1.6 KiB
Go
Raw Normal View History

2017-01-13 23:07:43 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2015-03-20 14:21:50 -06:00
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
// +build ignore
package main
import (
2019-01-23 21:12:10 -07:00
"log"
"math"
2018-09-06 15:48:09 -06:00
ui "github.com/gizak/termui"
2019-01-23 21:12:10 -07:00
"github.com/gizak/termui/widgets"
)
func main() {
2019-01-23 21:12:10 -07:00
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
2018-09-06 15:48:09 -06:00
defer ui.Close()
2019-01-23 21:12:10 -07:00
sinData := func() [][]float64 {
n := 220
2019-01-23 21:12:10 -07:00
data := make([][]float64, 2)
data[0] = make([]float64, n)
data[1] = make([]float64, n)
2018-08-16 18:40:32 -06:00
for i := 0; i < n; i++ {
2019-01-23 21:12:10 -07:00
data[0][i] = 1 + math.Sin(float64(i)/5)
data[1][i] = 1 + math.Cos(float64(i)/5)
}
2019-01-23 21:12:10 -07:00
return data
}()
2019-01-23 21:12:10 -07:00
lc0 := widgets.NewLineChart()
lc0.Title = "braille-mode Line Chart"
lc0.Data = sinData
lc0.SetRect(0, 0, 50, 15)
2018-09-06 15:48:09 -06:00
lc0.AxesColor = ui.ColorWhite
2019-01-23 21:12:10 -07:00
lc0.LineColors[0] = ui.ColorGreen
2019-01-23 21:12:10 -07:00
lc1 := widgets.NewLineChart()
lc1.Title = "custom Line Chart"
lc1.LineType = widgets.DotLine
lc1.Data = [][]float64{[]float64{1, 2, 3, 4, 5}}
lc1.SetRect(50, 0, 75, 10)
lc1.DotChar = '+'
2018-09-06 15:48:09 -06:00
lc1.AxesColor = ui.ColorWhite
2019-01-23 21:12:10 -07:00
lc1.LineColors[0] = ui.ColorYellow
lc1.DrawDirection = widgets.DrawLeft
2019-01-23 21:12:10 -07:00
lc2 := widgets.NewLineChart()
lc2.Title = "dot-mode Line Chart"
lc2.LineType = widgets.DotLine
lc2.Data = make([][]float64, 2)
lc2.Data[0] = sinData[0][4:]
lc2.Data[1] = sinData[1][4:]
lc2.SetRect(0, 15, 50, 30)
2018-09-06 15:48:09 -06:00
lc2.AxesColor = ui.ColorWhite
2019-01-23 21:12:10 -07:00
lc2.LineColors[0] = ui.ColorCyan
2018-09-06 15:48:09 -06:00
ui.Render(lc0, lc1, lc2)
uiEvents := ui.PollEvents()
2018-11-28 19:19:34 -07:00
for {
e := <-uiEvents
2018-11-28 19:19:34 -07:00
switch e.ID {
case "q", "<C-c>":
return
}
}
}