Chart width data interpolation

pull/94/head
Miguel Mota 3 years ago
parent 295f163ae2
commit 6605cc548c

@ -385,6 +385,12 @@ func (ct *Cointop) ShowChartLoader() error {
// ChartWidth returns the width for chart
func (ct *Cointop) ChartWidth() int {
ct.debuglog("chartClampedWidth()")
return ct.width()
ct.debuglog("chartWidth()")
w := ct.width()
max := 130
if w > max {
return max
}
return w
}

@ -1,6 +1,8 @@
package chartplot
import (
"math"
"github.com/miguelmota/cointop/pkg/termui"
)
@ -56,6 +58,8 @@ func (c *ChartPlot) SetData(data []float64) {
// GetChartPoints ...
func (c *ChartPlot) GetChartPoints(width int) [][]rune {
axisYWidth := 30
c.t.Data = interpolateData(c.t.Data, (width*2)-axisYWidth)
termui.Body = termui.NewGrid()
termui.Body.Width = width
termui.Body.AddRows(
@ -82,3 +86,21 @@ func (c *ChartPlot) GetChartPoints(width int) [][]rune {
return points
}
func interpolateData(data []float64, width int) []float64 {
var res []float64
stepFactor := float64(len(data)-1) / float64(width-1)
res = append(res, data[0])
for i := 1; i < width-1; i++ {
step := float64(i) * stepFactor
before := math.Floor(step)
after := math.Ceil(step)
atPoint := step - before
pointBefore := data[int(before)]
pointAfter := data[int(after)]
interpolated := pointBefore + (pointAfter-pointBefore)*atPoint
res = append(res, interpolated)
}
res = append(res, data[len(data)-1])
return res
}

@ -80,7 +80,7 @@ func NewLineChart() *LineChart {
lc.Mode = "braille"
lc.DotStyle = '•'
lc.axisXLabelGap = 2
lc.axisYLabelGap = 1
lc.axisYLabelGap = 0
lc.bottomValue = math.Inf(1)
lc.topValue = math.Inf(-1)
return lc
@ -199,23 +199,15 @@ func (lc *LineChart) calcLabelX() {
func shortenFloatVal(x float64) string {
if x > 1e12 {
return fmt.Sprintf("%.2fT", x/1e12)
return fmt.Sprintf("%.4fT", x/1e12)
}
if x > 1e9 {
return fmt.Sprintf("%.2fB", x/1e9)
return fmt.Sprintf("%.4fB", x/1e9)
}
if x > 1e6 {
return fmt.Sprintf("%.2fB", x/1e6)
return fmt.Sprintf("%.4fB", x/1e6)
}
//if len(s)-3 > 3 {
//s = fmt.Sprintf("%.2e", x)
//}
//if x < 0 {
//s = fmt.Sprintf("%.2f", x)
//}
return fmt.Sprintf("%.2f", x)
return fmt.Sprintf("%.4f", x)
}
func (lc *LineChart) calcLabelY() {
@ -226,8 +218,12 @@ func (lc *LineChart) calcLabelY() {
lc.labelY = make([][]rune, n)
maxLen := 0
for i := 0; i < n; i++ {
val := lc.bottomValue + float64(i)*span/float64(n)
s := str2runes(shortenFloatVal(val))
v := lc.bottomValue + float64(i)*span/float64(n)
// don't show negative Y axis labels
if v < 0 {
continue
}
s := str2runes(shortenFloatVal(v))
if len(s) > maxLen {
maxLen = len(s)
}
@ -273,17 +269,20 @@ func (lc *LineChart) calcLayout() {
}
}
span := lc.maxY - lc.minY
//span := lc.maxY - lc.minY
if lc.minY < lc.bottomValue {
lc.bottomValue = lc.minY - 0.2*span
lc.bottomValue = lc.minY
//lc.bottomValue = lc.minY - 0.2*span
}
if lc.maxY > lc.topValue {
lc.topValue = lc.maxY + 0.2*span
lc.topValue = lc.maxY
//lc.topValue = lc.maxY + 0.2*span
}
lc.axisYHeight = lc.innerArea.Dy() - 2
offset := 2
lc.axisYHeight = lc.innerArea.Dy() - offset
lc.calcLabelY()
lc.axisXWidth = lc.innerArea.Dx() - 1 - lc.labelYSpace
@ -296,7 +295,8 @@ func (lc *LineChart) calcLayout() {
func (lc *LineChart) plotAxes() Buffer {
buf := NewBuffer()
origY := lc.innerArea.Min.Y + lc.innerArea.Dy() - 2
offset := 2
origY := lc.innerArea.Min.Y + lc.innerArea.Dy() - offset
origX := lc.innerArea.Min.X + lc.labelYSpace
buf.Set(origX, origY, Cell{Ch: ORIGIN, Fg: lc.AxesColor, Bg: lc.Bg})

Loading…
Cancel
Save