This is a variation of the Vega Tree-Layout Example, where I have introduced a "legends" element to the spec.
"legends": [
{"fill": "color", "type": "symbol", "title": "depth"}
]| license: mit | |
| height: 500 | |
| scrolling: yes | |
| border: yes | |
This is a variation of the Vega Tree-Layout Example, where I have introduced a "legends" element to the spec.
"legends": [
{"fill": "color", "type": "symbol", "title": "depth"}
]| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <!-- uploaded using vegawidget --> | |
| <script src="https://cdn.jsdelivr.net/npm/vega@4.0.0-rc.3"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/vega-lite@2.6.0"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/vega-embed@3.16.0"></script> | |
| <link rel="stylesheet" type="text/css" href="vega-embed.css"> | |
| </head> | |
| <body> | |
| <div id="vis"></div> | |
| <script type="text/javascript"> | |
| const spec = "spec.json"; | |
| const opt = {"defaultStyle":true,"renderer":"canvas"}; | |
| vegaEmbed('#vis', spec, opt).then(function(result) { | |
| // access view as result.view | |
| }).catch(console.error); | |
| </script> | |
| </body> | |
| </html> |
| { | |
| "$schema": "https://vega.github.io/schema/vega/v4.json", | |
| "width": 600, | |
| "height": 1600, | |
| "padding": 5, | |
| "signals": [ | |
| { | |
| "name": "labels", | |
| "value": true, | |
| "bind": { | |
| "input": "checkbox" | |
| } | |
| }, | |
| { | |
| "name": "layout", | |
| "value": "tidy", | |
| "bind": { | |
| "input": "radio", | |
| "options": [ | |
| "tidy", | |
| "cluster" | |
| ] | |
| } | |
| }, | |
| { | |
| "name": "links", | |
| "value": "diagonal", | |
| "bind": { | |
| "input": "select", | |
| "options": [ | |
| "line", | |
| "curve", | |
| "diagonal", | |
| "orthogonal" | |
| ] | |
| } | |
| } | |
| ], | |
| "data": [ | |
| { | |
| "name": "tree", | |
| "url": "https://raw.githubusercontent.com/vega/vega/master/docs/data/flare.json", | |
| "transform": [ | |
| { | |
| "type": "stratify", | |
| "key": "id", | |
| "parentKey": "parent" | |
| }, | |
| { | |
| "type": "tree", | |
| "method": { | |
| "signal": "layout" | |
| }, | |
| "size": [ | |
| { | |
| "signal": "height" | |
| }, | |
| { | |
| "signal": "width - 100" | |
| } | |
| ], | |
| "as": [ | |
| "y", | |
| "x", | |
| "depth", | |
| "children" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "name": "links", | |
| "source": "tree", | |
| "transform": [ | |
| { | |
| "type": "treelinks" | |
| }, | |
| { | |
| "type": "linkpath", | |
| "orient": "horizontal", | |
| "shape": { | |
| "signal": "links" | |
| } | |
| } | |
| ] | |
| } | |
| ], | |
| "scales": [ | |
| { | |
| "name": "color", | |
| "type": "sequential", | |
| "range": { | |
| "scheme": "magma" | |
| }, | |
| "domain": { | |
| "data": "tree", | |
| "field": "depth" | |
| }, | |
| "zero": true | |
| } | |
| ], | |
| "marks": [ | |
| { | |
| "type": "path", | |
| "from": { | |
| "data": "links" | |
| }, | |
| "encode": { | |
| "update": { | |
| "path": { | |
| "field": "path" | |
| }, | |
| "stroke": { | |
| "value": "#ccc" | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| "type": "symbol", | |
| "from": { | |
| "data": "tree" | |
| }, | |
| "encode": { | |
| "enter": { | |
| "size": { | |
| "value": 100 | |
| }, | |
| "stroke": { | |
| "value": "#fff" | |
| } | |
| }, | |
| "update": { | |
| "x": { | |
| "field": "x" | |
| }, | |
| "y": { | |
| "field": "y" | |
| }, | |
| "fill": { | |
| "scale": "color", | |
| "field": "depth" | |
| } | |
| } | |
| } | |
| }, | |
| { | |
| "type": "text", | |
| "from": { | |
| "data": "tree" | |
| }, | |
| "encode": { | |
| "enter": { | |
| "text": { | |
| "field": "name" | |
| }, | |
| "fontSize": { | |
| "value": 9 | |
| }, | |
| "baseline": { | |
| "value": "middle" | |
| } | |
| }, | |
| "update": { | |
| "x": { | |
| "field": "x" | |
| }, | |
| "y": { | |
| "field": "y" | |
| }, | |
| "dx": { | |
| "signal": "datum.children ? -7 : 7" | |
| }, | |
| "align": { | |
| "signal": "datum.children ? 'right' : 'left'" | |
| }, | |
| "opacity": { | |
| "signal": "labels ? 1 : 0" | |
| } | |
| } | |
| } | |
| } | |
| ], | |
| "legends": [ | |
| { | |
| "fill": "color", | |
| "type": "symbol", | |
| "title": "depth" | |
| } | |
| ] | |
| } |