Skip to content

Commit f815828

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

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

src/dvc_render/vega_templates.py

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ 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"),
164161
},
165162
}
166163

@@ -188,10 +185,7 @@ class BarHorizontalTemplate(Template):
188185
"title": Template.anchor("y_label"),
189186
},
190187
"yOffset": {"field": "rev"},
191-
"color": {
192-
"field": "rev",
193-
"type": "nominal",
194-
},
188+
"color": Template.anchor("color"),
195189
},
196190
}
197191

@@ -441,10 +435,8 @@ class ScatterTemplate(Template):
441435
"type": "quantitative",
442436
"title": Template.anchor("y_label"),
443437
},
444-
"color": {
445-
"field": "rev",
446-
"type": "nominal",
447-
},
438+
"color": Template.anchor("color"),
439+
"shape": Template.anchor("shape"),
448440
},
449441
}
450442

@@ -472,10 +464,8 @@ class ScatterJitterTemplate(Template):
472464
"field": Template.anchor("y"),
473465
"title": Template.anchor("y_label"),
474466
},
475-
"color": {
476-
"field": "rev",
477-
"type": "nominal",
478-
},
467+
"color": Template.anchor("color"),
468+
"shape": Template.anchor("shape"),
479469
"xOffset": {"field": "randomX", "type": "quantitative"},
480470
"yOffset": {"field": "randomY", "type": "quantitative"},
481471
},
@@ -502,15 +492,26 @@ class SmoothLinearTemplate(Template):
502492
},
503493
},
504494
],
495+
"encoding": {
496+
"x": {
497+
"field": Template.anchor("x"),
498+
"type": "quantitative",
499+
"title": Template.anchor("x_label"),
500+
},
501+
"color": Template.anchor("color"),
502+
"strokeDash": Template.anchor("stroke_dash"),
503+
"shape": Template.anchor("shape"),
504+
},
505505
"layer": [
506506
{
507-
"mark": "line",
508-
"encoding": {
509-
"x": {
510-
"field": Template.anchor("x"),
511-
"type": "quantitative",
512-
"title": Template.anchor("x_label"),
507+
"layer": [
508+
{"mark": "line"},
509+
{
510+
"transform": [{"filter": {"param": "hover", "empty": False}}],
511+
"mark": "point",
513512
},
513+
],
514+
"encoding": {
514515
"y": {
515516
"field": Template.anchor("y"),
516517
"type": "quantitative",
@@ -521,18 +522,6 @@ class SmoothLinearTemplate(Template):
521522
"field": "rev",
522523
"type": "nominal",
523524
},
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-
],
536525
},
537526
"transform": [
538527
{
@@ -558,26 +547,10 @@ class SmoothLinearTemplate(Template):
558547
"scale": {"zero": False},
559548
},
560549
"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-
],
573550
},
574551
},
575552
{
576-
"mark": {
577-
"type": "circle",
578-
"size": 10,
579-
"tooltip": {"content": "encoding"},
580-
},
553+
"mark": {"type": "circle", "size": 10},
581554
"encoding": {
582555
"x": {
583556
"aggregate": "max",
@@ -595,6 +568,38 @@ class SmoothLinearTemplate(Template):
595568
"color": {"field": "rev", "type": "nominal"},
596569
},
597570
},
571+
{
572+
"transform": [
573+
{
574+
"pivot": Template.anchor("group_by"),
575+
"value": Template.anchor("y"),
576+
"groupby": [Template.anchor("x")],
577+
}
578+
],
579+
"mark": {
580+
"type": "rule",
581+
"tooltip": {"content": "data"},
582+
"stroke": "grey",
583+
},
584+
"encoding": {
585+
"opacity": {
586+
"condition": {"value": 0.3, "param": "hover", "empty": False},
587+
"value": 0,
588+
}
589+
},
590+
"params": [
591+
{
592+
"name": "hover",
593+
"select": {
594+
"type": "point",
595+
"fields": [Template.anchor("x")],
596+
"nearest": True,
597+
"on": "mouseover",
598+
"clear": "mouseout",
599+
},
600+
}
601+
],
602+
},
598603
],
599604
}
600605

@@ -628,10 +633,9 @@ class SimpleLinearTemplate(Template):
628633
"title": Template.anchor("y_label"),
629634
"scale": {"zero": False},
630635
},
631-
"color": {
632-
"field": "rev",
633-
"type": "nominal",
634-
},
636+
"color": Template.anchor("color"),
637+
"strokeDash": Template.anchor("stroke_dash"),
638+
"shape": Template.anchor("shape"),
635639
},
636640
}
637641

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)