Skip to content

Commit

Permalink
Merge fc049a1 into 16a1a79
Browse files Browse the repository at this point in the history
  • Loading branch information
Flix6x committed Jun 13, 2023
2 parents 16a1a79 + fc049a1 commit e0c394d
Showing 1 changed file with 66 additions and 14 deletions.
80 changes: 66 additions & 14 deletions flexmeasures/data/models/charts/belief_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,35 @@ def matrix_chart(
event_ends_before.timestamp() * 10**3,
],
}
mark = {"type": "rect", "clip": True, "opacity": 0.7}
tooltip = [
FIELD_DEFINITIONS["full_date"],
{
**event_value_field_definition,
**dict(title=f"{capitalize(sensor.sensor_type)}"),
},
FIELD_DEFINITIONS["source_name_and_id"],
FIELD_DEFINITIONS["source_model"],
]
chart_specs = {
"description": "A simple heatmap chart showing sensor data.",
# the sensor type is already shown as the y-axis title (avoid redundant info)
"title": capitalize(sensor.name) if sensor.name != sensor.sensor_type else None,
"layer": [
{
"mark": {
"type": "rect",
"clip": True,
},
"mark": mark,
"encoding": {
"x": event_start_field_definition,
"y": event_start_date_field_definition,
"color": event_value_field_definition,
"detail": FIELD_DEFINITIONS["source"],
"opacity": {"value": 0.7},
"tooltip": [
FIELD_DEFINITIONS["full_date"],
{
**event_value_field_definition,
**dict(title=f"{capitalize(sensor.sensor_type)}"),
},
FIELD_DEFINITIONS["source_name_and_id"],
FIELD_DEFINITIONS["source_model"],
],
"tooltip": tooltip,
},
"transform": [
{
# Mask overlapping data during the fall DST transition, which we show later with a special layer
"filter": "timezoneoffset(datum.event_start) >= timezoneoffset(datum.event_start + 60 * 60 * 1000) && timezoneoffset(datum.event_start) <= timezoneoffset(datum.event_start - 60 * 60 * 1000)"
},
{
"calculate": "datum.source.name + ' (ID: ' + datum.source.id + ')'",
"as": "source_name_and_id",
Expand All @@ -189,6 +191,12 @@ def matrix_chart(
},
},
},
create_fall_dst_transition_layer(
mark,
event_value_field_definition,
event_start_field_definition,
tooltip,
),
],
}
for k, v in override_chart_specs.items():
Expand All @@ -200,6 +208,50 @@ def matrix_chart(
return chart_specs


def create_fall_dst_transition_layer(
mark, event_value_field_definition, event_start_field_definition, tooltip
) -> dict:
"""Special layer for showing data during the daylight savings time transition in fall."""
return {
"mark": mark,
"encoding": {
"x": event_start_field_definition,
"y": {
"field": "dst_transition_event_start",
"type": "temporal",
"title": None,
"timeUnit": {"unit": "yearmonthdatehours", "step": 12},
},
"y2": {
"field": "dst_transition_event_start_next",
"timeUnit": {"unit": "yearmonthdatehours", "step": 12},
},
"color": event_value_field_definition,
"detail": FIELD_DEFINITIONS["source"],
"tooltip": tooltip,
},
"transform": [
{
"filter": "timezoneoffset(datum.event_start) < timezoneoffset(datum.event_start + 60 * 60 * 1000) || timezoneoffset(datum.event_start) > timezoneoffset(datum.event_start - 60 * 60 * 1000)"
},
{
# Push the more recent hour into the second 12-hour bin
"calculate": "timezoneoffset(datum.event_start + 60 * 60 * 1000) > timezoneoffset(datum.event_start) ? datum.event_start : datum.event_start + 12 * 60 * 60 * 1000",
"as": "dst_transition_event_start",
},
{
# Calculate a time point in the next 12-hour bin
"calculate": "datum.dst_transition_event_start + 12 * 60 * 60 * 1000 - 60 * 60 * 1000",
"as": "dst_transition_event_start_next",
},
{
"calculate": "datum.source.name + ' (ID: ' + datum.source.id + ')'",
"as": "source_name_and_id",
},
],
}


def chart_for_multiple_sensors(
sensors_to_show: list["Sensor", list["Sensor"]], # noqa F821
event_starts_after: datetime | None = None,
Expand Down

0 comments on commit e0c394d

Please sign in to comment.