Skip to content

Commit df9ef37

Browse files
committed
Update templates with new anchors
1 parent cad3a3f commit df9ef37

File tree

2 files changed

+64
-62
lines changed

2 files changed

+64
-62
lines changed

src/dvc_render/vega_templates.py

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ class BarHorizontalSortedTemplate(Template):
157157
"sort": "-x",
158158
},
159159
"yOffset": {"field": "rev"},
160-
"color": {
161-
"field": "rev",
162-
"type": "nominal",
163-
},
160+
"color": Template.anchor("color"),
161+
"row": Template.anchor("row"),
164162
},
165163
}
166164

@@ -188,10 +186,8 @@ class BarHorizontalTemplate(Template):
188186
"title": Template.anchor("y_label"),
189187
},
190188
"yOffset": {"field": "rev"},
191-
"color": {
192-
"field": "rev",
193-
"type": "nominal",
194-
},
189+
"color": Template.anchor("color"),
190+
"row": Template.anchor("row"),
195191
},
196192
}
197193

@@ -441,10 +437,8 @@ class ScatterTemplate(Template):
441437
"type": "quantitative",
442438
"title": Template.anchor("y_label"),
443439
},
444-
"color": {
445-
"field": "rev",
446-
"type": "nominal",
447-
},
440+
"color": Template.anchor("color"),
441+
"shape": Template.anchor("shape"),
448442
},
449443
}
450444

@@ -472,10 +466,8 @@ class ScatterJitterTemplate(Template):
472466
"field": Template.anchor("y"),
473467
"title": Template.anchor("y_label"),
474468
},
475-
"color": {
476-
"field": "rev",
477-
"type": "nominal",
478-
},
469+
"color": Template.anchor("color"),
470+
"shape": Template.anchor("shape"),
479471
"xOffset": {"field": "randomX", "type": "quantitative"},
480472
"yOffset": {"field": "randomY", "type": "quantitative"},
481473
},
@@ -502,15 +494,26 @@ class SmoothLinearTemplate(Template):
502494
},
503495
},
504496
],
497+
"encoding": {
498+
"x": {
499+
"field": Template.anchor("x"),
500+
"type": "quantitative",
501+
"title": Template.anchor("x_label"),
502+
},
503+
"color": Template.anchor("color"),
504+
"strokeDash": Template.anchor("stroke_dash"),
505+
"shape": Template.anchor("shape"),
506+
},
505507
"layer": [
506508
{
507-
"mark": "line",
508-
"encoding": {
509-
"x": {
510-
"field": Template.anchor("x"),
511-
"type": "quantitative",
512-
"title": Template.anchor("x_label"),
509+
"layer": [
510+
{"mark": "line"},
511+
{
512+
"transform": [{"filter": {"param": "hover", "empty": False}}],
513+
"mark": "point",
513514
},
515+
],
516+
"encoding": {
514517
"y": {
515518
"field": Template.anchor("y"),
516519
"type": "quantitative",
@@ -521,18 +524,6 @@ class SmoothLinearTemplate(Template):
521524
"field": "rev",
522525
"type": "nominal",
523526
},
524-
"tooltip": [
525-
{
526-
"field": Template.anchor("x"),
527-
"title": Template.anchor("x_label"),
528-
"type": "quantitative",
529-
},
530-
{
531-
"field": Template.anchor("y"),
532-
"title": Template.anchor("y_label"),
533-
"type": "quantitative",
534-
},
535-
],
536527
},
537528
"transform": [
538529
{
@@ -558,26 +549,10 @@ class SmoothLinearTemplate(Template):
558549
"scale": {"zero": False},
559550
},
560551
"color": {"field": "rev", "type": "nominal"},
561-
"tooltip": [
562-
{
563-
"field": Template.anchor("x"),
564-
"title": Template.anchor("x_label"),
565-
"type": "quantitative",
566-
},
567-
{
568-
"field": Template.anchor("y"),
569-
"title": Template.anchor("y_label"),
570-
"type": "quantitative",
571-
},
572-
],
573552
},
574553
},
575554
{
576-
"mark": {
577-
"type": "circle",
578-
"size": 10,
579-
"tooltip": {"content": "encoding"},
580-
},
555+
"mark": {"type": "circle", "size": 10},
581556
"encoding": {
582557
"x": {
583558
"aggregate": "max",
@@ -595,6 +570,38 @@ class SmoothLinearTemplate(Template):
595570
"color": {"field": "rev", "type": "nominal"},
596571
},
597572
},
573+
{
574+
"transform": [
575+
{
576+
"pivot": Template.anchor("group_by"),
577+
"value": Template.anchor("y"),
578+
"groupby": [Template.anchor("x")],
579+
}
580+
],
581+
"mark": {
582+
"type": "rule",
583+
"tooltip": {"content": "data"},
584+
"stroke": "grey",
585+
},
586+
"encoding": {
587+
"opacity": {
588+
"condition": {"value": 0.3, "param": "hover", "empty": False},
589+
"value": 0,
590+
}
591+
},
592+
"params": [
593+
{
594+
"name": "hover",
595+
"select": {
596+
"type": "point",
597+
"fields": [Template.anchor("x")],
598+
"nearest": True,
599+
"on": "mouseover",
600+
"clear": "mouseout",
601+
},
602+
}
603+
],
604+
},
598605
],
599606
}
600607

@@ -628,10 +635,9 @@ class SimpleLinearTemplate(Template):
628635
"title": Template.anchor("y_label"),
629636
"scale": {"zero": False},
630637
},
631-
"color": {
632-
"field": "rev",
633-
"type": "nominal",
634-
},
638+
"color": Template.anchor("color"),
639+
"strokeDash": Template.anchor("stroke_dash"),
640+
"shape": Template.anchor("shape"),
635641
},
636642
}
637643

tests/test_vega.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,11 @@ def test_default_template_mark():
4444

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

47-
assert plot_content["layer"][0]["mark"] == "line"
47+
assert plot_content["layer"][0]["layer"][0]["mark"] == "line"
4848

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

51-
assert plot_content["layer"][2]["mark"] == {
52-
"type": "circle",
53-
"size": 10,
54-
"tooltip": {"content": "encoding"},
55-
}
51+
assert plot_content["layer"][2]["mark"] == {"type": "circle", "size": 10}
5652

5753

5854
def test_choose_axes():
@@ -78,7 +74,7 @@ def test_choose_axes():
7874
"second_val": 300,
7975
},
8076
]
81-
assert plot_content["layer"][0]["encoding"]["x"]["field"] == "first_val"
77+
assert plot_content["encoding"]["x"]["field"] == "first_val"
8278
assert plot_content["layer"][0]["encoding"]["y"]["field"] == "second_val"
8379

8480

0 commit comments

Comments
 (0)