Элементы circle и square относятся к
базовым графическим marks в экосистеме Vega и Vega-Lite. Они
используются для отображения точек, узлов, координатных значений,
кластеров, плотностей распределения и интерактивных элементов
визуализации.
В основе обоих примитивов лежит концепция точечного отображения данных. Различие заключается исключительно в форме:
circle — окружность;square — квадрат.Оба marks часто применяются:
circle в Vega-LiteМинимальная спецификация:
{
"mark": "circle"
}
Полноценный пример:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"x": 10, "y": 28},
{"x": 20, "y": 55},
{"x": 30, "y": 43},
{"x": 40, "y": 91}
]
},
"mark": "circle",
"encoding": {
"x": {"field": "x", "type": "quantitative"},
"y": {"field": "y", "type": "quantitative"}
}
}
Размер задаётся через свойство size.
{
"mark": {
"type": "circle",
"size": 300
}
}
Размер задаётся в квадратных пикселях, а не в радиусе.
{
"encoding": {
"size": {
"field": "population",
"type": "quantitative"
}
}
}
В таком случае формируется bubble chart.
{
"mark": {
"type": "circle",
"color": "steelblue"
}
}
{
"encoding": {
"color": {
"field": "category",
"type": "nominal"
}
}
}
Свойство opacity особенно важно при большом количестве
точек.
{
"mark": {
"type": "circle",
"opacity": 0.4
}
}
Полезно для борьбы с overplotting.
{
"mark": {
"type": "circle",
"stroke": "black",
"strokeWidth": 2
}
}
circle{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"url": "data/cars.json"
},
"mark": "circle",
"encoding": {
"x": {
"field": "Horsepower",
"type": "quantitative"
},
"y": {
"field": "Miles_per_Gallon",
"type": "quantitative"
},
"color": {
"field": "Origin",
"type": "nominal"
}
}
}
circle особенно хорошо подходит для bubble chart.
{
"mark": "circle",
"encoding": {
"x": {"field": "income", "type": "quantitative"},
"y": {"field": "life_expectancy", "type": "quantitative"},
"size": {
"field": "population",
"type": "quantitative"
},
"color": {
"field": "continent",
"type": "nominal"
}
}
}
{
"selection": {
"highlight": {
"type": "single",
"on": "mouseover",
"empty": "none"
}
},
"mark": "circle",
"encoding": {
"opacity": {
"condition": {
"selection": "highlight",
"value": 1
},
"value": 0.3
}
}
}
{
"encoding": {
"tooltip": [
{"field": "name"},
{"field": "value"}
]
}
}
square в Vega-Lite{
"mark": "square"
}
square от
circle| Свойство | circle |
square |
|---|---|---|
| Форма | Круг | Квадрат |
| Восприятие плотности | Мягкое | Более структурированное |
| Heatmap | Реже | Очень часто |
| Pixel-style визуализация | Нет | Да |
| Табличные данные | Умеренно | Отлично |
| Геометрическая строгость | Ниже | Выше |
{
"mark": {
"type": "square",
"size": 400
}
}
squareОдно из главных применений.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"x": 1, "y": 1, "value": 10},
{"x": 2, "y": 1, "value": 30},
{"x": 1, "y": 2, "value": 50},
{"x": 2, "y": 2, "value": 90}
]
},
"mark": "square",
"encoding": {
"x": {"field": "x", "type": "ordinal"},
"y": {"field": "y", "type": "ordinal"},
"color": {
"field": "value",
"type": "quantitative"
}
}
}
square подходит для визуализации матриц и пиксельных
структур.
{
"mark": {
"type": "square",
"size": 100
}
}
{
"config": {
"circle": {
"opacity": 0.7,
"color": "tomato"
}
}
}
square{
"config": {
"square": {
"filled": true,
"stroke": "black"
}
}
}
В низкоуровневом Vega синтаксис отличается.
{
"marks": [
{
"type": "symbol",
"encode": {
"enter": {
"x": {"value": 100},
"y": {"value": 100},
"size": {"value": 400},
"shape": {"value": "circle"},
"fill": {"value": "steelblue"}
}
}
}
]
}
{
"marks": [
{
"type": "symbol",
"encode": {
"enter": {
"x": {"value": 150},
"y": {"value": 150},
"shape": {"value": "square"},
"size": {"value": 600},
"fill": {"value": "orange"}
}
}
}
]
}
В Vega используется mark symbol, а форма задаётся через
shape.
Поддерживаемые значения:
circlesquarecrossdiamondtriangle-uptriangle-downtriangle-righttriangle-left{
"encode": {
"update": {
"shape": {
"field": "shapeType"
}
}
}
}
{
"encoding": {
"size": {
"field": "amount",
"type": "quantitative",
"scale": {
"range": [10, 1000]
}
}
}
}
{
"encoding": {
"size": {
"field": "population",
"legend": {
"title": "Population"
}
}
}
}
circle и square часто используются
вместе.
{
"layer": [
{
"mark": "square"
},
{
"mark": {
"type": "circle",
"color": "white",
"size": 100
}
}
]
}
{
"encoding": {
"color": {
"condition": {
"test": "datum.value > 50",
"value": "red"
},
"value": "green"
}
}
}
{
"signals": [
{
"name": "hover",
"value": null,
"on": [
{
"events": "symbol:mouseover",
"update": "datum"
},
{
"events": "symbol:mouseout",
"update": "null"
}
]
}
]
}
circleПодходит для:
squareПодходит для:
При отображении тысяч объектов полезны:
{
"mark": {
"type": "circle",
"opacity": 0.2
}
}
{
"mark": {
"type": "square",
"size": 20
}
}
{
"mark": {
"type": "circle",
"filled": false,
"stroke": "black"
}
}
{
"mark": {
"type": "square",
"filled": false,
"strokeWidth": 2
}
}
{
"transform": [
{
"aggregate": [
{
"op": "mean",
"field": "value",
"as": "avg_value"
}
],
"groupby": ["category"]
}
]
}
{
"transform": [
{
"calculate": "datum.x + random() * 0.5",
"as": "jx"
}
]
}
{
"mark": {
"type": "circle",
"opacity": 0.1
}
}
{
"encoding": {
"x": {"field": "col"},
"y": {"field": "row"}
}
}
Важно учитывать визуальную интерпретацию:
circle обычно применяется:
square используется:
{
"config": {
"style": {
"cell": {
"stroke": "transparent"
}
}
}
}
{
"size": 10000
}
Может приводить к перекрытию визуализации.
При большом количестве точек:
{
"opacity": 1
}
делает график нечитаемым.
{
"type": "nominal"
}
для непрерывных значений ухудшает визуальное восприятие.
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{"x": "A", "y": "Q1", "v": 28},
{"x": "B", "y": "Q1", "v": 55},
{"x": "A", "y": "Q2", "v": 43},
{"x": "B", "y": "Q2", "v": 91}
]
},
"layer": [
{
"mark": "square",
"encoding": {
"x": {"field": "x", "type": "ordinal"},
"y": {"field": "y", "type": "ordinal"},
"color": {
"field": "v",
"type": "quantitative"
}
}
},
{
"mark": {
"type": "circle",
"color": "white"
},
"encoding": {
"x": {"field": "x", "type": "ordinal"},
"y": {"field": "y", "type": "ordinal"},
"size": {
"field": "v",
"type": "quantitative"
}
}
}
]
}