Skip to content

Instantly share code, notes, and snippets.

@tmori3y2
Last active October 10, 2019 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmori3y2/25c205e257c2386fe1f439757a0383d4 to your computer and use it in GitHub Desktop.
Save tmori3y2/25c205e257c2386fe1f439757a0383d4 to your computer and use it in GitHub Desktop.
[vega-lite] Line Chart of radian scale functions using sequence/fold/labelExpr (vega-lite v4.0)
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "Plots radian scale functions using sequence/fold/labelExpr.",
"width": 300,
"height": 150,
"data": {
"sequence": {
"start": -1,
"stop": 1.1,
"step": 0.1,
"as": "Normalized Frequency"
}
},
"transform": [
{
"calculate": "datum['Normalized Frequency'] * PI",
"as": "Radian"
},
{
"calculate": "sin(datum['Radian'])",
"as": "sin(x)"
},
{
"calculate": "cos(datum['Radian'])",
"as": "cos(x)"
},
{
"fold": ["sin(x)", "cos(x)"],
"as": ["Expression", "Value"]
}
],
"mark": "line",
"encoding": {
"x": {
"field": "Normalized Frequency",
"type": "quantitative",
"axis": {
"title": "x",
"format": ".2f",
"offset": -75,
"titleAngle": 0,
"titleX": -10,
"titleY": -5,
"tickCount": 4,
"labelExpr": "datum.label + 'π'"
}
},
"y": {
"field": "Value",
"type": "quantitative",
"axis": {
"title": "y",
"format": ".1f",
"offset": -150,
"titleAngle": 0,
"titleX": 0,
"titleY": -10
}
},
"color": {
"field": "Expression",
"type": "nominal",
"legend": {
"title": "Expression"
}
}
}
}
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "Plots radian scale functions using sequence/fold/labelExpr.",
"autosize": "pad",
"padding": 5,
"width": 300,
"height": 150,
"style": "cell",
"data": [
{
"name": "source_0",
"transform": [
{
"type": "sequence",
"start": -1,
"stop": 1.1,
"step": 0.1,
"as": "Normalized Frequency"
},
{
"type": "formula",
"expr": "datum['Normalized Frequency'] * PI",
"as": "Radian"
},
{"type": "formula", "expr": "sin(datum['Radian'])", "as": "sin(x)"},
{"type": "formula", "expr": "cos(datum['Radian'])", "as": "cos(x)"},
{
"type": "fold",
"fields": ["sin(x)", "cos(x)"],
"as": ["Expression", "Value"]
}
]
}
],
"marks": [
{
"name": "pathgroup",
"type": "group",
"from": {
"facet": {
"name": "faceted_path_main",
"data": "source_0",
"groupby": ["Expression"]
}
},
"encode": {
"update": {
"width": {"field": {"group": "width"}},
"height": {"field": {"group": "height"}}
}
},
"marks": [
{
"name": "marks",
"type": "line",
"style": ["line"],
"sort": {"field": "datum[\"Normalized Frequency\"]"},
"from": {"data": "faceted_path_main"},
"encode": {
"update": {
"stroke": {"scale": "color", "field": "Expression"},
"x": {"scale": "x", "field": "Normalized Frequency"},
"y": {"scale": "y", "field": "Value"},
"defined": {
"signal": "datum[\"Normalized Frequency\"] !== null && isFinite(datum[\"Normalized Frequency\"]) && datum[\"Value\"] !== null && isFinite(datum[\"Value\"])"
}
}
}
}
]
}
],
"scales": [
{
"name": "x",
"type": "linear",
"domain": {"data": "source_0", "field": "Normalized Frequency"},
"range": [0, {"signal": "width"}],
"nice": true,
"zero": false
},
{
"name": "y",
"type": "linear",
"domain": {"data": "source_0", "field": "Value"},
"range": [{"signal": "height"}, 0],
"nice": true,
"zero": true
},
{
"name": "color",
"type": "ordinal",
"domain": {"data": "source_0", "field": "Expression", "sort": true},
"range": "category"
}
],
"axes": [
{
"scale": "x",
"orient": "bottom",
"tickCount": 4,
"gridScale": "y",
"grid": true,
"domain": false,
"labels": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "y",
"orient": "left",
"gridScale": "x",
"grid": true,
"tickCount": {"signal": "ceil(height/40)"},
"domain": false,
"labels": false,
"maxExtent": 0,
"minExtent": 0,
"ticks": false,
"zindex": 0
},
{
"scale": "x",
"orient": "bottom",
"grid": false,
"title": "x",
"format": ".2f",
"offset": -75,
"tickCount": 4,
"titleAngle": 0,
"titleX": -10,
"titleY": -5,
"labelFlush": true,
"labelOverlap": true,
"encode": {
"labels": {"update": {"text": {"signal": "datum.label + 'π'"}}}
},
"zindex": 0
},
{
"scale": "y",
"orient": "left",
"grid": false,
"title": "y",
"format": ".1f",
"offset": -150,
"titleAngle": 0,
"titleX": 0,
"titleY": -10,
"labelOverlap": true,
"tickCount": {"signal": "ceil(height/40)"},
"zindex": 0
}
],
"legends": [
{
"title": "Expression",
"stroke": "color",
"gradientLength": {"signal": "clamp(height, 64, 200)"},
"symbolType": "stroke"
}
]
}
@tmori3y2
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment