Forum Discussion
Dened - Custom Matrix with double headers
Hi dm-p,
I hope you can help me here to deliver a matrix view in Deneb to look like this:
I aim to show the Category and Sub-category at the top of the matrix and colour the cells based on the values.
So far, here is my code:
{
"data": {"name": "dataset"},
"encoding": {
"y": {
"field": "Product",
"title": null
},
"x": {
"field": "Sub Cat",
"axis": {
"orient": "top"
}
}
},
"layer": [
{
"mark": {"type": "rect"},
"encoding": {
"color": {
"field": "Value",
"scale": {
"scheme": "lightgreyred"
},
"legend": null
}
}
},
{
"mark": {
"type": "text",
"tooltip": true,
"fontsize": 14
},
"encoding": {
"text": {
"field": "Value",
"type": "quantitative",
"format": ",.0f"
},
"color": {"value": "black"}
}
}
]
}And below is the output I got so far:
I was hoping that when defining the "x" "field", I could add multiple fields ... but I am unsure how to do it.
Many thanks
Hey H_insight. I believe I got everything implemented. Take a look and let me know.
Notes:
- When adjusting the size of the visual, you'll want to adjust the step width/height and the xOffset/yOffset for the rects sitting behind the row and column headers. You'll also want to adjust font size accordingly.
- Although there are alternatives, I added a calculated column to implement the logic for a custom sort order. The column is called "Sort Order". Here you should be able to implement any logic you'd like to determine the sorting of the products.
Spec:
{ "data": {"name": "dataset"}, "facet": { "column": { "field": "Category", "title": null, "header": {"labelFontSize": 14} } }, "spec": { "width": {"step": 70}, "height": {"step": 25}, "encoding": { "y": { "field": "Product", "title": null, "sort": {"field": "Sort Order"}, "axis": { "labelColor": "white", "labelFontWeight": 700 } }, "x": { "field": "Sub Cat", "axis": { "orient": "top", "labelAngle": 0, "labelColor": "white", "labelFontWeight": 700 }, "title": null } }, "layer": [ { "description": "x-axis background rects", "mark": {"type": "rect"}, "encoding": { "y": {"value": 0}, "yOffset": {"value": -25}, "color": { "condition": { "test": "datum['Sub Cat'] == 'Overall'", "value": "#C45920" }, "value": "#325964" } } }, { "description": "y-axis background rects", "mark": { "type": "rect", "opacity": 0.5 }, "encoding": { "x": {"value": 0}, "xOffset": {"value": -80}, "color": {"value": "black"} } }, { "description": "matrix value rects", "mark": {"type": "rect"}, "encoding": { "color": { "field": "Value", "type": "nominal", "scale": { "domain": [1, 2, 3, 4, 5], "range": [ "grey", "amber", "green", "red", "lightBlue" ] }, "legend": null } } }, { "mark": { "type": "text", "tooltip": true, "size": 14 }, "encoding": { "text": { "field": "Value", "type": "quantitative", "format": ",.0f" }, "color": { "field": "Value", "type": "quantitative", "scale": { "domain": [1, 2, 3, 4, 5], "range": [ "white", "white", "white", "white", "black" ] }, "legend": null } } } ] }, "resolve": { "scale": { "x": "independent", "y": "independent" } } }
.pbix here
9 Replies
- giammariamSolution Sage
Hi H_insight. Looks like you just need to implement faceting. I'd be happy to take a look but I can't access your sample file.
- H_insightHelper V
- giammariamSolution Sage
Alright, I made a few updates. The first one is the main part that gives you the matrices side-by-side.
- Implemented column faceting on category
- Added a transform to correctly sort the y-axis alphabetically based on the product number
- Rotated the x-axis labels to have an angle of 0
* Important note - with layout composition (in this case, faceting), you have to specifiy the width and height to be used for the individual charts. This means that if you resize the visual in the Power BI formatting properties, you'll want to update the width and height in the spec as well.
Let me know if this is what you are after. Happy to help if you have additional questions (I'm looking for Deneb/Vega/Vega-Lite practice).
Spec:
{ "data": {"name": "dataset"}, "transform": [ { "calculate": "parseInt(replace(datum['Product'], 'Product ', ''))", "as": "sortOrder" } ], "facet": { "column": { "field": "Category", "title": null, "header": {"labelFontSize": 14} } }, "spec": { "width": 525, "height": 320.5, "encoding": { "y": {"field": "Product", "title": null, "sort": {"field": "sortOrder"}}, "x": { "field": "Sub Cat", "axis": {"orient": "top", "labelAngle": 0}, "title": null } }, "layer": [ { "mark": {"type": "rect"}, "encoding": { "color": { "field": "Value", "scale": {"scheme": "lightgreyred"}, "legend": null } } }, { "mark": {"type": "text", "tooltip": true, "size": 14}, "encoding": { "text": {"field": "Value", "type": "quantitative", "format": ",.0f"}, "color": {"value": "black"} } } ] } }pbix file here