Skip to content

Commit

Permalink
Update templates with new anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Sep 13, 2023
1 parent cad3a3f commit f815828
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
114 changes: 59 additions & 55 deletions src/dvc_render/vega_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ class BarHorizontalSortedTemplate(Template):
"sort": "-x",
},
"yOffset": {"field": "rev"},
"color": {
"field": "rev",
"type": "nominal",
},
"color": Template.anchor("color"),
},
}

Expand Down Expand Up @@ -188,10 +185,7 @@ class BarHorizontalTemplate(Template):
"title": Template.anchor("y_label"),
},
"yOffset": {"field": "rev"},
"color": {
"field": "rev",
"type": "nominal",
},
"color": Template.anchor("color"),
},
}

Expand Down Expand Up @@ -441,10 +435,8 @@ class ScatterTemplate(Template):
"type": "quantitative",
"title": Template.anchor("y_label"),
},
"color": {
"field": "rev",
"type": "nominal",
},
"color": Template.anchor("color"),
"shape": Template.anchor("shape"),
},
}

Expand Down Expand Up @@ -472,10 +464,8 @@ class ScatterJitterTemplate(Template):
"field": Template.anchor("y"),
"title": Template.anchor("y_label"),
},
"color": {
"field": "rev",
"type": "nominal",
},
"color": Template.anchor("color"),
"shape": Template.anchor("shape"),
"xOffset": {"field": "randomX", "type": "quantitative"},
"yOffset": {"field": "randomY", "type": "quantitative"},
},
Expand All @@ -502,15 +492,26 @@ class SmoothLinearTemplate(Template):
},
},
],
"encoding": {
"x": {
"field": Template.anchor("x"),
"type": "quantitative",
"title": Template.anchor("x_label"),
},
"color": Template.anchor("color"),
"strokeDash": Template.anchor("stroke_dash"),
"shape": Template.anchor("shape"),
},
"layer": [
{
"mark": "line",
"encoding": {
"x": {
"field": Template.anchor("x"),
"type": "quantitative",
"title": Template.anchor("x_label"),
"layer": [
{"mark": "line"},
{
"transform": [{"filter": {"param": "hover", "empty": False}}],
"mark": "point",
},
],
"encoding": {
"y": {
"field": Template.anchor("y"),
"type": "quantitative",
Expand All @@ -521,18 +522,6 @@ class SmoothLinearTemplate(Template):
"field": "rev",
"type": "nominal",
},
"tooltip": [
{
"field": Template.anchor("x"),
"title": Template.anchor("x_label"),
"type": "quantitative",
},
{
"field": Template.anchor("y"),
"title": Template.anchor("y_label"),
"type": "quantitative",
},
],
},
"transform": [
{
Expand All @@ -558,26 +547,10 @@ class SmoothLinearTemplate(Template):
"scale": {"zero": False},
},
"color": {"field": "rev", "type": "nominal"},
"tooltip": [
{
"field": Template.anchor("x"),
"title": Template.anchor("x_label"),
"type": "quantitative",
},
{
"field": Template.anchor("y"),
"title": Template.anchor("y_label"),
"type": "quantitative",
},
],
},
},
{
"mark": {
"type": "circle",
"size": 10,
"tooltip": {"content": "encoding"},
},
"mark": {"type": "circle", "size": 10},
"encoding": {
"x": {
"aggregate": "max",
Expand All @@ -595,6 +568,38 @@ class SmoothLinearTemplate(Template):
"color": {"field": "rev", "type": "nominal"},
},
},
{
"transform": [
{
"pivot": Template.anchor("group_by"),
"value": Template.anchor("y"),
"groupby": [Template.anchor("x")],
}
],
"mark": {
"type": "rule",
"tooltip": {"content": "data"},
"stroke": "grey",
},
"encoding": {
"opacity": {
"condition": {"value": 0.3, "param": "hover", "empty": False},
"value": 0,
}
},
"params": [
{
"name": "hover",
"select": {
"type": "point",
"fields": [Template.anchor("x")],
"nearest": True,
"on": "mouseover",
"clear": "mouseout",
},
}
],
},
],
}

Expand Down Expand Up @@ -628,10 +633,9 @@ class SimpleLinearTemplate(Template):
"title": Template.anchor("y_label"),
"scale": {"zero": False},
},
"color": {
"field": "rev",
"type": "nominal",
},
"color": Template.anchor("color"),
"strokeDash": Template.anchor("stroke_dash"),
"shape": Template.anchor("shape"),
},
}

Expand Down
10 changes: 3 additions & 7 deletions tests/test_vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ def test_default_template_mark():

plot_content = VegaRenderer(datapoints, "foo").get_filled_template(as_string=False)

assert plot_content["layer"][0]["mark"] == "line"
assert plot_content["layer"][0]["layer"][0]["mark"] == "line"

assert plot_content["layer"][1]["mark"] == {"type": "line", "opacity": 0.2}

assert plot_content["layer"][2]["mark"] == {
"type": "circle",
"size": 10,
"tooltip": {"content": "encoding"},
}
assert plot_content["layer"][2]["mark"] == {"type": "circle", "size": 10}


def test_choose_axes():
Expand All @@ -78,7 +74,7 @@ def test_choose_axes():
"second_val": 300,
},
]
assert plot_content["layer"][0]["encoding"]["x"]["field"] == "first_val"
assert plot_content["encoding"]["x"]["field"] == "first_val"
assert plot_content["layer"][0]["encoding"]["y"]["field"] == "second_val"


Expand Down

0 comments on commit f815828

Please sign in to comment.