Signal that filters the data not work correctly in Vega - vega

I have a visualization that represents the cumulative total of accidents per month, that is, the difference between the accidents that occur and those that are resolved (closed). I start with some data for each claim that occurs (ID number, entry date, closing date and client who has the claim)
I am trying to put a signal that filters the data by clients, but it does not work correctly.
The problem occurs when I select the "Baukost" client, it joins the points well, but when I select "all" again it joins the lines wrong.
In the first photo I show the display that comes out by default (all)
In the second photo I show the visualization that comes out when I filter by "Baukost" client.
The third photo is where the problem that occurs when I reselect "all" is shown, which should be the same as the first image
Here I put the code that I have done and I ask you to please help me to see what I am failing. Thank you very much in advance.
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 800,
"height": 300,
"padding": 10,
"signals": [
{ "name": "Customer", "value": "All",
"bind": {
"input": "select",
"name": "Customers",
"options": [
"All",
"Baukost",
"CatalanaOcc",
],
}
},
],
"data": [
{"name": "Accidents_source_load",
"values": [
{
"Number" : "0691328/AF21",
"EntryDay" : 1,
"EntryMonth" : 7,
"EntryYear" : 2021,
"CloseDay" : 2,
"CloseMonth" : 8,
"CloseYear" : 2021,
"Customer" : "Baukost",
},
{
"Number" : "9989037919",
"EntryDay" : 16,
"EntryMonth" : 7,
"EntryYear" : 2021,
"CloseDay" : 25,
"CloseMonth" : 8,
"CloseYear" : 2021,
"Customer" : "Baukost",
},
{
"Number" : "3157418 ",
"EntryDay" : 24,
"EntryMonth" : 2,
"EntryYear" : 2022,
"CloseDay" : null,
"CloseMonth" : null,
"CloseYear" : null,
"Customer" : "Baukost",
},
{
"Number" : "84032071",
"EntryDay" : 24,
"EntryMonth" : 2,
"EntryYear" : 2022,
"CloseDay" : null,
"CloseMonth" : null,
"CloseYear" : null,
"Customer" : "Baukost",
},
{
"Number" : "27800304",
"EntryDay" : 27,
"EntryMonth" : 5,
"EntryYear" : 2021,
"CloseDay" : 2,
"CloseMonth" : 6,
"CloseYear" : 2021,
"Customer" : "CatalanaOcc",
},
{
"Number" : "27802536",
"EntryDay" : 27,
"EntryMonth" : 5,
"EntryYear" : 2021,
"CloseDay" : 28,
"CloseMonth" : 5,
"CloseYear" : 2021,
"Customer" : "CatalanaOcc",
},
{
"Number" : "27808817",
"EntryDay" : 28,
"EntryMonth" : 5,
"EntryYear" : 2021,
"CloseDay" : 2,
"CloseMonth" : 6,
"CloseYear" : 2021,
"Customer" : "CatalanaOcc",
},
{
"Number" : "27821933","EntryDay" : 1,
"EntryMonth" : 6,
"EntryYear" : 2021,"CloseDay" : null,
"CloseMonth" : null,
"CloseYear" : null,"Customer" : "CatalanaOcc",
},
]
},
{ "name": "Accidents",
"source": "Accidents_source_load",
"transform": [
{ "type": "formula",
"expr": "datetime(datum.EntryYear, datum.EntryMonth-1,datum.EntryDay)",
"as":"EntryDate"
},
{ "type": "formula",
"expr": "if(datum.CloseMonth==null, null, datetime(datum.CloseYear, datum.CloseMonth-1,datum.CloseDay))",
"as":"CloseDate"
},
{
"type": "formula",
"expr": "if(datum.CloseMonth==null,'Entry Cabinet', 'Closed Cabinet')",
"as":"Type"
},
{ "type": "formula",
"expr": "if(Customer=='All'||datum.Customer==Customer,1,0)",
"as": "cond_Customer"
},
{ "type": "filter",
"expr": "datum.cond_Customer==1"
},
{ "type": "collect",
"sort":{
"field":["EntryDate"],
"order": ["ascending"]
}
},
]
},
{ "name": "Close_table",
"source": "Accidents",
"transform": [
{
"type": "filter",
"expr": "datum.Type=='Closed Cabinet'"
},
{
"type": "formula",
"expr": "datum.CloseDate",
"as": "Date"
},
{
"type": "aggregate",
"groupby":["Date"],
"fields":["Number"],
"ops":["count"],
"as":["Total"]
},
{
"type": "collect",
"sort":
{
"field": ["Date"],
"order": ["ascending"]
}
},
{
"type": "window",
"sort": {"field": "Date", "order": "ascending"},
"ops": ["sum"],
"fields": [ "Total"],
"as": [ "Cumulative"]
},
{
"type": "formula",
"expr": "datum.Cumulative*(-1)",
"as": "Cumulative"
},
{ "type": "collect",
"sort":{
"field":["Date"],
"order": ["ascending"]
}
},
]
},
{ "name": "Entry_table",
"source": "Accidents",
"transform": [
{
"type": "formula",
"expr": "datum.EntryDate",
"as": "Date"
},
{
"type": "aggregate",
"groupby":["Date"],
"fields":["Number"],
"ops":["count"],
"as":["Total"]
},
{
"type": "collect",
"sort":
{
"field": ["Date"],
"order": ["ascending"]
}
},
{
"type": "window",
"ops": ["sum"],
"fields": [ "Total"],
"as": [ "Cumulative"]
},
{ "type": "collect",
"sort":{
"field":["Date"],
"order": ["ascending"]
}
},
]
},
{ "name": "Entry_Close",
"source":["Entry_table","Close_table"],
"transform": [
{ "type": "collect",
"sort":{
"field":["Date"],
"order": ["ascending"]
}
},
{ "type": "aggregate",
"groupby": ["Date"],
"fields": ["Cumulative"],
"ops": ["sum"],
"as": ["Cumulative_Total"]
},
{ "type": "collect",
"sort":{
"field":["Date"],
"order": ["ascending"]
}
},
]
},
],
"scales": [
{ "name": "yscale",
"type": "linear",
"domain": {"data": "Entry_Close", "field": "Cumulative_Total"},
"range": "height"
},
{
"name": "xscale",
"type": "time",
"domain": {"data": "Entry_Close", "field":"Date","sort": true},
"range": "width",
},
],
"axes": [
{ "orient": "bottom", "scale": "xscale" },
{ "orient": "left", "scale": "yscale" }
],
"marks": [
{
"type": "line",
"from": {"data": "Entry_Close"},
"encode": {
"update": {
"x": {"scale": "xscale", "field": "Date"},
"y": {"scale": "yscale", "field": "Cumulative_Total"},
"stroke": {"value": "blue"},
"strokeWidth": {"value": 4},
"interpolate": {"value": "linear"},
"strokeOpacity": {"value": 1},
},
"hover": {
"strokeOpacity": {"value": 0.5}
}
}
},
{ "type": "symbol",
"style": ["point"],
"from": {"data": "Entry_Close"},
"encode": {
"update": {
"x": {"scale": "xscale", "field": "Date"},
"y": {"scale": "yscale", "field": "Cumulative_Total"},
"size": {"value": 60},
"fill": {"value": "green"},
"cursor": { "value": "pointer" },
"tooltip": {"signal": "{'title': 'Cumulative', 'Date': datum.Date,'Total': datum.Cumulative_Total}"}
}
}
},
],
} ```

You need to add a sort field to your mark. This should work for you.
Editor
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 800,
"height": 300,
"padding": 10,
"signals": [
{
"name": "Customer",
"value": "All",
"bind": {
"input": "select",
"name": "Customers",
"options": ["All", "Baukost", "CatalanaOcc"]
}
}
],
"data": [
{
"name": "Accidents_source_load",
"values": [
{
"Number": "0691328/AF21",
"EntryDay": 1,
"EntryMonth": 7,
"EntryYear": 2021,
"CloseDay": 2,
"CloseMonth": 8,
"CloseYear": 2021,
"Customer": "Baukost"
},
{
"Number": "9989037919",
"EntryDay": 16,
"EntryMonth": 7,
"EntryYear": 2021,
"CloseDay": 25,
"CloseMonth": 8,
"CloseYear": 2021,
"Customer": "Baukost"
},
{
"Number": "3157418 ",
"EntryDay": 24,
"EntryMonth": 2,
"EntryYear": 2022,
"CloseDay": null,
"CloseMonth": null,
"CloseYear": null,
"Customer": "Baukost"
},
{
"Number": "84032071",
"EntryDay": 24,
"EntryMonth": 2,
"EntryYear": 2022,
"CloseDay": null,
"CloseMonth": null,
"CloseYear": null,
"Customer": "Baukost"
},
{
"Number": "27800304",
"EntryDay": 27,
"EntryMonth": 5,
"EntryYear": 2021,
"CloseDay": 2,
"CloseMonth": 6,
"CloseYear": 2021,
"Customer": "CatalanaOcc"
},
{
"Number": "27802536",
"EntryDay": 27,
"EntryMonth": 5,
"EntryYear": 2021,
"CloseDay": 28,
"CloseMonth": 5,
"CloseYear": 2021,
"Customer": "CatalanaOcc"
},
{
"Number": "27808817",
"EntryDay": 28,
"EntryMonth": 5,
"EntryYear": 2021,
"CloseDay": 2,
"CloseMonth": 6,
"CloseYear": 2021,
"Customer": "CatalanaOcc"
},
{
"Number": "27821933",
"EntryDay": 1,
"EntryMonth": 6,
"EntryYear": 2021,
"CloseDay": null,
"CloseMonth": null,
"CloseYear": null,
"Customer": "CatalanaOcc"
}
]
},
{
"name": "Accidents",
"source": "Accidents_source_load",
"transform": [
{
"type": "formula",
"expr": "datetime(datum.EntryYear, datum.EntryMonth-1,datum.EntryDay)",
"as": "EntryDate"
},
{
"type": "formula",
"expr": "if(datum.CloseMonth==null, null, datetime(datum.CloseYear, datum.CloseMonth-1,datum.CloseDay))",
"as": "CloseDate"
},
{
"type": "formula",
"expr": "if(datum.CloseMonth==null,'Entry Cabinet', 'Closed Cabinet')",
"as": "Type"
},
{
"type": "formula",
"expr": "if(Customer=='All'||datum.Customer==Customer,1,0)",
"as": "cond_Customer"
},
{"type": "filter", "expr": "datum.cond_Customer==1"},
{
"type": "collect",
"sort": {"field": ["EntryDate"], "order": ["ascending"]}
}
]
},
{
"name": "Close_table",
"source": "Accidents",
"transform": [
{"type": "filter", "expr": "datum.Type=='Closed Cabinet'"},
{"type": "formula", "expr": "datum.CloseDate", "as": "Date"},
{
"type": "aggregate",
"groupby": ["Date"],
"fields": ["Number"],
"ops": ["count"],
"as": ["Total"]
},
{
"type": "collect",
"sort": {"field": ["Date"], "order": ["ascending"]}
},
{
"type": "window",
"sort": {"field": "Date", "order": "ascending"},
"ops": ["sum"],
"fields": ["Total"],
"as": ["Cumulative"]
},
{
"type": "formula",
"expr": "datum.Cumulative*(-1)",
"as": "Cumulative"
},
{"type": "collect", "sort": {"field": ["Date"], "order": ["ascending"]}}
]
},
{
"name": "Entry_table",
"source": "Accidents",
"transform": [
{"type": "formula", "expr": "datum.EntryDate", "as": "Date"},
{
"type": "aggregate",
"groupby": ["Date"],
"fields": ["Number"],
"ops": ["count"],
"as": ["Total"]
},
{
"type": "collect",
"sort": {"field": ["Date"], "order": ["ascending"]}
},
{
"type": "window",
"ops": ["sum"],
"fields": ["Total"],
"as": ["Cumulative"]
},
{"type": "collect", "sort": {"field": ["Date"], "order": ["ascending"]}}
]
},
{
"name": "Entry_Close",
"source": ["Entry_table", "Close_table"],
"transform": [
{
"type": "collect",
"sort": {"field": ["Date"], "order": ["ascending"]}
},
{
"type": "aggregate",
"groupby": ["Date"],
"fields": ["Cumulative"],
"ops": ["sum"],
"as": ["Cumulative_Total"]
},
{"type": "collect", "sort": {"field": ["Date"], "order": ["ascending"]}}
]
}
],
"scales": [
{
"name": "yscale",
"type": "linear",
"domain": {"data": "Entry_Close", "field": "Cumulative_Total"},
"range": "height"
},
{
"name": "xscale",
"type": "time",
"domain": {"data": "Entry_Close", "field": "Date", "sort": true},
"range": "width"
}
],
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "left", "scale": "yscale"}
],
"marks": [
{
"type": "line",
"from": {"data": "Entry_Close"},
"sort": {"field": "datum.Date"},
"encode": {
"update": {
"x": {"scale": "xscale", "field": "Date"},
"y": {"scale": "yscale", "field": "Cumulative_Total"},
"stroke": {"value": "blue"},
"strokeWidth": {"value": 4},
"interpolate": {"value": "linear"},
"strokeOpacity": {"value": 1}
},
"hover": {"strokeOpacity": {"value": 0.5}}
}
},
{
"type": "symbol",
"style": ["point"],
"from": {"data": "Entry_Close"},
"encode": {
"update": {
"x": {"scale": "xscale", "field": "Date"},
"y": {"scale": "yscale", "field": "Cumulative_Total"},
"size": {"value": 60},
"fill": {"value": "green"},
"cursor": {"value": "pointer"},
"tooltip": {
"signal": "{'title': 'Cumulative', 'Date': datum.Date,'Total': datum.Cumulative_Total}"
}
}
}
}
]
}

Related

Vega Zoomable USA Map

I want to use this map https://vega.github.io/vega/examples/zoomable-world-map/ but only for USA.
I used this spec.
private specVega = {
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An interactive world map supporting pan and zoom.",
"width": 900,
"height": 500,
"autosize": "none",
"signals": [
{ "name": "tx", "update": "width / 2" },
{ "name": "ty", "update": "height / 2" },
{
"name": "scale",
"value": 150,
"on": [{
"events": {"type": "wheel", "consume": true},
"update": "clamp(scale * pow(1.0005, -event.deltaY * pow(16, event.deltaMode)), 150, 3000)"
}]
},
{
"name": "angles",
"value": [0, 0],
"on": [{
"events": "mousedown",
"update": "[rotateX, centerY]"
}]
},
{
"name": "cloned",
"value": null,
"on": [{
"events": "mousedown",
"update": "copy('projection')"
}]
},
{
"name": "start",
"value": null,
"on": [{
"events": "mousedown",
"update": "invert(cloned, xy())"
}]
},
{
"name": "drag", "value": null,
"on": [{
"events": "[mousedown, window:mouseup] > window:mousemove",
"update": "invert(cloned, xy())"
}]
},
{
"name": "delta", "value": null,
"on": [{
"events": {"signal": "drag"},
"update": "[drag[0] - start[0], start[1] - drag[1]]"
}]
},
{
"name": "rotateX", "value": 0,
"on": [{
"events": {"signal": "delta"},
"update": "angles[0] + delta[0]"
}]
},
{
"name": "centerY", "value": 0,
"on": [{
"events": {"signal": "delta"},
"update": "clamp(angles[1] + delta[1], -60, 60)"
}]
}
],
"projections": [
{
"name": "projection",
"type": "mercator",
"scale": {"signal": "scale"},
"rotate": [{"signal": "rotateX"}, 0, 0],
"center": [0, {"signal": "centerY"}],
"translate": [{"signal": "tx"}, {"signal": "ty"}]
}
],
"data": [
{
"name": "counties",
values: null,
"format": {"type": "topojson", "feature": "states"},
},
{
"name": "graticule",
"transform": [
{ "type": "graticule", "step": [15, 15] }
]
}
],
"marks": [
{
"type": "shape",
"from": {"data": "graticule"},
"encode": {
"enter": {
"strokeWidth": {"value": 1},
"stroke": {"value": "#ddd"},
"fill": {"value": null}
}
},
"transform": [
{ "type": "geoshape", "projection": "projection" }
]
},
{
"type": "shape",
"from": {"data": "states"},
"encode": {
"enter": {
"strokeWidth": {"value": 0.5},
"stroke": {"value": "#bbb"},
"fill": {"value": "#e5e8d3"}
}
},
"transform": [
{ "type": "geoshape", "projection": "projection" }
]
}
]
}
this.specVega["data"][0]["values"] = "data/us-10m.json" (this is just for understanding which data i used)
So i put data for only US here, but it didn't work. Got an error in console:
Undefined data set name: "states"
In general i just need a zoomable map for USA only, which i am going to use as a bubble map.
If this map has a zoom i would definitely use it https://vega.github.io/vega-lite/examples/geo_layer.html
Here is an working example of USA map by states or counties in "albersUsa" projection. Zooming is by mouse wheel and panning by mouse click-drag. For better performance, zooming and panning are best done with states instead of counties.
View in Vega online editor
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An interactive USA map supporting pan and zoom.",
"width": 900,
"height": 500,
"autosize": "none",
"signals": [
{
"name": "signal_show_map_graticule",
"value": true,
"bind": {
"input": "checkbox",
"name": "Map graticule: "}
},
{
"name": "signal_states_or_counties",
"value": "states",
"bind": { "input": "select",
"options": ["states", "counties"],
"name": "Map areas: "
}
},
{
"name": "signal_map_scale",
"value": 1000,
"bind": {"input": "range", "min": 1000, "max": 4000, "step": 50, "name": "Map scale: "},
"on": [{
"events": {"type": "wheel", "consume": true},
"update": "round(clamp(signal_map_scale * pow(1.0005, -event.deltaY * pow(16, event.deltaMode)), 1000, 4000))"
}]
},
{ "name": "signal_translate_xy",
"value": [450, 250],
"update": "[clamp(signal_translate_last_xy[0] + signal_mouse_delta_xy[0], -300, 1200), clamp(signal_translate_last_xy[1] + signal_mouse_delta_xy[1], -200, 800)]"
},
{ "name": "signal_translate_last_xy",
"value": [450, 250],
"on": [{
"events": [
{"type": "mousedown"}
],
"update": "signal_translate_xy"
}]
},
{
"name": "signal_mouse_start_xy",
"value": [0, 0],
"on": [{
"events": [
{"type": "mousedown"}
],
"update": "xy()"
}]
},
{
"name": "signal_mouse_drag_xy",
"value": [0, 0],
"on": [{
"events": [
{
"type": "mousemove",
"between": [
{"type": "mousedown"},
{"type": "mouseup"}
]
}
],
"update": "xy()"
}]
},
{
"name": "signal_mouse_delta_xy",
"value": [0, 0],
"update": "[signal_mouse_drag_xy[0] - signal_mouse_start_xy[0], signal_mouse_drag_xy[1] - signal_mouse_start_xy[1]]"
}
],
"projections": [
{
"name": "map_projection",
"type": "albersUsa",
"scale": {"signal": "signal_map_scale"},
"translate": {"signal": "signal_translate_xy"}
}
],
"data": [
{
"name": "data_geo_usa",
"url": "data/us-10m.json",
"format": {
"type": "topojson",
"feature": {"signal": "signal_states_or_counties"}
}
},
{
"name": "data_geo_graticule",
"transform": [
{ "type": "graticule", "step": [5, 5] }
]
}
],
"marks": [
{
"type": "shape",
"from": {"data": "data_geo_graticule"},
"encode": {
"enter": {
"strokeWidth": {"value": 1},
"stroke": {"value": "#ddd"}
},
"update": {
"strokeOpacity": {"signal": "signal_show_map_graticule ? 1 : 0"}
}
},
"transform": [
{ "type": "geoshape", "projection": "map_projection" }
]
},
{
"type": "shape",
"from": {"data": "data_geo_usa"},
"encode": {
"enter": {
"strokeWidth": {"value": 0.5},
"stroke": {"value": "#bbb"},
"fill": {"value": "#e5e8d3"}
}
},
"transform": [
{ "type": "geoshape", "projection": "map_projection" }
]
}
]
}
Change "data" to:
"data": [
{
"name": "states",
"url": "data/us-10m.json",
"format": {"type": "topojson", "feature": "states"}
},
View in Vega online editor
Apologies but I'm not a mapping expert. I have the following where zoom works fine but the drag is a little janky but definitely useable. It would probably take me quite a while to get this any better.
Roy's answer is another option for you.
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"autosize": "none",
"background": "white",
"padding": 5,
"width": 500,
"height": 300,
"signals": [
{"name": "tx", "update": "width / 2"},
{"name": "ty", "update": "height / 2"},
{
"name": "scale",
"value": 685,
"on": [
{
"events": {"type": "wheel", "consume": true},
"update": "clamp(scale * pow(1.0005, -event.deltaY * pow(16, event.deltaMode)), 150, 3000)"
}
]
},
{
"name": "angles",
"value": [222, 146],
"on": [{"events": "mousedown", "update": "[rotateX, centerY]"}]
},
{
"name": "cloned",
"value": null,
"on": [{"events": "mousedown", "update": "copy('projection')"}]
},
{
"name": "start",
"value": null,
"on": [{"events": "mousedown", "update": "invert(cloned, xy())"}]
},
{
"name": "drag",
"value": null,
"on": [
{
"events": "[mousedown, window:mouseup] > window:mousemove",
"update": "invert(cloned, xy())"
}
]
},
{
"name": "delta",
"value": null,
"on": [
{
"events": {"signal": "drag"},
"update": "[(drag[0] - start[0])*4, (start[1] - drag[1])*4]"
}
]
},
{
"name": "rotateX",
"value": 250,
"on": [{"events": {"signal": "delta"}, "update": "angles[0] + delta[0]"}]
},
{
"name": "centerY",
"value": 140,
"on": [{"events": {"signal": "delta"}, "update": "angles[1] + delta[1]"}]
}
],
"data": [
{
"name": "source_0",
"url": "data/us-10m.json",
"format": {"type": "topojson", "feature": "states"}
}
],
"projections": [
{
"name": "projection",
"scale": {"signal": "scale"},
"translate": [{"signal": "rotateX"}, {"signal": "centerY"}],
"type": "albersUsa"
}
],
"marks": [
{
"name": "marks",
"type": "shape",
"style": ["geoshape"],
"from": {"data": "source_0"},
"encode": {
"update": {"fill": {"value": "lightgray"}, "stroke": {"value": "white"}}
},
"transform": [{"type": "geoshape", "projection": "projection"}]
}
]
}

Labels on Rects in Vega Stacked Bar Chart

In the old vega-label documentation there is reference to a labeled stacked bar chart example. The example spec doesn’t work in the online Vega editor, throwing an error that a.map is not a function.
I am trying to do almost exactly what is referenced in the above example: create a vertically-stacked bar chart where each segment is labeled with its value. I have seen a few examples of this in Vega-Lite with layers, but how can it be achieved in Vega?
I’ve attempted to recreate the above spec using a group containing the rect mark and the text mark, but I’m not getting it right. I also see how to display the value as a tooltip on hover, but I’d like it displayed in the rectangle of each segment of the bar itself.
Ideally I would end up with a chart like this:
Here’s the spec I’ve been working with:
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A basic stacked bar chart example.",
"width": 294,
"height": 200,
"padding": 5,
"autosize": {"type": "fit-x", "contains": "padding"},
"data": [
{
"name": "categories",
"values": [
{"y": 5, "c": 0, "name": "other"},
{"y": 7, "c": 1, "name": "corn"},
{"y": 22, "c": 2, "name": "pasture/hay"},
{"y": 31, "c": 3, "name": "developed"},
{"y": 35, "c": 4, "name": "forest"}
],
"transform": [
{
"type": "stack",
"field": "y"
}
]
}
],
"scales": [
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true, "zero": true,
"domain": {"data": "categories", "field": "y1"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "categories", "field": "c"}
}
],
"axes": [
],
"marks": [
{
"name": "categoriesBars",
"type": "group",
"from": {"data": "categories"},
"zindex": 1,
"marks": [
{
"type": "rect",
"from": {"data": "categories"},
"encode": {
"enter": {
"x": {"value": 0},
"width": {"value": 294},
"y": {"scale": "y", "field": "y0"},
"y2": {"scale": "y", "field": "y1"},
"fill": {"scale": "color", "field": "c"}
},
"update": {
"fillOpacity": {"value": 1}
},
"hover": {
"fillOpacity": {"value": 0.5}
}
}
},
{
"type": "text",
"encode": {
"enter": {
"y": {"field": {"parent": "y"}},
"x": {"value": 130},
"fill": [
{"test": "contrast('white', datum.fill) > contrast('black', datum.fill)", "value": "white"},
{"value": "black"}
],
"align": {"value": "center"},
"baseline": {"value": "middle"},
"text": {"field": {"parent": "name"}}
}
}
}
]
}
]
}
Is this what you're after?
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A basic stacked bar chart example.",
"width": 294,
"height": 200,
"padding": 5,
"autosize": {"type": "fit-x", "contains": "padding"},
"data": [
{
"name": "categories",
"values": [
{"y": 5, "c": 0, "name": "other"},
{"y": 7, "c": 1, "name": "corn"},
{"y": 22, "c": 2, "name": "pasture/hay"},
{"y": 31, "c": 3, "name": "developed"},
{"y": 35, "c": 4, "name": "forest"}
],
"transform": [{"type": "stack", "field": "y"}]
}
],
"scales": [
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": true,
"domain": {"data": "categories", "field": "y1"}
},
{
"name": "color",
"type": "ordinal",
"range": {"scheme": "pastel2"},
"domain": {"data": "categories", "field": "c"}
}
],
"axes": [],
"marks": [
{
"name": "i",
"type": "rect",
"from": {"data": "categories"},
"encode": {
"enter": {
"x": {"value": 0},
"width": {"value": 294},
"y": {"scale": "y", "field": "y0"},
"y2": {"scale": "y", "field": "y1"},
"fill": {"scale": "color", "field": "c"}
},
"update": {"fillOpacity": {"value": 1}},
"hover": {"fillOpacity": {"value": 0.5}}
}
},
{
"type": "text",
"from": {"data": "i"},
"encode": {
"update": {
"y": {"field": "y"},
"dy": {"signal": "(datum.y2 - datum.y)/2"},
"x": {"signal": "width/2"},
"fill": [
{
"test": "contrast('white', datum.fill) > contrast('black', datum.fill)",
"value": "white"
},
{"value": "black"}
],
"align": {"value": "center"},
"baseline": {"value": "middle"},
"text": {"field": "datum.name"}
}
}
}
]
}

How to display multiple charts with varying x-scales side by side in Vega

I have a set of charts with the same y-scale but varying x-scales. I am using hconcat to display them side by side. In order to conserve space and avoid repetition, I have disabled the y-axis for all but the first chart. However, this is causing the title of the first chart to offset:
This is a link to a Vega Editor.
As the blue circle indicates, the two titles, "Chain" and "Mini Invaders," are not in line. Is there a way to fix this?
I have tried to express these charts using facet but as far as I can tell, facets do not permit varying x-scales. However, please do let me know if this is somehow possible with facets.
You need labelBound: true in your spec.
Editor
{
"config": {
"view": {
"continuousWidth": 400,
"continuousHeight": 300,
"stroke": "#000000",
"strokeOpacity": 1,
"strokeWidth": 2
},
"axis": {"labelFontSize": 24, "titleFontSize": 24, "labelBound":true},
"legend": {"labelFontSize": 24, "labelLimit": 0, "titleFontSize": 32},
"title": {"baseline": "bottom", "fontSize": 24}
},
"hconcat": [
{
"layer": [
{
"mark": {"type": "area", "clip": true, "opacity": 0.2},
"encoding": {
"color": {
"field": "Variations",
"legend": {
"orient": "top",
"symbolOpacity": 1,
"symbolSize": 200,
"symbolStrokeWidth": 3,
"symbolType": "stroke"
},
"scale": {
"domain": ["Original Algorithm"],
"range": [
"#e41a1c",
"#377eb8",
"#4daf4a",
"#984ea3",
"#a65628",
"#646464"
]
},
"type": "nominal"
},
"x": {"field": "step", "type": "quantitative"},
"y": {
"axis": {"labels": true, "tickCount": 5, "title": null},
"field": "lower",
"type": "quantitative"
},
"y2": {"field": "upper"}
}
},
{
"mark": {"type": "line", "clip": true},
"encoding": {
"color": {"field": "Variations", "type": "nominal"},
"x": {"field": "step", "type": "quantitative"},
"y": {
"field": "regret",
"scale": {"domain": [0, 1]},
"type": "quantitative"
}
}
}
],
"height": 200,
"title": "Chain",
"transform": [{"filter": "(datum.domain === 'Chain')"}],
"width": 200
},
{
"layer": [
{
"mark": {"type": "area", "clip": true, "opacity": 0.2},
"encoding": {
"color": {
"field": "Variations",
"legend": {
"orient": "top",
"symbolOpacity": 1,
"symbolSize": 200,
"symbolStrokeWidth": 3,
"symbolType": "stroke"
},
"scale": {
"domain": ["Original Algorithm"],
"range": [
"#e41a1c",
"#377eb8",
"#4daf4a",
"#984ea3",
"#a65628",
"#646464"
]
},
"type": "nominal"
},
"x": {"field": "step", "type": "quantitative"},
"y": {
"axis": {"labels": false, "tickCount": 5, "title": null},
"field": "lower",
"type": "quantitative"
},
"y2": {"field": "upper"}
}
},
{
"mark": {"type": "line", "clip": true},
"encoding": {
"color": {"field": "Variations", "type": "nominal"},
"x": {"field": "step", "type": "quantitative"},
"y": {
"field": "regret",
"scale": {"domain": [0, 1]},
"type": "quantitative"
}
}
}
],
"height": 200,
"title": "Mini Invaders",
"transform": [{"filter": "(datum.domain === 'Mini Invaders')"}],
"width": 200
}

Vega How to SUM all descendants value of every node on a treemap

I am trying to access the sum of values for a node in VEGA. In other words, I want to display sum of "percentage" values of all leaves for each parent node.
Got the following Vega specs (https://gist.github.com/omerakko/655674f9f37e9361fe5378b6d440e411)
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An example of treemap layout for hierarchical data.",
"width": 960,
"height": 500,
"padding": 2.5,
"autosize": "none",
"data": [
{
"name": "tree",
"url": "https://raw.githubusercontent.com/omerakko/VEGA/main/vegaTreemapData.json",
"transform": [
{
"type": "stratify",
"key": "id",
"parentKey": "parent"
},
{
"type": "treemap",
"field": "percentage",
"sort": {"field": "value", "order":"descending"},
"round": true,
"method": "resquarify",
"ratio": 1,
"size": [{"signal": "width"}, {"signal": "height"}],
"paddingOuter": 2,
"paddingInner":2
}
]
},
{
"name": "nodes",
"source": "tree",
"transform": [{ "type": "filter", "expr": "datum.children" }]
},
{
"name": "leaves",
"source": "tree",
"transform": [{ "type": "filter", "expr": "!datum.children" },
{"type": "filter", "expr": "datum.percentage > 0"}]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "nodes", "field": "name"},
"range": [
"transparent", "#dd96ba", "#dea84e", "#c83836", "#dfde9b",
"#5eafb9", "#adc35d"]
},
{
"name": "size",
"type": "ordinal",
"domain": [0, 1, 2, 3],
"range": [256, 28, 20, 14]
},
{
"name": "opacity",
"type": "ordinal",
"domain": [0, 1, 2, 3],
"range": [0.15, 0.5, 0.8, 1.0]
}
],
"marks": [
{
"type": "rect",
"from": {"data": "nodes"},
"interactive": false,
"encode": {
"enter": {
"fill": {"value":"#333238"},
"stroke": {"scale": "color", "field": "name"},
"strokeWidth":{"value": 5}
},
"update": {
"x": {"field": "x0"},
"y": {"field": "y0"},
"x2": {"field": "x1"},
"y2": {"field": "y1"},
"stroke": {"scale": "color", "field": "name"}
}
}
},
{
"type": "rect",
"from": {"data": "leaves"},
"encode": {
"enter": {
"stroke": {"value": "#fff"}
},
"update": {
"x": {"field": "x0"},
"y": {"field": "y0"},
"x2": {"field": "x1"},
"y2": {"field": "y1"},
"fill": {"value": "transparent"}
},
"hover": {
"fill": {"value": "red"}
}
}
},
{
"type": "text",
"from": {"data": "nodes"},
"interactive": false,
"encode": {
"enter": {
"font": {"value": "Helvetica Neue, Arial"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"fill": {"scale": "color", "field": "name"},
"text": {"field": "name"},
"fontSize": {"scale": "size", "field": "depth"}
},
"update": {
"x": {"signal": "0.5 * (datum.x0 + datum.x1)"},
"y": {"signal": "0.5 * (datum.y0 + datum.y1)"}
}
}
}
]
}
There is doc available https://vega.github.io/vega/docs/transforms/treemap/ here saying that I can access to what I want, but I couldnt manage to apply it to the specs.
It seems like it's not currently possible in Vega. There's this PR that hasn't been merged yet.
But you can work around that by manually aggregating the values and then looking them up. Here a gist in which there's now a total field for each node, the relevant part being:
{
"name": "leaves",
"source": "tree",
"transform": [
{"type": "filter", "expr": "!datum.children"},
{"type": "filter", "expr": "datum.percentage > 0.3"}
]
},
{
"name": "totals",
"source": "leaves",
"transform": [
{
"type": "aggregate",
"groupby": ["parent"],
"fields": ["percentage"],
"as": ["total"],
"ops": ["sum"]
}
]
},
{
"name": "nodes",
"source": "tree",
"transform": [
{"type": "filter", "expr": "datum.children"},
{
"type": "lookup",
"from": "totals",
"key": "parent",
"fields": ["id"],
"values": ["total"],
"as": ["total"]
}
]
},
Basically you just get sums of all the leaves by parent in totals and then, after constructing the base nodes dataset, look the total up in totals.
Note that this will only work for this particular example, where there are exactly two levels in the hierarchy.

Brushing/linking in vega (not vega-lite)

I'm trying to brush/link two plots in vega, more specifically a node-link diagram and a couple of scatterplots. Based on how dragging works with signals in the node-link diagram I did get quite far, but not far enough...
For the sake of simplicity, I'll provide a little test code here using just two scatterplots:
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"padding": 0,
"autosize": "none",
"width": 800,
"height": 400,
"signals": [
{ "description": "Any datapoint is activated",
"name": "datapoint_is_activated", "value": false,
"on": [
{
"events": "symbol:mouseover",
"update": "true"
},
{
"events": "symbol:mouseout",
"update": "false"
}
]
},
{ "description": "Active datapoint",
"name": "activated_datapoint", "value": null,
"on": [
{
"events": "symbol:mouseover",
"update": "item()"
},
{
"events": "symbol:mouseout",
"update": "null"
}
]
}
],
"data": [
{
"name": "table",
"values": [
{"name": "point A", "a": 1, "b": 2, "c": 3},
{"name": "point B", "a": 4, "b": 5, "c": 6},
{"name": "point C", "a": 9, "b": 8, "c": 7}
]
}
],
"scales": [
{ "name": "xscale",
"type": "linear",
"domain": [0,10],
"range": [0,200]
},
{ "name": "yscale",
"type": "linear",
"domain": [0,10],
"range": [0,200]
}
],
"layout": {"padding": 20},
"marks": [
{ "name": "plot1",
"type": "group",
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "right", "scale": "yscale"}
],
"marks": [
{
"type": "symbol",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": "a", "scale": "xscale"},
"y": {"field": "b", "scale": "yscale"},
"tooltip": {"field": "name"}
},
"update": {
"size": {"value": 100},
"fill": {"value": "grey"}
}
}
}
]
},
{ "name": "plot2",
"type": "group",
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "right", "scale": "yscale"}
],
"marks": [
{
"type": "symbol",
"from": {"data": "table"},
"on": [
{
"trigger": "datapoint_is_activated",
"modify": "activated_datapoint",
"values": "{fill: \"red\"}"
},
{
"trigger": "!datapoint_is_activated",
"modify": "activated_datapoint",
"values": "{fill: \"grey\"}"
}
],
"encode": {
"enter": {
"x": {"field": "a", "scale": "xscale"},
"y": {"field": "c", "scale": "yscale"},
"size": {"value": 100},
"tooltip": {"field": "name"}
},
"update": {
"fill": {"value": "grey"}
}
}
}
]
}
]
}
The resulting image looks like this:
The idea is that hovering over a datapoint in the left plot will highlight the corresponding datapoint in the right plot. I know this is straightforward in vega-lite, but that does not (yet) support node-link diagrams. Hence: vega.
My approach in the code is to:
create a signal in the outer scope that tracks (a) if there is a point activated, and (b) which point this is
in plot 2 to have a trigger that takes the activated datapoint and changes its colour to 'red'.
I have an inkling feeling that it has to do with my definition of the modify and values part, but I can't figure it out.
Any help very much appreciated!
Thank you,
jan.
After a lot of trial and error, I did find a way to do this. Instead of using a trigger (which I believe would be the canonical way of doing this), I merely use a test in the fill section: datapoint_is_activated && datum === activated_datapoint.datum. See code and image below.
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"padding": 0,
"autosize": "none",
"width": 800,
"height": 400,
"signals": [
{ "description": "Any datapoint is activated",
"name": "datapoint_is_activated", "value": false,
"on": [
{
"events": "symbol:mouseover",
"update": "true"
},
{
"events": "symbol:mouseout",
"update": "false"
}
]
},
{ "description": "Active datapoint",
"name": "activated_datapoint", "value": null,
"on": [
{
"events": "symbol:mouseover",
"update": "item()"
},
{
"events": "symbol:mouseout",
"update": "null"
}
]
}
],
"data": [
{
"name": "table",
"values": [
{"name": "point A", "a": 2, "b": 2, "c": 4},
{"name": "point B", "a": 4, "b": 5, "c": 6},
{"name": "point C", "a": 5, "b": 3, "c": 5}
]
}
],
"scales": [
{ "name": "xscale",
"type": "linear",
"domain": [0,10],
"range": [0,200]
},
{ "name": "yscale",
"type": "linear",
"domain": [0,10],
"range": [0,200]
}
],
"layout": {"padding": 20},
"marks": [
{ "name": "plot1",
"type": "group",
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "right", "scale": "yscale"}
],
"marks": [
{
"type": "symbol",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": "a", "scale": "xscale"},
"y": {"field": "b", "scale": "yscale"},
"tooltip": {"field": "name"},
"size": {"value": 200}
},
"update": {
"fill": [
{"test": "datapoint_is_activated && datum === activated_datapoint.datum",
"value": "red"},
{"value": "lightgrey"}
]
}
}
}
]
},
{ "name": "plot2",
"type": "group",
"axes": [
{"orient": "bottom", "scale": "xscale"},
{"orient": "right", "scale": "yscale"}
],
"marks": [
{
"type": "symbol",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": "a", "scale": "xscale"},
"y": {"field": "c", "scale": "yscale"},
"size": {"value": 200},
"tooltip": {"field": "name"}
},
"update": {
"fill": [
{"test": "datapoint_is_activated && datum === activated_datapoint.datum",
"value": "red"},
{"value": "lightgrey"}
]
}
}
}
]
}
]
}