adjust stock ticker when charts are disabled to make it more compact

This commit is contained in:
Chuck
2025-07-23 17:18:21 -05:00
parent 9280538e08
commit 0781b72c1d
2 changed files with 19 additions and 21 deletions

View File

@@ -88,13 +88,13 @@
"stocks": { "stocks": {
"enabled": true, "enabled": true,
"update_interval": 600, "update_interval": 600,
"scroll_speed": 1,
"scroll_delay": 0.01,
"toggle_chart": false,
"symbols": [ "symbols": [
"ASTS", "SCHD", "INTC", "NVDA", "T", "VOO", "SMCI" "ASTS", "SCHD", "INTC", "NVDA", "T", "VOO", "SMCI"
], ],
"display_format": "{symbol}: ${price} ({change}%)", "display_format": "{symbol}: ${price} ({change}%)"
"scroll_speed": 1,
"scroll_delay": 0.01,
"toggle_chart": false
}, },
"crypto": { "crypto": {
"enabled": true, "enabled": true,

View File

@@ -425,8 +425,8 @@ class StockManager:
def _create_stock_display(self, symbol: str, price: float, change: float, change_percent: float, is_crypto: bool = False) -> Image.Image: def _create_stock_display(self, symbol: str, price: float, change: float, change_percent: float, is_crypto: bool = False) -> Image.Image:
"""Create a display image for a stock or crypto with logo, symbol, price, and change.""" """Create a display image for a stock or crypto with logo, symbol, price, and change."""
# Create a wider image for scrolling # Create a wider image for scrolling - adjust width based on chart toggle
width = self.display_manager.matrix.width * 2 width = self.display_manager.matrix.width * (2 if self.toggle_chart else 1.5) # Reduced width when no chart
height = self.display_manager.matrix.height height = self.display_manager.matrix.height
image = Image.new('RGB', (width, height), color=(0, 0, 0)) image = Image.new('RGB', (width, height), color=(0, 0, 0))
draw = ImageDraw.Draw(image) draw = ImageDraw.Draw(image)
@@ -461,10 +461,12 @@ class StockManager:
price_bbox = draw.textbbox((0, 0), price_text, font=price_font) price_bbox = draw.textbbox((0, 0), price_text, font=price_font)
change_bbox = draw.textbbox((0, 0), change_text, font=small_font) change_bbox = draw.textbbox((0, 0), change_text, font=small_font)
# Calculate total height needed # Calculate total height needed - adjust gaps based on chart toggle
text_gap = 2 if self.toggle_chart else 1 # Reduced gap when no chart
total_text_height = (symbol_bbox[3] - symbol_bbox[1]) + \ total_text_height = (symbol_bbox[3] - symbol_bbox[1]) + \
(price_bbox[3] - price_bbox[1]) + \ (price_bbox[3] - price_bbox[1]) + \
(change_bbox[3] - change_bbox[1]) (change_bbox[3] - change_bbox[1]) + \
(text_gap * 2) # Account for gaps between elements
# Calculate starting y position to center all text # Calculate starting y position to center all text
start_y = (height - total_text_height) // 2 start_y = (height - total_text_height) // 2
@@ -474,8 +476,8 @@ class StockManager:
# When chart is enabled, center text more to the left # When chart is enabled, center text more to the left
column_x = width // 2.85 column_x = width // 2.85
else: else:
# When chart is disabled, center text more to the right # When chart is disabled, position text closer to logo
column_x = width // 2.2 column_x = width // 3
# Draw symbol # Draw symbol
symbol_width = symbol_bbox[2] - symbol_bbox[0] symbol_width = symbol_bbox[2] - symbol_bbox[0]
@@ -485,13 +487,13 @@ class StockManager:
# Draw price # Draw price
price_width = price_bbox[2] - price_bbox[0] price_width = price_bbox[2] - price_bbox[0]
price_x = column_x - (price_width // 2) price_x = column_x - (price_width // 2)
price_y = start_y + (symbol_bbox[3] - symbol_bbox[1]) + 2 # Small gap after symbol price_y = start_y + (symbol_bbox[3] - symbol_bbox[1]) + text_gap # Adjusted gap
draw.text((price_x, price_y), price_text, font=price_font, fill=(255, 255, 255)) draw.text((price_x, price_y), price_text, font=price_font, fill=(255, 255, 255))
# Draw change with color based on value # Draw change with color based on value
change_width = change_bbox[2] - change_bbox[0] change_width = change_bbox[2] - change_bbox[0]
change_x = column_x - (change_width // 2) change_x = column_x - (change_width // 2)
change_y = price_y + (price_bbox[3] - price_bbox[1]) + 2 # Small gap after price change_y = price_y + (price_bbox[3] - price_bbox[1]) + text_gap # Adjusted gap
change_color = (0, 255, 0) if change >= 0 else (255, 0, 0) change_color = (0, 255, 0) if change >= 0 else (255, 0, 0)
draw.text((change_x, change_y), change_text, font=small_font, fill=change_color) draw.text((change_x, change_y), change_text, font=small_font, fill=change_color)
@@ -505,7 +507,7 @@ class StockManager:
# Calculate chart dimensions # Calculate chart dimensions
chart_width = int(width // 2.5) # Reduced from width//2.5 to prevent overlap chart_width = int(width // 2.5) # Reduced from width//2.5 to prevent overlap
chart_height = height // 1.5 chart_height = height // 1.5
chart_x = width - chart_width # - 4 # 4px margin from right edge chart_x = width - chart_width - 4 # 4px margin from right edge
chart_y = (height - chart_height) // 2 chart_y = (height - chart_height) // 2
# Find min and max prices for scaling # Find min and max prices for scaling
@@ -526,14 +528,10 @@ class StockManager:
y = chart_y + chart_height - int(((price - min_price) / price_range) * chart_height) y = chart_y + chart_height - int(((price - min_price) / price_range) * chart_height)
points.append((x, y)) points.append((x, y))
# Draw the line # Draw lines between points
if len(points) >= 2: color = self._get_stock_color(symbol)
draw.line(points, fill=change_color, width=1) for i in range(len(points) - 1):
draw.line([points[i], points[i + 1]], fill=color, width=1)
# Draw dots at start and end points only
for point in [points[0], points[-1]]:
draw.ellipse([point[0]-1, point[1]-1, point[0]+1, point[1]+1],
fill=change_color)
return image return image