diff --git a/src/dvc_render/vega_templates.py b/src/dvc_render/vega_templates.py index 82fb527..484f77c 100644 --- a/src/dvc_render/vega_templates.py +++ b/src/dvc_render/vega_templates.py @@ -157,10 +157,7 @@ class BarHorizontalSortedTemplate(Template): "sort": "-x", }, "yOffset": {"field": "rev"}, - "color": { - "field": "rev", - "type": "nominal", - }, + "color": Template.anchor("color"), }, } @@ -188,10 +185,7 @@ class BarHorizontalTemplate(Template): "title": Template.anchor("y_label"), }, "yOffset": {"field": "rev"}, - "color": { - "field": "rev", - "type": "nominal", - }, + "color": Template.anchor("color"), }, } @@ -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"), }, } @@ -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"}, }, @@ -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", @@ -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": [ { @@ -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", @@ -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", + }, + } + ], + }, ], } @@ -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"), }, } diff --git a/tests/test_vega.py b/tests/test_vega.py index f545665..65c2ff0 100644 --- a/tests/test_vega.py +++ b/tests/test_vega.py @@ -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(): @@ -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"