diff --git a/cointop/chart.go b/cointop/chart.go index 19fd5c8..87ee20f 100644 --- a/cointop/chart.go +++ b/cointop/chart.go @@ -173,14 +173,10 @@ func (ct *Cointop) ChartPoints(symbol string, name string) error { } // Resample cachedata - maxPoints := len(cacheData) - if maxPoints > 2*maxX { - maxPoints = 2 * maxX - } timeQuantum := timedata.CalculateTimeQuantum(cacheData) newStart := time.Unix(start, 0).Add(timeQuantum) newEnd := time.Unix(end, 0).Add(-timeQuantum) - timeData := timedata.ResampleTimeSeriesData(cacheData, float64(newStart.UnixMilli()), float64(newEnd.UnixMilli()), maxPoints) + timeData := timedata.ResampleTimeSeriesData(cacheData, float64(newStart.UnixMilli()), float64(newEnd.UnixMilli()), chart.GetChartDataSize(maxX)) // Extract just the values from the data var data []float64 @@ -276,17 +272,6 @@ func (ct *Cointop) PortfolioChart() error { allCacheData = append(allCacheData, PriceData{p, cacheData}) } - // Calculate how many data points to provide to the chart. Limit maxPoints to 2*maxX - maxPoints := 0 - for _, cacheData := range allCacheData { - if len(cacheData.data) > maxPoints { - maxPoints = len(cacheData.data) - } - } - if maxPoints > 2*maxX { - maxPoints = 2 * maxX - } - // Use the gap between price samples to adjust start/end in by one interval var timeQuantum time.Duration for _, cacheData := range allCacheData { @@ -301,7 +286,7 @@ func (ct *Cointop) PortfolioChart() error { // Resample and sum data var data []float64 for _, cacheData := range allCacheData { - coinData := timedata.ResampleTimeSeriesData(cacheData.data, float64(newStart.UnixMilli()), float64(newEnd.UnixMilli()), maxPoints) + coinData := timedata.ResampleTimeSeriesData(cacheData.data, float64(newStart.UnixMilli()), float64(newEnd.UnixMilli()), chart.GetChartDataSize(maxX)) // sum (excluding NaN) for i := range coinData { price := coinData[i][1] diff --git a/pkg/chartplot/chartplot.go b/pkg/chartplot/chartplot.go index c9101b3..cc7e8b6 100644 --- a/pkg/chartplot/chartplot.go +++ b/pkg/chartplot/chartplot.go @@ -56,10 +56,18 @@ func (c *ChartPlot) SetData(data []float64) { c.t.Data = data } +func (c *ChartPlot) GetChartDataSize(width int) int { + axisYWidth := 30 + return (width * 2) - axisYWidth +} + // GetChartPoints ... func (c *ChartPlot) GetChartPoints(width int) [][]rune { - axisYWidth := 30 - c.t.Data = interpolateData(c.t.Data, (width*2)-axisYWidth) + targetWidth := c.GetChartDataSize(width) + if len(c.t.Data) != targetWidth { + // Don't resample data if it's already the right size + c.t.Data = interpolateData(c.t.Data, targetWidth) + } termui.Body = termui.NewGrid() termui.Body.Width = width termui.Body.AddRows(