Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolution of bar charts #310

Merged
merged 6 commits into from Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -16,6 +16,7 @@ New features
Bugfixes
-----------
* Fix recording time of schedules triggered by UDI events [see `PR #300 <http://www.github.com/FlexMeasures/flexmeasures/pull/300>`_]
* Set bar width of bar charts based on sensor resolution [see `PR #310 <http://www.github.com/FlexMeasures/flexmeasures/pull/310>`_]

Infrastructure / Support
----------------------
Expand Down
19 changes: 14 additions & 5 deletions flexmeasures/data/models/charts/belief_charts.py
@@ -1,21 +1,24 @@
from flexmeasures.data.models.charts.defaults import FIELD_DEFINITIONS
from flexmeasures.utils.flexmeasures_inflection import capitalize


def bar_chart(title: str, quantity: str = "unknown quantity", unit: str = "a.u."):
if not unit:
unit = "a.u."
def bar_chart(
sensor: "Sensor", # noqa F821
):
unit = sensor.unit if sensor.unit else "a.u."
event_value_field_definition = dict(
title=f"{quantity} ({unit})",
title=f"{capitalize(sensor.sensor_type)} ({unit})",
format=".3s",
stack=None,
**FIELD_DEFINITIONS["event_value"],
)
return {
"description": "A simple bar chart.",
"title": title,
"title": capitalize(sensor.name),
"mark": "bar",
"encoding": {
"x": FIELD_DEFINITIONS["event_start"],
"x2": FIELD_DEFINITIONS["event_end"],
"y": event_value_field_definition,
"color": FIELD_DEFINITIONS["source"],
"opacity": {"value": 0.7},
Expand All @@ -25,4 +28,10 @@ def bar_chart(title: str, quantity: str = "unknown quantity", unit: str = "a.u."
FIELD_DEFINITIONS["source"],
],
},
"transform": [
{
"calculate": f"datum.event_start + {sensor.event_resolution.total_seconds() * 1000}",
"as": "event_end",
},
],
}
11 changes: 9 additions & 2 deletions flexmeasures/data/models/charts/defaults.py
Expand Up @@ -16,6 +16,11 @@
type="temporal",
title=None,
),
"event_end": dict(
field="event_end",
type="temporal",
title=None,
),
"event_value": dict(
field="event_value",
type="quantitative",
Expand Down Expand Up @@ -50,12 +55,14 @@ def decorated_chart_specs(*args, **kwargs):
chart_specs["data"] = {"name": dataset_name}
chart_specs["height"] = HEIGHT
chart_specs["width"] = WIDTH
chart_specs["transform"] = [
if "transform" not in chart_specs:
chart_specs["transform"] = []
chart_specs["transform"].append(
{
"as": "full_date",
"calculate": f"timeFormat(datum.event_start, '{TIME_FORMAT}')",
}
]
)
return chart_specs

return decorated_chart_specs
5 changes: 1 addition & 4 deletions flexmeasures/data/models/time_series.py
Expand Up @@ -28,7 +28,6 @@
from flexmeasures.data.models.generic_assets import GenericAsset
from flexmeasures.data.models.validation_utils import check_required_attributes
from flexmeasures.utils.time_utils import server_now
from flexmeasures.utils.flexmeasures_inflection import capitalize


class Sensor(db.Model, tb.SensorDBMixin, AuthModelMixin):
Expand Down Expand Up @@ -270,9 +269,7 @@ def chart(
) # todo remove this placeholder when sensor types are modelled
chart_specs = chart_type_to_chart_specs(
chart_type,
title=capitalize(self.name),
quantity=capitalize(self.sensor_type),
unit=self.unit,
sensor=self,
dataset_name=dataset_name,
**kwargs,
)
Expand Down