diff --git a/src/dvc_render/vega_templates.py b/src/dvc_render/vega_templates.py index 78b91b2..d13e9b6 100644 --- a/src/dvc_render/vega_templates.py +++ b/src/dvc_render/vega_templates.py @@ -113,7 +113,11 @@ class BarHorizontalSortedTemplate(Template): "sort": "-x", }, "yOffset": {"field": "rev"}, - "color": {"field": "rev", "type": "nominal"}, + "color": { + "field": "rev", + "type": "nominal", + "legend": {"orient": "top", "direction": "vertical"}, + }, }, } @@ -141,7 +145,11 @@ class BarHorizontalTemplate(Template): "title": Template.anchor("y_label"), }, "yOffset": {"field": "rev"}, - "color": {"field": "rev", "type": "nominal"}, + "color": { + "field": "rev", + "type": "nominal", + "legend": {"orient": "top", "direction": "vertical"}, + }, }, } @@ -393,7 +401,11 @@ class ScatterTemplate(Template): "title": Template.anchor("y_label"), "scale": {"zero": False}, }, - "color": {"field": "rev", "type": "nominal"}, + "color": { + "field": "rev", + "type": "nominal", + "legend": {"orient": "top", "direction": "vertical"}, + }, }, "layer": [ {"mark": "point"}, @@ -473,25 +485,18 @@ class SmoothLinearTemplate(Template): "params": [ { "name": "smooth", - "value": 0.2, + "value": 0.001, "bind": { "input": "range", "min": 0.001, "max": 1, - "step": 0.01, + "step": 0.001, }, }, ], - "transform": [ - { - "loess": Template.anchor("y"), - "on": Template.anchor("x"), - "groupby": ["rev", "filename"], - "bandwidth": {"signal": "smooth"}, - } - ], "layer": [ { + "mark": "line", "encoding": { "x": { "field": Template.anchor("x"), @@ -510,23 +515,46 @@ class SmoothLinearTemplate(Template): "legend": {"orient": "top", "direction": "vertical"}, }, }, - "layer": [ + "transform": [ { - "mark": { - "type": "line", - "point": True, - "tooltip": {"content": "data"}, - } - } + "loess": Template.anchor("y"), + "on": Template.anchor("x"), + "groupby": ["rev", "filename", "field", "filename::field"], + "bandwidth": {"signal": "smooth"}, + }, ], - } + }, + { + "mark": { + "type": "point", + "tooltip": {"content": "data"}, + }, + "encoding": { + "x": { + "field": Template.anchor("x"), + "type": "quantitative", + "title": Template.anchor("x_label"), + }, + "y": { + "field": Template.anchor("y"), + "type": "quantitative", + "title": Template.anchor("y_label"), + "scale": {"zero": False}, + }, + "color": {"field": "rev", "type": "nominal"}, + }, + }, ], } -class LinearTemplate(Template): +class LinearTemplate(SmoothLinearTemplate): DEFAULT_NAME = "linear" + +class SimpleLinearTemplate(Template): + DEFAULT_NAME = "simple" + DEFAULT_CONTENT = { "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "data": {"values": Template.anchor("data")}, @@ -535,7 +563,6 @@ class LinearTemplate(Template): "height": 300, "mark": { "type": "line", - "point": True, "tooltip": {"content": "data"}, }, "encoding": { @@ -559,10 +586,6 @@ class LinearTemplate(Template): } -class SimpleLinearTemplate(LinearTemplate): - DEFAULT_NAME = "simple" - - TEMPLATES = [ SimpleLinearTemplate, LinearTemplate, diff --git a/tests/test_vega.py b/tests/test_vega.py index 8005dba..39b6d4d 100644 --- a/tests/test_vega.py +++ b/tests/test_vega.py @@ -45,9 +45,10 @@ def test_default_template_mark(): plot_content = json.loads(VegaRenderer(datapoints, "foo").partial_html()) - assert plot_content["mark"] == { - "type": "line", - "point": True, + assert plot_content["layer"][0]["mark"] == "line" + + assert plot_content["layer"][1]["mark"] == { + "type": "point", "tooltip": {"content": "data"}, } @@ -73,8 +74,8 @@ def test_choose_axes(): "second_val": 300, }, ] - assert plot_content["encoding"]["x"]["field"] == "first_val" - assert plot_content["encoding"]["y"]["field"] == "second_val" + assert plot_content["layer"][0]["encoding"]["x"]["field"] == "first_val" + assert plot_content["layer"][0]["encoding"]["y"]["field"] == "second_val" def test_confusion():