Related
I'm trying to create an indented tree e.g. as in https://observablehq.com/#d3/indented-tree
I think that what this example does which I can't replicate in vega is encapsulated in this code:
root = { let i = 0; return d3.hierarchy(data).eachBefore(d => d.index = i++); }
eachBefore is a pre-order traversal on the output of d3.hierarchy.
Is there any way to get this result from (upstream) vega, or is this a feature request for an index output from the tree transform? (/something similar, or else a custom transform)
By the way, I think it may be easy to turn the specific tree layout example into an indented tree because the id happens to give the same 'index' ordering (I think), but think we need to use eachBefore where the data isn't so conveniently ordered.
Thanks for any suggestions!
Declan
Update
I made a change in vega-hierarchy described here:
https://github.com/declann/vega/commit/a651ff36cd3f0897054aa1b236f82e701db62432
Now I can use pre_traversal_id from tree transform output to do indented trees, e.g.:
indented tree in (custom) vega-editor, with tree output including pre_traversal_id field
Modified spec: https://gist.github.com/declann/91fd150ae04016e5890a30295fa58a07
Not sure if this help, but, when I enter at vega.github.io/vega/examples/tree-layout I played with the controls and (after change the settings to: layout:tidy - links:orthogonal - separation:true) I got a similar result you shown in the observablehq page:
Open the Chart in the Vega Editor
Code:
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"description": "An example of Cartesian layouts for a node-link diagram of hierarchical data.",
"width": 600,
"height": 1600,
"padding": 5,
"signals": [
{
"name": "labels", "value": true
},
{
"name": "layout", "value": "tidy"
},
{
"name": "links", "value": "orthogonal"
}
],
"data": [
{
"name": "tree",
"url": "data/flare.json",
"transform": [
{
"type": "stratify",
"key": "id",
"parentKey": "parent"
},
{
"type": "tree",
"method": {"signal": "layout", "value": "tidy"},
"size": [{"signal": "height"}, {"signal": "width - 100"}],
"separation": true,
"as": ["y", "x", "depth", "children"]
}
]
},
{
"name": "links",
"source": "tree",
"transform": [
{ "type": "treelinks" },
{
"type": "linkpath",
"orient": "horizontal",
"shape": {"signal": "links"}
}
]
}
],
"scales": [
{
"name": "color",
"type": "linear",
"range": {"scheme": "greys"},
"domain": {"data": "tree", "field": "depth"},
"zero": true
}
],
"marks": [
{
"type": "path",
"from": {"data": "links"},
"encode": {
"update": {
"path": {"field": "path"},
"stroke": {"value": "#828282"}
}
}
},
{
"type": "symbol",
"from": {"data": "tree"},
"encode": {
"enter": {
"size": {"value": 25}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"fill": {"field": "depth"}
}
}
},
{
"type": "text",
"from": {"data": "tree"},
"encode": {
"enter": {
"text": {"field": "name"},
"baseline": {"value": "middle"}
},
"update": {
"x": {"field": "x"},
"y": {"field": "y"},
"dx": {"signal": "datum.children ? -7 : 7"},
"align": {"signal": "datum.children ? 'right' : 'left'"}
}
}
}
]
}
I believe this is possible by adding expression transforms for x (based on tree_node depth) and y (based on the tree node id)
The gist of it is to transform x and y after the tree transform
{"type": "stratify", "key": "id", "parentKey": "parent"},
{
"type": "tree",
"method": {"signal": "layout"},
"size": [{"signal": "height"}, {"signal": "width - 100"}],
"separation": {"signal": "separation"},
"as": ["_", "_", "depth", "children"]
},
{"type": "formula", "expr": "datum.depth * 20", "as": "x"},
{"type": "formula", "expr": "datum.id * 14", "as": "y"}
Here's an example that modifies the Vega Tree layout example
First of all, it is hard to describe what I exactly mean by "table based data", because in some way all the input data for vega is "table-ish", but this example should make it clear:
Most (if not all) of the Vega-Lite examples for multi line charts use data like,
"data": {
"values": [
{"id": 0, "symbol": "A", "value": 4},
{"id": 1, "symbol": "A", "value": 2},
{"id": 0, "symbol": "B", "value": 3},
{"id": 1, "symbol": "B", "value": 8}
]
}
which is simple to color the lines of A and B with an ecoding like this,
"mark": "line",
"encoding": {
"x": {"field": "id", "type": "quantitative"},
"y": {"field": "value", "type": "quantitative"},
"color": {"field": "symbol", "type": "nominal"}
}
But what if I want to produce the same result with a table based form of data like this,
"data": {
"values": [
{"id": 0, "A": 4, "B": 3},
{"id": 1, "A": 2, "B": 8}
]
}
1. How can I encode table based data into one colored multi line chart?
A basic encoding could be to create line charts for every field and layer them on top of each other like this,
"encoding": {
"x": {"field": "id", "type": "quantitative"}
},
"layer": [
{
"mark": "line",
"encoding": {
"y": {"field": "A", "type": "quantitative"}
}
},
{
"mark": "line",
"encoding": {
"y": {"field": "B", "type": "quantitative"}
}
}
]
But with this I don't know how to color the lines differently or how to create a legend.
2. Is this type of input data idiomatic to the way vega/vega-lite is designed?
The data that vega-lite works with is often known as "long-form" or "column-oriented" data. The type of data you're asking about is often known as "wide-form" or "row-oriented" data. This is discussed briefly in the documentation for Altair, a Python wrapper for vega-lite: https://altair-viz.github.io/user_guide/data.html#long-form-vs-wide-form-data
In the current release of Vega-Lite (v2.X) your only option is to modify the data source to be column-oriented with an external tool. This will change in the v3.0 release of Vega-Lite, which adds the Fold transform which is designed to convert row-oriented data to column-oriented within a chart specification.
So, in Vega-Lite 3, you could use the fold transform like this (vega editor link):
{
"data": {"values": [{"id": 0, "A": 4, "B": 3}, {"id": 1, "A": 2, "B": 8}]},
"transform": [{"fold": ["A", "B"]}],
"mark": "line",
"encoding": {
"x": {"field": "id", "type": "quantitative"},
"y": {"field": "value", "type": "quantitative"},
"color": {"field": "key", "type": "nominal"}
}
}
Another solution (a bit tedious) is to use layer and create n layers for n columns
{
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"data": {"url": "data/seattle-weather.csv", "format": {"type": "csv"}},
"layer": [{
"mark": {"type": "line", "color": "orange"},
"encoding": {
"x": {"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
"y": {"field": "temp_max", "type": "quantitative"}
}
}, {
"mark": {"type": "line", "color": "red"},
"encoding": {
"x": {"timeUnit": "yearmonthdate", "field": "date", "type": "temporal"},
"y": {"field": "temp_min", "type": "quantitative"}
}
}]
}
Future support for layer repeat (https://github.com/vega/vega-lite/issues/1274) may make this a more reasonable solution.
i whant to make every bar in chart - in different collor. please help.
so default chart like described in docs:
https://vega.github.io/vega/examples/bar-chart/
for example - i whant to make like: blue, red, yellow, green, blue, blue.
please help! thanks!
Using the Vega bar chart example you linked to:
First, create a scale to define the colors:
{
"name": "colors",
"type": "ordinal",
"domain": {"data": "table", "field": "category"},
"range": {"scheme": "category10"}
}
The value for the "scheme" can be a built-in scheme from Vega, or a custom scheme.
Then, you need to specify the color for the fill on the mark's update property set, specifying the name of the scheme:
"update": {
"fill": {"scale": "colors", "field": "category"}
},
You can define a categorical color scale and use it to change the color of the bar mark. Check out some of the other examples that use categorical color scales.
I had the same problem and with a little help from https://vega.github.io/vega-lite/examples/bar_color_disabled_scale.html I got to the following - the idea is to encode the color in the data:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": "A", "b": 28, "c": "black"},
{"a": "B", "b": 55, "c": "black"},
{"a": "C", "b": 43, "c": "black"},
{"a": "D", "b": 91, "c": "red"}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "ordinal"},
"y": {"field": "b", "type": "quantitative"},
"color": {"field": "c", "type": "nominal", "scale": null}
}
}
alternatively this also works and keeps the legend if you need it:
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": "A", "b": 28},
{"a": "B", "b": 55},
{"a": "C", "b": 43},
{"a": "D", "b": 91}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "ordinal", "sort": ["C", "A", "B", "D"]},
"y": {"field": "b", "type": "quantitative"},
"color": {"field": "a", "type": "nominal", "sort": ["C", "A", "B", "D"], "scale": {"range": ["red", "black", "black", "black", "black"]}}
}
}
For bar charts
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A bar chart that directly encodes color names in the data.",
"data": {
"values": [
{
"color": "yellow",
"b": 28
},
{
"color": "green",
"b": 55
},
{
"color": "blue",
"b": 43
}
]
},
"mark": "bar",
"encoding": {
"x": {
"field": "color",
"type": "nominal"
},
"y": {
"field": "b",
"type": "quantitative"
},
"color": {
"field": "color",
"type": "nominal",
"scale": null
}
}
}
For Pie charts
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 200,
"height": 200,
"autosize": "none",
"signals": [
{
"name": "startAngle", "value": 0,
"bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
},
{
"name": "endAngle", "value": 6.29,
"bind": {"input": "range", "min": 0, "max": 6.29, "step": 0.01}
},
{
"name": "padAngle", "value": 0,
"bind": {"input": "range", "min": 0, "max": 0.1}
},
{
"name": "innerRadius", "value": 0,
"bind": {"input": "range", "min": 0, "max": 90, "step": 1}
},
{
"name": "cornerRadius", "value": 0,
"bind": {"input": "range", "min": 0, "max": 10, "step": 0.5}
},
{
"name": "sort", "value": false,
"bind": {"input": "checkbox"}
}
],
"data": [
{
"name": "table",
"values": [
{"field": 10,"color": "blue"},
{"field": 3,"color": "green"},
{"field": 7,"color": "red"},
{"field": 8,"color":"orange"},
{"field": 15,"color":"yellow"}
],
"transform": [
{
"type": "pie",
"field": "field",
"startAngle": {"signal": "startAngle"},
"endAngle": {"signal": "endAngle"},
"sort": {"signal": "sort"}
}
]
}
],
"scales": [
{
"name": "color",
"type": "ordinal",
"domain": {"data": "table", "field": "color"},
"range": {"scheme": "category20"}
}
],
"marks": [
{
"type": "arc",
"from": {"data": "table"},
"encode": {
"enter": {
"fill": { "field": "color",
"type": "nominal",
"scale": null},
"x": {"signal": "width / 2"},
"y": {"signal": "height / 2"}
},
"update": {
"startAngle": {"field": "startAngle"},
"endAngle": {"field": "endAngle"},
"padAngle": {"signal": "padAngle"},
"innerRadius": {"signal": "innerRadius"},
"outerRadius": {"signal": "width / 2"},
"cornerRadius": {"signal": "cornerRadius"}
}
}
}
]
}
The vega documentation/tutorials suggest that setting up a horizontal bar chart is easy, but I can't figure it out for the life of me. How would I go about changing a vertical bar chart, such as http://vega.github.io/vega-editor/index.html?spec=bar, to a horizontal one?
Like this
{
"width": 200,
"height": 210,
"padding": "auto",
"data": [
{
"name": "raw",
"values": [
{"x": "A","y": 28},
{"x": "B","y": 55},
{"x": "C","y": 43},
{"x": "D","y": 91},
{"x": "E","y": 81},
{"x": "F","y": 53},
{"x": "G","y": 19},
{"x": "H","y": 87},
{"x": "I","y": 52}
],
"format": {"parse": {"y": "number"}},
"transform": [{"type": "filter","test": "(d.data.y!==null)"}]
}
],
"scales": [],
"marks": [
{
"_name": "cell",
"type": "group",
"properties": {
"enter": {"width": {"value": 200},"height": {"value": 210}}
},
"scales": [
{
"name": "x",
"type": "linear",
"domain": {"data": "raw","field": "data.y"},
"range": [0,200],
"zero": true,
"reverse": false,
"round": true,
"nice": true
},
{
"name": "y",
"type": "ordinal",
"domain": {"data": "raw","field": "data.x"},
"sort": true,
"range": [0,210],
"bandWidth": 21,
"round": true,
"nice": true,
"points": true,
"padding": 1
}
],
"axes": [
{
"type": "x",
"scale": "x",
"properties": {
"grid": {
"stroke": {"value": "#000000"},
"opacity": {"value": 0.08}
}
},
"layer": "back",
"format": "",
"ticks": 5,
"titleOffset": 38,
"grid": true,
"title": "y"
},
{
"type": "y",
"scale": "y",
"properties": {
"labels": {"text": {"template": "{{data | truncate:25}}"}},
"grid": {
"stroke": {"value": "#000000"},
"opacity": {"value": 0.08}
}
},
"layer": "back",
"titleOffset": 28,
"grid": true,
"title": "x"
}
],
"marks": [
{
"type": "rect",
"from": {"data": "raw"},
"properties": {
"enter": {
"x": {"scale": "x","field": "data.y"},
"x2": {"value": 0},
"yc": {"scale": "y","field": "data.x"},
"height": {"value": 21,"offset": -1},
"fill": {"value": "#4682b4"}
},
"update": {
"x": {"scale": "x","field": "data.y"},
"x2": {"value": 0},
"yc": {"scale": "y","field": "data.x"},
"height": {"value": 21,"offset": -1},
"fill": {"value": "#4682b4"}
}
}
}
],
"legends": []
}
]
}
I generated this chart from
{
"data": {
"values": [
{"x":"A", "y":28}, {"x":"B", "y":55}, {"x":"C", "y":43},
{"x":"D", "y":91}, {"x":"E", "y":81}, {"x":"F", "y":53},
{"x":"G", "y":19}, {"x":"H", "y":87}, {"x":"I", "y":52}
]
},
"marktype": "bar",
"encoding": {
"y": {"type": "O","name": "x"},
"x": {"type": "Q","name": "y"}
}
}
with vega-lite (https://vega.github.io/vega-lite/).
How can I replace the color in the pie chart?
Please find my code below :
AmCharts.makeChart("pie-diagramm", {
"type": "pie",
"theme": "none",
"dataProvider": [{
"title": "Интернэшнл",
"value": 100
}, {
"title": "Казахстан",
"value": 90
},{
"title": "Лтд",
"value": 100
},
{
"title": "Лимитед",
"value": 400},
{
"title": "компания",
"value": 400},
{
"title": "ЛУКАРКО",
"value": 600},{
"title": "Компани",
"value": 700
},
{
"title": "«КазМунайГаз»",
"value": 900},{
"title": "КТК",
"value": 350},
{
"title": "Федерация",
"value": 1300},
{
"title": "Пайплайн",
"value": 90}
],
"titleField": "title",
"valueField": "value",
"labelRadius": 5,
"fontSize": 16,
"radius": "25%",
"innerRadius": "30%",
"labelText": "[[title]]",
"exportConfig": {
"menuItems": [{
"icon": '/lib/3/images/export.png',
"format": 'png'
}]
}
});
Thanks!
add color attribute in your DataProvider
dataProvider": [{
"title": "Интернэшнл",
"value": 100,
"color": "your choice of color"
}, {
"title": "Казахстан",
"value": 90,
"color": "your choice of color"
}
http://docs.amcharts.com/3/javascriptcharts/AmPieChart,
http://docs.amcharts.com/3/javascriptcharts/Slice
the dataprovider holds the property of each slice in pie chart
chart.chartData = dataProvider;
The best and most simple way to do this would be setting background-color style of your chart's container div. Or you can set chart.backgroundColor="#FF0000"; and chart.backgroundAlpha to some bigger than 0 value.