-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from jonmmease/jonmmease/trail_mark
Add trail mark
- Loading branch information
Showing
16 changed files
with
10,152 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4,598 changes: 4,598 additions & 0 deletions
4,598
sg2d-vega-test-data/vega-scenegraphs/trail/trail_stocks.sg.json
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5,158 changes: 5,158 additions & 0 deletions
5,158
sg2d-vega-test-data/vega-scenegraphs/trail/trail_stocks_opacity.sg.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{ | ||
"$schema": "https://vega.github.io/schema/vega/v5.json", | ||
"description": "Stock prices of 5 Tech Companies over Time.", | ||
"background": "white", | ||
"padding": 5, | ||
"width": 400, | ||
"height": 400, | ||
"style": "cell", | ||
"config": {"style": {"cell": {"stroke": "transparent"}}}, | ||
"data": [ | ||
{ | ||
"name": "source_0", | ||
"url": "data/stocks.csv", | ||
"format": {"type": "csv", "parse": {"date": "date"}, "delimiter": ","} | ||
} | ||
], | ||
"marks": [ | ||
{ | ||
"name": "pathgroup", | ||
"type": "group", | ||
"from": { | ||
"facet": { | ||
"name": "faceted_path_main", | ||
"data": "source_0", | ||
"groupby": ["symbol"] | ||
} | ||
}, | ||
"encode": { | ||
"update": { | ||
"width": {"field": {"group": "width"}}, | ||
"height": {"field": {"group": "height"}} | ||
} | ||
}, | ||
"marks": [ | ||
{ | ||
"name": "marks", | ||
"type": "trail", | ||
"style": ["trail"], | ||
"sort": {"field": "datum[\"date\"]"}, | ||
"from": {"data": "faceted_path_main"}, | ||
"encode": { | ||
"update": { | ||
"fill": {"scale": "color", "field": "symbol"}, | ||
"description": { | ||
"signal": "\"date: \" + (timeFormat(datum[\"date\"], '%b %d, %Y')) + \"; price: \" + (format(datum[\"price\"], \"\")) + \"; symbol: \" + (isValid(datum[\"symbol\"]) ? datum[\"symbol\"] : \"\"+datum[\"symbol\"])" | ||
}, | ||
"x": {"scale": "x", "field": "date"}, | ||
"y": {"scale": "y", "field": "price"}, | ||
"size": {"scale": "size", "field": "price"}, | ||
"defined": { | ||
"signal": "isValid(datum[\"date\"]) && isFinite(+datum[\"date\"]) && isValid(datum[\"price\"]) && isFinite(+datum[\"price\"])" | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"scales": [ | ||
{ | ||
"name": "x", | ||
"type": "time", | ||
"domain": {"data": "source_0", "field": "date"}, | ||
"range": [0, {"signal": "width"}] | ||
}, | ||
{ | ||
"name": "y", | ||
"type": "linear", | ||
"domain": {"data": "source_0", "field": "price"}, | ||
"range": [{"signal": "height"}, 0], | ||
"nice": true, | ||
"zero": true | ||
}, | ||
{ | ||
"name": "color", | ||
"type": "ordinal", | ||
"domain": {"data": "source_0", "field": "symbol", "sort": true}, | ||
"range": "category" | ||
}, | ||
{ | ||
"name": "size", | ||
"type": "linear", | ||
"domain": {"data": "source_0", "field": "price"}, | ||
"range": [1, 8], | ||
"zero": true | ||
} | ||
] | ||
} |
89 changes: 89 additions & 0 deletions
89
sg2d-vega-test-data/vega-specs/trail/trail_stocks_opacity.vg.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{ | ||
"$schema": "https://vega.github.io/schema/vega/v5.json", | ||
"description": "Stock prices of 5 Tech Companies over Time.", | ||
"background": "white", | ||
"padding": 5, | ||
"width": 400, | ||
"height": 400, | ||
"style": "cell", | ||
"config": {"style": {"cell": {"stroke": "transparent"}}}, | ||
"data": [ | ||
{ | ||
"name": "source_0", | ||
"url": "data/stocks.csv", | ||
"format": {"type": "csv", "parse": {"date": "date"}, "delimiter": ","} | ||
} | ||
], | ||
"marks": [ | ||
{ | ||
"name": "pathgroup", | ||
"type": "group", | ||
"from": { | ||
"facet": { | ||
"name": "faceted_path_main", | ||
"data": "source_0", | ||
"groupby": ["symbol"] | ||
} | ||
}, | ||
"encode": { | ||
"update": { | ||
"width": {"field": {"group": "width"}}, | ||
"height": {"field": {"group": "height"}} | ||
} | ||
}, | ||
"marks": [ | ||
{ | ||
"name": "marks", | ||
"type": "trail", | ||
"style": ["trail"], | ||
"sort": {"field": "datum[\"date\"]"}, | ||
"from": {"data": "faceted_path_main"}, | ||
"encode": { | ||
"update": { | ||
"fill": {"scale": "color", "field": "symbol"}, | ||
"fillOpacity": {"value": 0.6}, | ||
"description": { | ||
"signal": "\"date: \" + (timeFormat(datum[\"date\"], '%b %d, %Y')) + \"; price: \" + (format(datum[\"price\"], \"\")) + \"; symbol: \" + (isValid(datum[\"symbol\"]) ? datum[\"symbol\"] : \"\"+datum[\"symbol\"])" | ||
}, | ||
"x": {"scale": "x", "field": "date"}, | ||
"y": {"scale": "y", "field": "price"}, | ||
"size": {"scale": "size", "field": "price"}, | ||
"defined": { | ||
"signal": "isValid(datum[\"date\"]) && isFinite(+datum[\"date\"]) && isValid(datum[\"price\"]) && isFinite(+datum[\"price\"])" | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"scales": [ | ||
{ | ||
"name": "x", | ||
"type": "time", | ||
"domain": {"data": "source_0", "field": "date"}, | ||
"range": [0, {"signal": "width"}] | ||
}, | ||
{ | ||
"name": "y", | ||
"type": "linear", | ||
"domain": {"data": "source_0", "field": "price"}, | ||
"range": [{"signal": "height"}, 0], | ||
"nice": true, | ||
"zero": true | ||
}, | ||
{ | ||
"name": "color", | ||
"type": "ordinal", | ||
"domain": {"data": "source_0", "field": "symbol", "sort": true}, | ||
"range": "category" | ||
}, | ||
{ | ||
"name": "size", | ||
"type": "linear", | ||
"domain": {"data": "source_0", "field": "price"}, | ||
"range": [1, 8], | ||
"zero": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ pub mod rule; | |
pub mod shape; | ||
pub mod symbol; | ||
pub mod text; | ||
pub mod trail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::error::VegaSceneGraphError; | ||
use crate::marks::mark::{VegaMarkContainer, VegaMarkItem}; | ||
use serde::{Deserialize, Serialize}; | ||
use sg2d::marks::mark::SceneMark; | ||
use sg2d::marks::trail::TrailMark; | ||
use sg2d::value::EncodingValue; | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct VegaTrailItem { | ||
pub x: f32, | ||
pub y: f32, | ||
pub defined: Option<bool>, | ||
pub size: Option<f32>, | ||
pub fill: Option<String>, | ||
pub fill_opacity: Option<f32>, | ||
pub opacity: Option<f32>, | ||
} | ||
|
||
impl VegaMarkItem for VegaTrailItem {} | ||
|
||
impl VegaMarkContainer<VegaTrailItem> { | ||
pub fn to_scene_graph(&self, origin: [f32; 2]) -> Result<SceneMark, VegaSceneGraphError> { | ||
// Get shape of first item and use that for all items for now | ||
let first = self.items.first(); | ||
|
||
// Parse stroke color (Note, vega uses "fill" for the trail, but we use stroke | ||
let mut stroke = [0.0, 0.0, 0.0, 1.0]; | ||
if let Some(item) = &first { | ||
if let Some(stroke_css) = &item.fill { | ||
let c = csscolorparser::parse(stroke_css)?; | ||
let base_opacity = item.opacity.unwrap_or(1.0); | ||
let stroke_opacity = c.a as f32 * item.fill_opacity.unwrap_or(1.0) * base_opacity; | ||
stroke = [c.r as f32, c.g as f32, c.b as f32, stroke_opacity] | ||
} | ||
} | ||
|
||
let mut mark = TrailMark { | ||
clip: self.clip, | ||
stroke, | ||
..Default::default() | ||
}; | ||
|
||
// Init vector for each encoding channel | ||
let mut x = Vec::<f32>::new(); | ||
let mut y = Vec::<f32>::new(); | ||
let mut size = Vec::<f32>::new(); | ||
let mut defined = Vec::<bool>::new(); | ||
|
||
for item in &self.items { | ||
x.push(item.x + origin[0]); | ||
y.push(item.y + origin[1]); | ||
if let Some(v) = item.size { | ||
size.push(v); | ||
} | ||
if let Some(v) = item.defined { | ||
defined.push(v); | ||
} | ||
} | ||
|
||
// Override values with vectors | ||
let len = self.items.len(); | ||
mark.len = len as u32; | ||
|
||
if x.len() == len { | ||
mark.x = EncodingValue::Array { values: x }; | ||
} | ||
if y.len() == len { | ||
mark.y = EncodingValue::Array { values: y }; | ||
} | ||
if size.len() == len { | ||
mark.size = EncodingValue::Array { values: size }; | ||
} | ||
if defined.len() == len { | ||
mark.defined = EncodingValue::Array { values: defined }; | ||
} | ||
|
||
Ok(SceneMark::Trail(mark)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.