Improve graph quality with custom CSS

This commit is contained in:
nemunaire 2022-08-14 13:31:54 +02:00
parent cb2aea4113
commit c03e90d38a
2 changed files with 43 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import os import os
from tempfile import NamedTemporaryFile
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
@ -18,6 +19,35 @@ class Config:
self.fnt_RI_path = "/usr/share/fonts/TTF/LinBiolinum_RIah.ttf" self.fnt_RI_path = "/usr/share/fonts/TTF/LinBiolinum_RIah.ttf"
self.fnt_RB_path = "/usr/share/fonts/TTF/LinBiolinum_RBah.ttf" self.fnt_RB_path = "/usr/share/fonts/TTF/LinBiolinum_RBah.ttf"
self.css_overlay = NamedTemporaryFile()
with open(self.css_overlay.name, 'w') as f:
f.write('''
{{ id }} .bound {
font-size: 0.9em;
}
{{ id }} .gauge path {
fill-opacity: 1 !important;
}
{{ id }} .series .line {
stroke-width: 2 !important;
}
{{ id }} .x .guides .line {
stroke-width: 0 !important;
}
{{ id }} .y .guides .line {
stroke: #555;
stroke-width: 0.7;
}
{{ id }} .y .guides .line.axis {
stroke: #000;
stroke-width: 1;
}
''')
import pygal
self.pygal_config = pygal.Config()
self.pygal_config.css.append('file://' + self.css_overlay.name)
from pygal.style import Style from pygal.style import Style
self.pygal_custom_style = Style( self.pygal_custom_style = Style(
background='white', background='white',

View File

@ -188,7 +188,7 @@ class WeatherRainModule:
thisdayweather = WeatherAPI().get_daily()["data"][0] thisdayweather = WeatherAPI().get_daily()["data"][0]
gauge = pygal.SolidGauge(half_pie=True, inner_radius=0.70, width=width, height=height*1.55, style=config.pygal_custom_style, show_legend=False, margin_top=-height*0.58, margin_left=1, margin_right=1) gauge = pygal.SolidGauge(config.pygal_config, half_pie=True, inner_radius=0.70, width=width, height=height*1.55, style=config.pygal_custom_style, show_legend=False, margin_top=-height*0.58, margin_left=1, margin_right=1)
percent_formatter = lambda x: '{:.10g}%'.format(x) percent_formatter = lambda x: '{:.10g}%'.format(x)
gauge.value_formatter = percent_formatter gauge.value_formatter = percent_formatter
@ -222,17 +222,25 @@ class WeatherTemperatureModule:
else: else:
thisdayweather = thisdayweather[0] thisdayweather = thisdayweather[0]
line_chart = pygal.Line(interpolate='cubic', width=width, height=height, inverse_y_axis=False, x_label_rotation=45, secondary_range=(0,100) if thisdayweather["precipProbability"] > 0 else (0,10), **config.charts_opts)
line_chart.value_formatter = lambda x: "%d" % x
hours_weather = WeatherAPI().get_hourly() hours_weather = WeatherAPI().get_hourly()
hourly_min = 0
hourly_max = 0
for h in hours_weather["data"]:
if hourly_min > h["temperature"]:
hourly_min = h["temperature"]
if hourly_max < h["temperature"]:
hourly_max = h["temperature"]
line_chart = pygal.Line(config.pygal_config, interpolate='cubic', width=width+10, height=height, inverse_y_axis=False, x_label_rotation=45, range=(hourly_min, hourly_max), secondary_range=(0,100) if thisdayweather["precipProbability"] > 0 else (0,10), **config.charts_opts)
line_chart.value_formatter = lambda x: "%d" % x
line_chart.x_labels = [WeatherAPI().read_timestamp(d["time"]).strftime("%Hh") if datetime.fromtimestamp(d["time"]).hour % 2 == 0 else "" for d in hours_weather["data"][:self.limit_futur]] line_chart.x_labels = [WeatherAPI().read_timestamp(d["time"]).strftime("%Hh") if datetime.fromtimestamp(d["time"]).hour % 2 == 0 else "" for d in hours_weather["data"][:self.limit_futur]]
line_chart.add('Températures', [d["temperature"] for d in hours_weather["data"][:self.limit_futur]], show_dots=False) line_chart.add('Températures', [d["temperature"] for d in hours_weather["data"][:self.limit_futur]], show_dots=False)
if thisdayweather["precipProbability"] > 0: if thisdayweather["precipProbability"] > 0:
line_chart.add('Précipitations', [d["precipProbability"] * 100 for d in hours_weather["data"][:self.limit_futur]], secondary=True, show_dots=False, fill=True) line_chart.add('Précipitations', [d["precipProbability"] * 100 for d in hours_weather["data"][:self.limit_futur]], secondary=True, show_dots=False, fill=True if hourly_min == 0 else False)
else: else:
line_chart.add('Index UV', [d["uvIndex"] for d in hours_weather["data"][:self.limit_futur]], secondary=True, show_dots=False) line_chart.add('Index UV', [d["uvIndex"] for d in hours_weather["data"][:self.limit_futur]], secondary=True, show_dots=False)