diff options
author | Miguel Mota <miguelmota2@gmail.com> | 2021-02-18 23:57:00 -0800 |
---|---|---|
committer | Miguel Mota <miguelmota2@gmail.com> | 2021-02-18 23:58:14 -0800 |
commit | 6605cc548cf6bb359970d9773027e65ac46e7d34 (patch) | |
tree | d0ef6b820e28e5fee9b446e5f0ec81088b8e4d27 | |
parent | 295f163ae24e8bde849f72bfdf7b604234b4691e (diff) |
Chart width data interpolation
-rw-r--r-- | cointop/chart.go | 10 | ||||
-rw-r--r-- | pkg/chartplot/chartplot.go | 22 | ||||
-rw-r--r-- | pkg/termui/linechart.go | 40 |
3 files changed, 50 insertions, 22 deletions
diff --git a/cointop/chart.go b/cointop/chart.go index 1326b10..935e330 100644 --- a/cointop/chart.go +++ b/cointop/chart.go @@ -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 } diff --git a/pkg/chartplot/chartplot.go b/pkg/chartplot/chartplot.go index df52972..8dbbd4a 100644 --- a/pkg/chartplot/chartplot.go +++ b/pkg/chartplot/chartplot.go @@ -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 +} diff --git a/pkg/termui/linechart.go b/pkg/termui/linechart.go index ab26670..68bcfae 100644 --- a/pkg/termui/linechart.go +++ b/pkg/termui/linechart.go @@ -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}) |