forked from polygon-io/go-app-ticker-wall
/
tickers.go
196 lines (161 loc) · 5.81 KB
/
tickers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package gui
import (
"fmt"
"math"
"github.com/megacoder/go-app-ticker-wall/models"
"github.com/megacoder/nanovgo"
)
func (g *GUI) renderTickers(globalOffset float32) error {
tickers := g.DetermineTickersForRender(globalOffset)
for _, ticker := range tickers {
// This happens when there are more screen pixels than we can cover with the current amount of tickers.
// The solution is to add more tickers, but we should fix the underlying maths issue in `DetermineTickersForRender`.
// TODO: Fix layout calculation issue in `DetermineTickersForRender`
if ticker == nil {
continue
}
g.renderTicker(ticker, globalOffset)
}
return nil
}
const (
graphSize = 180
// graphViewportPercentage is the amount of movement ( up or down ) we chart at
// native scale before we are required to "squish" to fit into the viewport.
graphViewportPercentage = .04 // 4% viewport movement range up or down ( 8% total ).
// Ticker box settings.
tickerBoxHeight = 240
tickerBoxMargin = 30
tickerBoxPadding = 50
tickerBoxBorderRadius = 8
// Font sizes.
upperRowFontSize = 96
bottomRowFontSize = 58
maxCompanyNameCharacters = 14
)
// renderTickerBg sets the background of the ticker box to a solid color.
func (g *GUI) renderTickerBg(leftOffset float32) {
screen := g.client.GetScreen()
settings := g.client.GetSettings()
topOffset := float32((screen.Height / 2) - (tickerBoxHeight / 2))
leftOffset += (tickerBoxMargin / 2)
boxWidth := float32(settings.TickerBoxWidth) - tickerBoxMargin
// Set BG color
g.nanoCtx.BeginPath()
g.nanoCtx.RoundedRect(leftOffset, topOffset, boxWidth, tickerBoxHeight, tickerBoxBorderRadius)
g.nanoCtx.SetFillColor(settings.TickerBoxBGColor.ToNanov())
g.nanoCtx.Fill()
}
func (g *GUI) renderTicker(ticker *models.Ticker, globalOffset float32) {
// Get necessary parameters.
settings := g.client.GetSettings()
screen := g.client.GetScreen()
tickerOffset := g.TickerOffset(globalOffset, ticker)
// Render background rectangle.
g.renderTickerBg(tickerOffset)
// Calculate offsets.
offsetLeft := (tickerOffset + (tickerBoxMargin / 2)) + tickerBoxPadding
offsetTop := float32((screen.Height / 2) - (tickerBoxHeight / 2))
offsetRight := ((tickerOffset + float32(settings.TickerBoxWidth)) - tickerBoxMargin) - tickerBoxPadding
// Calculate the Y offset for the two rows. Using percentages so if we change
// ticker box size, it should scale accordingly.
upperRowTopOffset := offsetTop + (tickerBoxHeight * .33)
lowerRowTopOffset := offsetTop + (tickerBoxHeight * .66)
// Actual text rendering ---
// Ticker.
g.nanoCtx.SetFontFace("sans-bold")
g.nanoCtx.SetTextAlign(nanovgo.AlignLeft | nanovgo.AlignMiddle)
g.nanoCtx.SetFontSize(upperRowFontSize)
g.nanoCtx.SetFillColor(settings.FontColor.ToNanov())
g.nanoCtx.TextBox(offsetLeft, upperRowTopOffset, 900, ticker.Ticker)
// Price.
textString := fmt.Sprintf("%.2f", ticker.Price)
boundedTextWidth, _ := g.nanoCtx.TextBounds(0, 0, textString)
g.nanoCtx.Text(offsetRight-boundedTextWidth, upperRowTopOffset, textString)
// Company Name.
g.nanoCtx.SetFontSize(bottomRowFontSize)
g.nanoCtx.SetFontFace("sans-light")
companyName := ticker.CompanyName
if len(companyName) >= maxCompanyNameCharacters {
companyName = companyName[:(maxCompanyNameCharacters-3)] + "..."
}
g.nanoCtx.TextBox(offsetLeft, lowerRowTopOffset, 900, companyName)
// Percentage Gained / Loss test.
directionalColor := settings.UpColor
if ticker.PriceChangePercentage < 0 {
directionalColor = settings.DownColor
}
priceDifference := ticker.Price - ticker.PreviousClosePrice
g.nanoCtx.SetFillColor(directionalColor.ToNanov())
textString = fmt.Sprintf("%+.2f (%+.2f%%)", priceDifference, ticker.PriceChangePercentage)
boundedTextWidth, _ = g.nanoCtx.TextBounds(0, 0, textString)
g.nanoCtx.Text(offsetRight-boundedTextWidth, lowerRowTopOffset, textString)
// Graph.
topOffset := float32((screen.Height / 2) - (graphSize / 2))
g.renderGraph(ticker, offsetLeft+400, topOffset, graphSize, directionalColor)
}
func (g *GUI) renderGraph(ticker *models.Ticker, x, y, width float32, color *models.RGBA) {
g.drawGraph(g.nanoCtx, ticker, x, y, width, width, 2, color)
}
func (g *GUI) drawGraph(ctx *nanovgo.Context, ticker *models.Ticker, x, y, w, h, t float32, color *models.RGBA) {
points := len(ticker.Aggs)
// if we have no data, don't continue.
if points < 2 {
return
}
sx := make([]float32, points)
sy := make([]float32, points)
dx := w / float32(points-1)
// Generate graph points.
var min, max float64
for i, agg := range ticker.Aggs {
// Check if max.
if agg.Price > max {
max = agg.Price
}
// Check if min.
if agg.Price < min || min == 0 {
min = agg.Price
}
// Set X,Y for this point.
sy[i] = float32(agg.Price)
sx[i] = x + float32(i)*dx
}
// Middle of our range.
midRange := float32((min + max) / 2)
// Now we must normalize Y axis to fix in our bounds.
var absMax float32
for i, val := range sy {
sy[i] = (val - midRange) / midRange
absValue := float32(math.Abs(float64(sy[i])))
if absValue > absMax {
absMax = absValue
}
}
// If our values are outside of the viewport range percentage, we must squish values
// to be inside our desired viewport range percentage.
if absMax > graphViewportPercentage {
for i, val := range sy {
sy[i] = (val / absMax) * graphViewportPercentage
}
}
// Change percentage diff to pixel offsets:
middleOfViewport := h / 2
baseMultiplier := (middleOfViewport / graphViewportPercentage)
for i, val := range sy {
sy[i] = (y + h) - ((baseMultiplier * val) + middleOfViewport)
}
ctx.BeginPath()
ctx.MoveTo(sx[0], sy[0])
for i := 1; i < points; i++ {
ctx.LineTo(sx[i], sy[i])
}
ctx.SetStrokeColor(color.ToNanov())
ctx.SetStrokeWidth(4.0)
ctx.Stroke()
ctx.BeginPath()
ctx.Circle(sx[points-1], sy[points-1], 6.0)
ctx.SetFillColor(color.ToNanov())
ctx.Fill()
ctx.SetStrokeWidth(1.0)
}