Vega-lite difference between Firefox and Chrome - vega

I have a vega-lite chart that shows up as expected in Chrome (72.0.3626.96), but not in Firefox (70.0.1). I have checked the spec in the Vega Editor. Does anyone know why this might be?
Here are the rendered charts:
Firefox:
Chrome:
Here is the spec:
Any help you might be able to give would be much appreciated.
Apologies, but I do not know how to collapse this code.
{
"$schema": "https://vega.github.io/schema/vega-lite/v3.2.1.json",
"background": "white",
"config": {"mark": {"tooltip": null}, "view": {"height": 300, "width": 400}},
"datasets": {
"data-511198e25d4dbee99248144390684caa": [
{
"counts": 338,
"filter_method": "greater than",
"grade": "9",
"index": 3,
"perc": 0.2669826224328594,
"school_code": "Board",
"threshold": "8",
"year": 20172018,
"year_lab": "2017/18",
"year_lab_q": "2017"
},
{
"counts": 414,
"filter_method": "greater than",
"grade": "9",
"index": 4,
"perc": 0.30689399555226093,
"school_code": "Board",
"threshold": "8",
"year": 20182019,
"year_lab": "2018/19",
"year_lab_q": "2018"
}
],
"data-72a083843a98847e44077116c495e448": [
{
"counts": 49,
"filter_method": "greater than",
"grade": "9",
"index": 0,
"perc": 0.3356164383561644,
"school_code": "KING",
"threshold": "8",
"year": 20142015,
"year_lab": "2014/15",
"year_lab_q": "2014"
},
{
"counts": 62,
"filter_method": "greater than",
"grade": "9",
"index": 5,
"perc": 0.3668639053254438,
"school_code": "MLTS",
"threshold": "8",
"year": 20162017,
"year_lab": "2016/17",
"year_lab_q": "2016"
},
{
"counts": 53,
"filter_method": "greater than",
"grade": "9",
"index": 6,
"perc": 0.29608938547486036,
"school_code": "KING",
"threshold": "8",
"year": 20172018,
"year_lab": "2017/18",
"year_lab_q": "2017"
},
{
"counts": 44,
"filter_method": "greater than",
"grade": "9",
"index": 7,
"perc": 0.25882352941176473,
"school_code": "MLTS",
"threshold": "8",
"year": 20172018,
"year_lab": "2017/18",
"year_lab_q": "2017"
},
{
"counts": 53,
"filter_method": "greater than",
"grade": "9",
"index": 8,
"perc": 0.3212121212121212,
"school_code": "KING",
"threshold": "8",
"year": 20182019,
"year_lab": "2018/19",
"year_lab_q": "2018"
},
{
"counts": 61,
"filter_method": "greater than",
"grade": "9",
"index": 9,
"perc": 0.25206611570247933,
"school_code": "MLTS",
"threshold": "8",
"year": 20182019,
"year_lab": "2018/19",
"year_lab_q": "2018"
}
]
},
"height": 400,
"layer": [
{
"data": {"name": "data-72a083843a98847e44077116c495e448"},
"encoding": {
"color": {
"field": "school_code",
"legend": {"labelFontSize": 15, "titleFontSize": 20},
"title": null,
"type": "nominal"
},
"tooltip": [
{
"field": "perc",
"format": ".2%",
"title": "percentage",
"type": "quantitative"
},
{
"field": "counts",
"title": "number",
"type": "quantitative"
},
{"field": "year_lab", "title": "school year", "type": "nominal"},
{"field": "school_code", "title": "level", "type": "nominal"},
{"field": "grade", "type": "nominal"},
{"field": "filter_method", "type": "nominal"},
{"field": "threshold", "type": "nominal"}
],
"x": {
"axis": {"format": "%Y", "tickCount": 5},
"field": "year_lab_q",
"scale": {"domain": ["2013.9", "2018.5"]},
"title": "School Year (beginning)",
"type": "temporal"
},
"y": {
"axis": {"format": ".0%"},
"field": "perc",
"title": "Percentage",
"type": "quantitative"
}
},
"mark": {"point": true, "type": "line"},
"selection": {
"selector001": {
"bind": "scales",
"encodings": ["x", "y"],
"type": "interval"
}
}
},
{
"data": {"name": "data-511198e25d4dbee99248144390684caa"},
"encoding": {
"color": {
"field": "school_code",
"legend": {"labelFontSize": 15, "titleFontSize": 20},
"scale": {"domain": ["Board"], "range": ["black"]},
"title": null,
"type": "nominal"
},
"tooltip": [
{
"field": "perc",
"format": ".2%",
"title": "percentage",
"type": "quantitative"
},
{
"field": "counts",
"title": "number",
"type": "quantitative"
},
{"field": "year_lab", "title": "school year", "type": "nominal"},
{"field": "school_code", "title": "level", "type": "nominal"},
{"field": "grade", "type": "nominal"},
{"field": "filter_method", "type": "nominal"},
{"field": "threshold", "type": "nominal"}
],
"x": {"field": "year_lab_q", "type": "temporal"},
"y": {"field": "perc", "type": "quantitative"}
},
"mark": {"point": true, "type": "line"}
}
],
"resolve": {"scale": {"color": "independent"}},
"title": "A title!",
"width": 700
}

It appears that your temporal values are not being parsed correctly in firefox (details of javascript date parsing behavior is often browser-dependent). You could try forcing the correct parsing by changing your data specification (in both places) to:
"data": {
"name": "data-72a083843a98847e44077116c495e448",
"format": {"parse": {"year_lab_q": "date:%Y"}}
}
This should ensure that the year string is parsed as a year, rather than e.g. a unix timestamp.
The other place date parsing is happening is in your domain specification. You might try changing those to a more standard time format, e.g.
"domain": ["2013-11-01", "2018-06-01"]

Related

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
}

Signal that filters the data not work correctly in 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}"
}
}
}
}
]
}

Stacked barchart in Vega

I am trying to make a stacked barchart in Vega and have hit a wall where I swear that my code should work, based on the several examples I've been cross referencing, but instead I keep getting perfect axes...with no bars.
The way it's supposed to work in this example is that Alamo's "Operations" "Debt" and "Below Cap" should stack, with State Average having the same categories of data stacked in the same order next to it. Both should sum to the same level (1). The transformed data looks to be in good shape, as the y0/y1 and order are correct.
I initially assumed the problem is in the marks section since that's where the bars are defined but I can't identify what the issue is there, so now I'm wondering if it's something else. But I don't know what else to try.
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A stacked bar chart describing ad valorem taxes in Texas community college districts.",
"width": 300,
"height": 300,
"padding": 5,
"background": "white",
"style": "cell",
"data": [
{
"name": "source_0",
"values": [
{
"name": "Operations (MO)",
"amount": 0.1078,
"district": "Alamo",
"sort": 1,
"y0": 0,
"y1": 0.1078
},
{
"name": "Debt (IS)",
"amount": 0.0414,
"district": "Alamo",
"sort": 2,
"y0": 0.1078,
"y1": 0.1492
},
{
"name": "Below Cap",
"amount": 0.8508,
"district": "Alamo",
"sort": 3,
"y0": 0.1492,
"y1": 1
},
{
"name": "Operations (MO)",
"amount": 0.152,
"district": "State Average",
"sort": 1,
"y0": 0,
"y1": 0.152
},
{
"name": "Debt (IS)",
"amount": 0.027,
"district": "State Average",
"sort": 2,
"y0": 0.152,
"y1": 0.179
},
{
"name": "Below Cap",
"amount": 0.821,
"district": "State Average",
"sort": 3,
"y0": 0.179,
"y1": 1
}
],
"format": {"type": "json"},
"transform": [
{
"type": "stack",
"groupby": ["district"],
"sort": {"field": "sort"},
"field": "amount"
}
]
}
],
"scales": [
{
"name": "x",
"type": "band",
"range": "width",
"domain": {"data": "source_0", "field": "district"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": true,
"domain": {"data": "source_0", "field": "y1"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "source_0", "field": "name"}
}
],
"axes": [
{"orient": "bottom", "scale": "x", "zindex": 1},
{"orient": "left", "scale": "y", "zindex": 1}
],
"marks": [
{
"type": "rect",
"from": {"data": "source_0"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "district"},
"width": {"scale": "x", "band": 1},
"y": {"scale": "y", "field": "y1"},
"y2": {"scale": "y", "value": "y0"},
"fill": {"scale": "color", "field": "name"}
}
}
}
]
}
Change your y2 from value to field. Editor
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "A stacked bar chart describing ad valorem taxes in Texas community college districts.",
"width": 300,
"height": 300,
"padding": 5,
"background": "white",
"style": "cell",
"data": [
{
"name": "source_0",
"values": [
{
"name": "Operations (MO)",
"amount": 0.1078,
"district": "Alamo",
"sort": 1,
"y0": 0,
"y1": 0.1078
},
{
"name": "Debt (IS)",
"amount": 0.0414,
"district": "Alamo",
"sort": 2,
"y0": 0.1078,
"y1": 0.1492
},
{
"name": "Below Cap",
"amount": 0.8508,
"district": "Alamo",
"sort": 3,
"y0": 0.1492,
"y1": 1
},
{
"name": "Operations (MO)",
"amount": 0.152,
"district": "State Average",
"sort": 1,
"y0": 0,
"y1": 0.152
},
{
"name": "Debt (IS)",
"amount": 0.027,
"district": "State Average",
"sort": 2,
"y0": 0.152,
"y1": 0.179
},
{
"name": "Below Cap",
"amount": 0.821,
"district": "State Average",
"sort": 3,
"y0": 0.179,
"y1": 1
}
],
"format": {"type": "json"},
"transform": [
{
"type": "stack",
"groupby": ["district"],
"sort": {"field": "sort"},
"field": "amount"
}
]
}
],
"scales": [
{
"name": "x",
"type": "band",
"range": "width",
"domain": {"data": "source_0", "field": "district"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": true,
"domain": {"data": "source_0", "field": "y1"}
},
{
"name": "color",
"type": "ordinal",
"range": "category",
"domain": {"data": "source_0", "field": "name"}
}
],
"axes": [
{"orient": "bottom", "scale": "x", "zindex": 1},
{"orient": "left", "scale": "y", "zindex": 1}
],
"marks": [
{
"type": "rect",
"from": {"data": "source_0"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "district"},
"width": {"scale": "x", "band": 1},
"y": {"scale": "y", "field": "y1"},
"y2": {"scale": "y", "field": "y0"},
"fill": {"scale": "color", "field": "name"}
}
}
}
]
}

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"}
]
}
}
}
]
}
]
}