Skip to content

Commit 5145d0d

Browse files
committed
fix plot having multiple x fields
1 parent 63f2fc3 commit 5145d0d

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

dvc/render/converter/vega.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ def flat_datapoints(self, revision): # noqa: C901, PLR0912
204204
)
205205
else:
206206
x_file, x_field = xs[0]
207-
props_update["x"] = x_field
207+
208+
num_xs = len(xs)
209+
unify_x_fields = num_xs > 1 and len({x[1] for x in xs}) > 1
210+
props_update["x"] = "dvc_inferred_x_value" if unify_x_fields else x_field
208211

209212
ys = list(_get_ys(properties, file2datapoints))
210213

211-
num_xs = len(xs)
212214
num_ys = len(ys)
213215
if num_xs > 1 and num_xs != num_ys:
214216
raise DvcException(
@@ -264,8 +266,9 @@ def flat_datapoints(self, revision): # noqa: C901, PLR0912
264266
try:
265267
_update_from_field(
266268
datapoints,
267-
field=x_field,
269+
field="dvc_inferred_x_value" if unify_x_fields else x_field,
268270
source_datapoints=x_datapoints,
271+
source_field=x_field,
269272
)
270273
except IndexError:
271274
raise DvcException( # noqa: B904

tests/unit/render/test_vega_converter.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,107 @@ def test_finding_lists(dictionary, expected_result):
410410
},
411411
id="multi_file_y_same_prefix",
412412
),
413+
pytest.param(
414+
{
415+
"f": {"metric": [{"x1": 1, "v": 0.1}]},
416+
"f2": {"metric": [{"x2": 100, "v": 0.1}]},
417+
},
418+
{"y": {"f": ["v"], "f2": ["v"]}, "x": {"f": "x1", "f2": "x2"}},
419+
[
420+
{
421+
"x1": 1,
422+
"v": 0.1,
423+
"dvc_inferred_x_value": 1,
424+
REVISION: "r",
425+
FILENAME: "f",
426+
FIELD: "v",
427+
},
428+
{
429+
"x2": 100,
430+
"v": 0.1,
431+
"dvc_inferred_x_value": 100,
432+
REVISION: "r",
433+
FILENAME: "f2",
434+
FIELD: "v",
435+
},
436+
],
437+
{
438+
"anchors_y_definitions": [
439+
{FILENAME: "f", FIELD: "v"},
440+
{FILENAME: "f2", FIELD: "v"},
441+
],
442+
"x": "dvc_inferred_x_value",
443+
"y": "v",
444+
"x_label": "x",
445+
"y_label": "v",
446+
},
447+
id="multiple_x_fields",
448+
),
449+
pytest.param(
450+
{
451+
"f": {
452+
"metric": [
453+
{"v": 1, "v2": 0.1, "x1": 100},
454+
{"v": 2, "v2": 0.2, "x1": 1000},
455+
]
456+
},
457+
"f2": {"metric": [{"x2": -2}, {"x2": -4}]},
458+
},
459+
{"y": ["v", "v2"], "x": {"f": "x1", "f2": "x2"}},
460+
[
461+
{
462+
"dvc_inferred_x_value": 100,
463+
"dvc_inferred_y_value": 1,
464+
"v": 1,
465+
"v2": 0.1,
466+
"x1": 100,
467+
REVISION: "r",
468+
FILENAME: "f",
469+
FIELD: "v",
470+
},
471+
{
472+
"dvc_inferred_x_value": 1000,
473+
"dvc_inferred_y_value": 2,
474+
"v": 2,
475+
"v2": 0.2,
476+
"x1": 1000,
477+
REVISION: "r",
478+
FILENAME: "f",
479+
FIELD: "v",
480+
},
481+
{
482+
"dvc_inferred_x_value": -2,
483+
"dvc_inferred_y_value": 0.1,
484+
"v": 1,
485+
"v2": 0.1,
486+
"x1": 100,
487+
REVISION: "r",
488+
FILENAME: "f",
489+
FIELD: "v2",
490+
},
491+
{
492+
"dvc_inferred_x_value": -4,
493+
"dvc_inferred_y_value": 0.2,
494+
"v": 2,
495+
"v2": 0.2,
496+
"x1": 1000,
497+
REVISION: "r",
498+
FILENAME: "f",
499+
FIELD: "v2",
500+
},
501+
],
502+
{
503+
"anchors_y_definitions": [
504+
{FILENAME: "f", FIELD: "v"},
505+
{FILENAME: "f", FIELD: "v2"},
506+
],
507+
"x": "dvc_inferred_x_value",
508+
"y": "dvc_inferred_y_value",
509+
"x_label": "x",
510+
"y_label": "y",
511+
},
512+
id="y_list_x_dict",
513+
),
413514
],
414515
)
415516
def test_convert(

0 commit comments

Comments
 (0)