Created
August 11, 2024 09:07
-
-
Save maxanatsko/22a3017e787828a134e932e26541c74f to your computer and use it in GitHub Desktop.
Categorial Treemap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"$schema": "https://vega.github.io/schema/vega/v5.json", | |
"description": "Treemap visualization using Deneb with Power BI data.", | |
"autosize": { | |
"type": "fit", | |
"contains": "padding" | |
}, | |
"padding": 0, | |
"signals": [ | |
{ | |
"name": "layout", | |
"value": "squarify" | |
}, | |
{ | |
"name": "aspectRatio", | |
"value": 1 | |
} | |
], | |
"data": [ | |
{ | |
"name": "dataset", | |
"values": [ | |
{ | |
"Ticker": "AAPL", | |
"Category": "Technology", | |
"Subcategory": "Consumer Electronics", | |
"Market Cap": 3287742584880, | |
"% Change": 0.019519094766619588 | |
}, | |
{ | |
"Ticker": "AMD", | |
"Category": "Technology", | |
"Subcategory": "Semiconductors", | |
"Market Cap": 217313557328.15, | |
"% Change": -0.0031182715866061883 | |
}, | |
{ | |
"Ticker": "AMZN", | |
"Category": "Consumer Discretionary", | |
"Subcategory": "E-commerce and Cloud Computing", | |
"Market Cap": 1752129935114.14, | |
"% Change": 0.0032451923076922597 | |
}, | |
{ | |
"Ticker": "AVGO", | |
"Category": "Technology", | |
"Subcategory": "Semiconductors", | |
"Market Cap": 690313063292.3999, | |
"% Change": 0.028155339805825262 | |
}, | |
{ | |
"Ticker": "DIS", | |
"Category": "Communication Services", | |
"Subcategory": "Entertainment", | |
"Market Cap": 156349368029.8, | |
"% Change": 0.0009288285150353919 | |
}, | |
{ | |
"Ticker": "GOOGL", | |
"Category": "Communication Services", | |
"Subcategory": "Internet Content and Information", | |
"Market Cap": 2014777699999.9998, | |
"% Change": 0.022873570401849865 | |
}, | |
{ | |
"Ticker": "INTC", | |
"Category": "Technology", | |
"Subcategory": "Semiconductors", | |
"Market Cap": 8390292559.96, | |
"% Change": -0.018426294820717004 | |
}, | |
{ | |
"Ticker": "META", | |
"Category": "Technology", | |
"Subcategory": "Social Media and Internet Services", | |
"Market Cap": 1390863220200.81, | |
"% Change": 0.01981444601120719 | |
}, | |
{ | |
"Ticker": "MU", | |
"Category": "Technology", | |
"Subcategory": "Semiconductors", | |
"Market Cap": 103073823678.8, | |
"% Change": 0.01405381489747053 | |
}, | |
{ | |
"Ticker": "NFLX", | |
"Category": "Communication Services", | |
"Subcategory": "Entertainment", | |
"Market Cap": null, | |
"% Change": 0.017575626107963283 | |
}, | |
{ | |
"Ticker": "NVDA", | |
"Category": "Technology", | |
"Subcategory": "Semiconductors", | |
"Market Cap": 2576676321357.5, | |
"% Change": -0.00842483907610754 | |
}, | |
{ | |
"Ticker": "TSLA", | |
"Category": "Consumer Cyclical", | |
"Subcategory": "Auto Manufacturers", | |
"Market Cap": 637839233400, | |
"% Change": 0.01497081958893766 | |
}, | |
{ | |
"Ticker": "TXN", | |
"Category": "Technology", | |
"Subcategory": "Semiconductors", | |
"Market Cap": 175496564548.23, | |
"% Change": 0.006229714166055898 | |
} | |
], | |
"transform": [ | |
{ | |
"type": "nest", | |
"generate": true, | |
"keys": [ | |
"Category", | |
"Subcategory" | |
] | |
}, | |
{ | |
"type": "treemap", | |
"field": "Market Cap", | |
"sort": { | |
"field": "Market Cap" | |
}, | |
"round": true, | |
"method": { | |
"signal": "layout" | |
}, | |
"ratio": { | |
"signal": "aspectRatio" | |
}, | |
"size": [ | |
{ | |
"signal": "width" | |
}, | |
{ | |
"signal": "height" | |
} | |
] | |
} | |
] | |
}, | |
{ | |
"name": "nodes", | |
"source": "dataset", | |
"transform": [ | |
{ | |
"type": "filter", | |
"expr": "datum.children" | |
} | |
] | |
}, | |
{ | |
"name": "leaves", | |
"source": "dataset", | |
"transform": [ | |
{ | |
"type": "filter", | |
"expr": "!datum.children" | |
} | |
] | |
} | |
], | |
"scales": [ | |
{ | |
"name": "color", | |
"type": "linear", | |
"domain": [ | |
-0.03, | |
-0.02, | |
-0.01, | |
0, | |
0.01, | |
0.02, | |
0.03 | |
], | |
"range": [ | |
"#e44841", | |
"#aa4d46", | |
"#814753", | |
"#262930", | |
"#487753", | |
"#559c4e", | |
"#68c866" | |
] | |
}, | |
{ | |
"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] | |
} | |
], | |
"layout": { | |
"padding": 5, | |
"bounds": "flush", | |
"center": true, | |
"headerBand": 1 | |
}, | |
"marks": [ | |
// { | |
// "type": "rect", | |
// "from": {"data": "nodes"}, | |
// "interactive": false, | |
// "encode": { | |
// "enter": { | |
// "fill": {"scale": "color", "field": "Category"} | |
// }, | |
// "update": { | |
// "x": {"field": "x0"}, | |
// "y": {"field": "y0"}, | |
// "x2": {"field": "x1"}, | |
// "y2": {"field": "y1"} | |
// } | |
// } | |
// }, | |
{ | |
"type": "group", | |
"from": { | |
"facet": { | |
"data": "leaves", | |
"name": "facet", | |
"groupby": [ "Category", "Subcategory" ] | |
} | |
}, | |
"encode": { | |
"enter": { | |
"x": { | |
"field": "x" | |
}, | |
"y": { | |
"field": "y" | |
} | |
} | |
}, | |
"marks": [ | |
// Treemap Content | |
{ | |
"type": "rect", | |
"from": { | |
"data": "facet" | |
}, | |
"encode": { | |
"enter": { | |
"fill": { | |
"scale": "color", | |
"field": "% Change" | |
}, | |
"stroke": { | |
"value": "#262930" | |
} | |
}, | |
"update": { | |
"x": { | |
"field": "x0" | |
}, | |
"y": { | |
"signal": "datum.y0 + 30" | |
}, // Adjust for header height | |
"x2": { | |
"field": "x1" | |
}, | |
"y2": { | |
"field": "y1" | |
} | |
} | |
} | |
}, | |
{ | |
"type": "text", | |
"from": { | |
"data": "facet" | |
}, | |
"interactive": false, | |
"encode": { | |
"enter": { | |
"align": { | |
"value": "center" | |
}, | |
"baseline": { | |
"value": "middle" | |
}, | |
"fill": { | |
"value": "#fff" | |
}, | |
"text": { | |
"field": "Ticker" | |
}, | |
"fontSize": { | |
"value": 36 | |
}, | |
"fontWeight": { | |
"value": "bold" | |
} | |
}, | |
"update": { | |
"x": { | |
"signal": "0.5 * (datum.x0 + datum.x1)" | |
}, | |
"y": { | |
"signal": "datum.y0 + 0.5 * (datum.y1 - datum.y0) - 10" | |
} | |
} | |
} | |
}, | |
{ | |
"type": "text", | |
"from": { | |
"data": "facet" | |
}, | |
"interactive": false, | |
"encode": { | |
"enter": { | |
"align": { | |
"value": "center" | |
}, | |
"baseline": { | |
"value": "middle" | |
}, | |
"fill": { | |
"value": "#fff" | |
}, | |
"text": { | |
"signal": "datum['% Change']" | |
}, | |
"fontSize": { | |
"value": 20 | |
} | |
}, | |
"update": { | |
"x": { | |
"signal": "0.5 * (datum.x0 + datum.x1)" | |
}, | |
"y": { | |
"signal": "datum.y0 + 0.5 * (datum.y1 - datum.y0) + 20" | |
} | |
} | |
} | |
} | |
] | |
} | |
,{ | |
"type": "text", | |
"from": {"data": "nodes"}, | |
"interactive": false, | |
"encode": { | |
"enter": { | |
"align": {"value": "center"}, | |
"baseline": {"value": "middle" }, | |
"fill": {"value": "#fff"}, | |
"text": {"field": "key"}, | |
"fontSize": {"scale": "size", "field": "depth"}, | |
"fillOpacity": {"scale": "opacity", "field": "depth"} | |
}, | |
"update": { | |
"x": {"signal": "0.5 * (datum.x0 + datum.x1)"}, | |
"y": {"signal": "0.5 * (datum.y0 + datum.y1)"} | |
} | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment