Skip to content

Commit

Permalink
Merge pull request #2515 from jakevdp/datum-chart
Browse files Browse the repository at this point in the history
Inject empty data at top level for easier datum encodings
  • Loading branch information
jakevdp authored Nov 12, 2021
2 parents 3d0b92b + cff40e3 commit ea35b61
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
7 changes: 3 additions & 4 deletions altair/examples/line_chart_with_datum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
from vega_datasets import data

source = data.stocks()
no_source = alt.Data(values=[{}])

lines = (
alt.Chart(source)
.mark_line()
.encode(x=alt.X("date"), y=alt.Y("price"), color="symbol")
.encode(x="date", y="price", color="symbol")
)

xrule = (
alt.Chart(no_source)
alt.Chart()
.mark_rule(color="cyan", strokeWidth=2)
.encode(x=alt.datum(alt.DateTime(year=2006, month="November")))
)

yrule = (
alt.Chart(no_source).mark_rule(strokeDash=[12, 6], size=2).encode(y=alt.datum(350))
alt.Chart().mark_rule(strokeDash=[12, 6], size=2).encode(y=alt.datum(350))
)


Expand Down
4 changes: 1 addition & 3 deletions altair/examples/pacman_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import numpy as np
import altair as alt

no_source = alt.Data(values=[{}])

alt.Chart(no_source).mark_arc(color="gold").encode(
alt.Chart().mark_arc(color="gold").encode(
theta=alt.datum((5 / 8) * np.pi, scale=None),
theta2=alt.datum((19 / 8) * np.pi),
radius=alt.datum(100, scale=None),
Expand Down
15 changes: 14 additions & 1 deletion altair/vegalite/v4/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def _dataset_name(values):
"""
if isinstance(values, core.InlineDataset):
values = values.to_dict()
if values == [{}]:
return "empty"
values_json = json.dumps(values, sort_keys=True)
hsh = hashlib.md5(values_json.encode()).hexdigest()
return "data-" + hsh
Expand Down Expand Up @@ -109,7 +111,7 @@ def _prepare_data(data, context=None):
@utils.use_signature(core.LookupData)
class LookupData(core.LookupData):
def to_dict(self, *args, **kwargs):
"""Convert the chart to a dictionary suitable for JSON export"""
"""Convert the chart to a dictionary suitable for JSON export."""
copy = self.copy(deep=False)
copy.data = _prepare_data(copy.data, kwargs.get("context"))
return super(LookupData, copy).to_dict(*args, **kwargs)
Expand Down Expand Up @@ -2006,6 +2008,17 @@ def from_dict(cls, dct, validate=True):
# As a last resort, try using the Root vegalite object
return core.Root.from_dict(dct, validate)

def to_dict(self, *args, **kwargs):
"""Convert the chart to a dictionary suitable for JSON export."""
context = kwargs.get("context", {})
if self.data is Undefined and "data" not in context:
# No data specified here or in parent: inject empty data
# for easier specification of datum encodings.
copy = self.copy(deep=False)
copy.data = core.InlineData(values=[{}])
return super(Chart, copy).to_dict(*args, **kwargs)
return super().to_dict(*args, **kwargs)

def add_selection(self, *selections):
"""Add one or more selections to the chart."""
if not selections:
Expand Down

0 comments on commit ea35b61

Please sign in to comment.