Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Vega's radial text positioning #58

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
191 changes: 191 additions & 0 deletions avenger-vega-test-data/vega-scenegraphs/text/arc_radial.sg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
{
"width": 211,
"height": 221,
"origin": [
6,
6
],
"scenegraph": {
"clip": false,
"interactive": true,
"items": [
{
"items": [
{
"clip": false,
"interactive": true,
"items": [
{
"stroke": "#fff",
"innerRadius": 20,
"fill": "#f58518",
"description": "data: 12",
"x": 100,
"y": 100,
"outerRadius": 58.43075691322092,
"startAngle": 0.7113039970391984,
"endAngle": 0.23710133234639946
},
{
"stroke": "#fff",
"innerRadius": 20,
"fill": "#72b7b2",
"description": "data: 23",
"x": 100,
"y": 100,
"outerRadius": 73.20497374089409,
"startAngle": 2.371013323463995,
"endAngle": 1.4621248828027968
},
{
"stroke": "#fff",
"innerRadius": 20,
"fill": "#54a24b",
"description": "data: 47",
"x": 100,
"y": 100,
"outerRadius": 96.05665904189708,
"startAngle": 4.22830709351079,
"endAngle": 2.371013323463995
},
{
"stroke": "#fff",
"innerRadius": 20,
"fill": "#4c78a8",
"description": "data: 6",
"x": 100,
"y": 100,
"outerRadius": 47.1746488194703,
"startAngle": 0.23710133234639946,
"endAngle": 0
},
{
"stroke": "#fff",
"innerRadius": 20,
"fill": "#eeca3b",
"description": "data: 52",
"x": 100,
"y": 100,
"outerRadius": 100,
"startAngle": 6.283185307179586,
"endAngle": 4.22830709351079
},
{
"stroke": "#fff",
"innerRadius": 20,
"fill": "#e45756",
"description": "data: 19",
"x": 100,
"y": 100,
"outerRadius": 68.35764198615911,
"startAngle": 1.4621248828027968,
"endAngle": 0.7113039970391984
}
],
"marktype": "arc",
"name": "layer_0_marks",
"role": "mark",
"zindex": 0
},
{
"clip": false,
"interactive": true,
"items": [
{
"font": "sans-serif",
"fontSize": 11,
"fill": "#f58518",
"description": "data: 12",
"x": 100,
"y": 100,
"text": "12",
"align": "center",
"baseline": "middle",
"radius": 68.43075691322092,
"theta": 0.4742026646927989
},
{
"font": "sans-serif",
"fontSize": 11,
"fill": "#72b7b2",
"description": "data: 23",
"x": 100,
"y": 100,
"text": "23",
"align": "center",
"baseline": "middle",
"radius": 83.20497374089409,
"theta": 1.9165691031333958
},
{
"font": "sans-serif",
"fontSize": 11,
"fill": "#54a24b",
"description": "data: 47",
"x": 100,
"y": 100,
"text": "47",
"align": "center",
"baseline": "middle",
"radius": 106.05665904189708,
"theta": 3.299660208487393
},
{
"font": "sans-serif",
"fontSize": 11,
"fill": "#4c78a8",
"description": "data: 6",
"x": 100,
"y": 100,
"text": "6",
"align": "center",
"baseline": "middle",
"radius": 57.1746488194703,
"theta": 0.11855066617319973
},
{
"font": "sans-serif",
"fontSize": 11,
"fill": "#eeca3b",
"description": "data: 52",
"x": 100,
"y": 100,
"text": "52",
"align": "center",
"baseline": "middle",
"radius": 110,
"theta": 5.255746200345189
},
{
"font": "sans-serif",
"fontSize": 11,
"fill": "#e45756",
"description": "data: 19",
"x": 100,
"y": 100,
"text": "19",
"align": "center",
"baseline": "middle",
"radius": 78.35764198615911,
"theta": 1.0867144399209976
}
],
"marktype": "text",
"name": "layer_1_marks",
"role": "mark",
"zindex": 0
}
],
"fill": "transparent",
"x": 0,
"y": 0,
"width": 200,
"height": 200
}
],
"marktype": "group",
"name": "root",
"role": "frame",
"zindex": 0
}
}
Binary file modified avenger-vega-test-data/vega-scenegraphs/vl-convert/geoScale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 137 additions & 0 deletions avenger-vega-test-data/vega-specs/text/arc_radial.vg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A simple radial chart with embedded data.",
"background": "white",
"padding": 5,
"width": 200,
"height": 200,
"style": "view",
"data": [
{"name": "source_0", "values": [12, 23, 47, 6, 52, 19]},
{
"name": "data_1",
"source": "source_0",
"transform": [
{
"type": "stack",
"groupby": [],
"field": "data",
"sort": {"field": ["data"], "order": ["ascending"]},
"as": ["data_start", "data_end"],
"offset": "zero"
},
{
"type": "filter",
"expr": "isValid(datum[\"data\"]) && isFinite(+datum[\"data\"])"
}
]
},
{
"name": "data_2",
"source": "source_0",
"transform": [
{
"type": "stack",
"groupby": [],
"field": "data",
"sort": {
"field": ["data", "data"],
"order": ["ascending", "ascending"]
},
"as": ["data_start", "data_end"],
"offset": "zero"
},
{
"type": "filter",
"expr": "isValid(datum[\"data\"]) && isFinite(+datum[\"data\"])"
}
]
}
],
"marks": [
{
"name": "layer_0_marks",
"type": "arc",
"style": ["arc"],
"from": {"data": "data_1"},
"encode": {
"update": {
"stroke": {"value": "#fff"},
"innerRadius": {"value": 20},
"fill": {"scale": "color", "field": "data"},
"description": {
"signal": "\"data: \" + (format(datum[\"data\"], \"\"))"
},
"x": {"signal": "width", "mult": 0.5},
"y": {"signal": "height", "mult": 0.5},
"outerRadius": {"scale": "radius", "field": "data"},
"startAngle": {"scale": "theta", "field": "data_end"},
"endAngle": {"scale": "theta", "field": "data_start"}
}
}
},
{
"name": "layer_1_marks",
"type": "text",
"style": ["text"],
"from": {"data": "data_2"},
"encode": {
"update": {
"fill": {"scale": "color", "field": "data"},
"description": {
"signal": "\"data: \" + (format(datum[\"data\"], \"\"))"
},
"x": {"signal": "width", "mult": 0.5},
"y": {"signal": "height", "mult": 0.5},
"text": {"signal": "format(datum[\"data\"], \"\")"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"radius": {"scale": "radius", "field": "data", "offset": 10},
"theta": {
"signal": "scale(\"theta\", 0.5 * datum[\"data_start\"] + 0.5 * datum[\"data_end\"])"
}
}
}
}
],
"scales": [
{
"name": "theta",
"type": "linear",
"domain": {
"fields": [
{"data": "data_1", "field": "data_start"},
{"data": "data_1", "field": "data_end"},
{"data": "data_2", "field": "data_start"},
{"data": "data_2", "field": "data_end"}
]
},
"range": [0, 6.283185307179586],
"zero": true
},
{
"name": "radius",
"type": "sqrt",
"domain": {
"fields": [
{"data": "data_1", "field": "data"},
{"data": "data_2", "field": "data"}
]
},
"range": [20, {"signal": "min(width,height)/2"}],
"zero": true
},
{
"name": "color",
"type": "ordinal",
"domain": {
"fields": [
{"data": "data_1", "field": "data"},
{"data": "data_2", "field": "data"}
],
"sort": true
},
"range": "category"
}
]
}
4 changes: 1 addition & 3 deletions avenger-vega/src/marks/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ impl VegaMarkContainer<VegaArcItem> {
if let Some(v) = &item.stroke {
let opacity = item.stroke_opacity.unwrap_or(1.0) * item.opacity.unwrap_or(1.0);
stroke.push(v.to_color_or_grad(opacity, &mut gradients)?);
}
if let Some(s) = item.stroke_width {
stroke_width.push(s);
stroke_width.push(item.stroke_width.unwrap_or(1.0));
}
if let Some(v) = item.zindex {
zindex.push(v);
Expand Down
16 changes: 14 additions & 2 deletions avenger-vega/src/marks/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use avenger::marks::text::{
};
use avenger::marks::value::EncodingValue;
use serde::{Deserialize, Serialize};
use std::f32::consts::PI;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -16,6 +17,8 @@ pub struct VegaTextItem {
pub text: Option<serde_json::Value>,

// Optional
pub radius: Option<f32>,
pub theta: Option<f32>,
pub align: Option<TextAlignSpec>,
pub angle: Option<f32>,
pub baseline: Option<TextBaselineSpec>,
Expand Down Expand Up @@ -77,8 +80,17 @@ impl VegaMarkContainer<VegaTextItem> {
color.push([c.r as f32, c.g as f32, c.b as f32, opacity])
}

x.push(item.x.unwrap_or(0.0) + item.dx.unwrap_or(0.0));
y.push(item.y.unwrap_or(0.0) + item.dy.unwrap_or(0.0));
// Compute x and y
let mut item_x = item.x.unwrap_or(0.0);
let mut item_y = item.y.unwrap_or(0.0);
if let (Some(radius), Some(theta)) = (item.radius, item.theta) {
item_x += radius * f32::cos(theta - PI / 2.0);
item_y += radius * f32::sin(theta - PI / 2.0);
}
item_x += item.dx.unwrap_or(0.0);
item_y += item.dy.unwrap_or(0.0);
x.push(item_x);
y.push(item_y);
text.push(match item.text.clone() {
Some(serde_json::Value::String(s)) => s,
Some(serde_json::Value::Null) | None => "".to_string(),
Expand Down
1 change: 1 addition & 0 deletions avenger-wgpu/tests/test_image_baselines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ mod test_image_baselines {
case("text", "text_rotation", 0.015),
case("text", "letter_scatter", 0.012),
case("text", "lasagna_plot", 0.01),
case("text", "arc_radial", 0.01),

// vl-convert doesn't support emoji at all
case("text", "emoji", 2.0),
Expand Down
Loading