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 multi sourced belief charts #228

Merged
merged 4 commits into from Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 14 additions & 13 deletions flexmeasures/data/models/charts/belief_charts.py
@@ -1,27 +1,28 @@
from flexmeasures.data.models.charts.defaults import TIME_TITLE, TIME_TOOLTIP_TITLE
from flexmeasures.data.models.charts.defaults import FIELD_DEFINITIONS


def bar_chart(title: str, quantity: str = "unknown quantity", unit: str = "a.u."):
if not unit:
unit = "a.u."
event_value_field_definition = dict(
title=f"{quantity} ({unit})",
format=".3s",
stack=None,
**FIELD_DEFINITIONS["event_value"],
)
return {
"description": "A simple bar chart.",
"title": title,
"mark": "bar",
"encoding": {
"x": {"field": "event_start", "type": "T", "title": TIME_TITLE},
"y": {
"field": "event_value",
"type": "quantitative",
"title": quantity + " (" + unit + ")",
},
"x": FIELD_DEFINITIONS["event_start"],
"y": event_value_field_definition,
"color": FIELD_DEFINITIONS["source"],
"opacity": {"value": 0.7},
"tooltip": [
{"field": "full_date", "title": TIME_TOOLTIP_TITLE, "type": "nominal"},
{
"field": "event_value",
"title": quantity + " (" + unit + ")",
"type": "quantitative",
},
FIELD_DEFINITIONS["full_date"],
event_value_field_definition,
FIELD_DEFINITIONS["source"],
],
},
}
23 changes: 21 additions & 2 deletions flexmeasures/data/models/charts/defaults.py
Expand Up @@ -9,9 +9,28 @@
REDUCED_HEIGHT = REDUCED_WIDTH = 60
SELECTOR_COLOR = "darkred"
TIME_FORMAT = "%I:%M %p on %A %b %e, %Y"
TIME_TOOLTIP_TITLE = "Time and date"
TIME_TITLE = None
TIME_SELECTION_TOOLTIP = "Click and drag to select a time window"
FIELD_DEFINITIONS = {
"event_start": dict(
field="event_start",
type="temporal",
title=None,
),
"event_value": dict(
field="event_value",
type="quantitative",
),
"source": dict(
field="source",
type="ordinal",
title="Source",
),
"full_date": dict(
field="full_date",
type="nominal",
title="Time and date",
),
}


def apply_chart_defaults(fn):
Expand Down
9 changes: 9 additions & 0 deletions flexmeasures/data/models/charts/test_chart_defaults.py
@@ -0,0 +1,9 @@
import altair as alt

from flexmeasures.data.models.charts.defaults import FIELD_DEFINITIONS


def test_default_encodings():
"""Check default encodings for valid vega-lite specifications."""
for field_name, field_definition in FIELD_DEFINITIONS.items():
assert alt.StringFieldDefWithCondition(**field_definition)
2 changes: 1 addition & 1 deletion flexmeasures/data/models/time_series.py
Expand Up @@ -97,7 +97,7 @@ def search_beliefs(
)
if as_json:
df = bdf.reset_index()
df["source"] = df["source"].apply(lambda x: x.name)
df["source"] = df["source"].astype(str)
return df.to_json(orient="records")
return bdf

Expand Down