THINK FIRST·CODE LATER

← All labs

An expressiveness checker for chart specifications

Problem

Read one Vega-Lite specification (JSON) from standard input. For each encoding channel, in alphabetical order, print x: t (quantitative) - magnitude rank 1 (the field — or count for a count aggregate — and type; the rank suffix only for quantitative fields on x/y (rank 1), size (3), opacity or color (4)).

Then report issues in the same channel order:

  • E1 nominal field on size, opacity or strokeWidth: E1 nominal 'd' on size suggests an order that does not exist;
  • E2 quantitative field on shape: E2 quantitative 'p' on shape cannot show magnitude;
  • E3 ordinal or quantitative field on color with a categorical scheme (category10, category20, tableau10, tableau20, set1, set2, set3, dark2, accent, paired): E3 ordinal 'l' with categorical scheme 'category10' hides the order;
  • E4 nominal field on color with more than 9 distinct values in data.values: E4 12 categories on color hue (more than 9 are hard to tell apart).

Finish with 3 expressiveness issue(s) or expressive: every attribute uses a channel that matches its type.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v5-4-data-1.jsonl

{"mark": "point", "data": {"values": [{"d": "Lucheng", "p": 40, "t": 12, "l": "good"}]},
 "encoding": {"x": {"field": "t", "type": "quantitative"}, "y": {"field": "p", "type": "quantitative"},
  "size": {"field": "d", "type": "nominal"}, "shape": {"field": "p", "type": "quantitative"},
  "color": {"field": "l", "type": "ordinal", "scale": {"scheme": "category10"}}}}
What your program must print for v5-4-data-1.jsonl
color: l (ordinal)
shape: p (quantitative)
size: d (nominal)
x: t (quantitative) - magnitude rank 1
y: p (quantitative) - magnitude rank 1
E3 ordinal 'l' with categorical scheme 'category10' hides the order
E2 quantitative 'p' on shape cannot show magnitude
E1 nominal 'd' on size suggests an order that does not exist
3 expressiveness issue(s)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v5-4-data-1.jsonl

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v5-4-data-1.jsonl | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v5-4-data-1.jsonl", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.