Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
NishPatel
Resolver II
Resolver II

Vega Lite Cumulative Stacked Bar Chart Label Issue

Hi All,

@giammariam, @dm-p

 

I have created cumulative stacked bar chart and trying to add individual as well as total labels to this. As soon as I include text layer, it messes up the order of the month. 

 

Here is the spec for only stacked bar chart...

 

{
"title": {
"align": "middle",
"orient": "top",
"offset": 5,
"text": "AOG Billing",
"font": "Segoe UI",
"fontSize": 24,
"fontWeight": "bold",
"fontStyle": "normal"
},
"data": {"name": "dataset"},
"transform": [
{
"calculate": "if(datum.Category === 'Parts', 0, if(datum.Category === 'Spares Allocation', 1,if(datum.Category === 'Labor', 2,if(datum.Category === 'Material', 3, 4))))",
"as": "sortOrder"
},
{
"calculate": "if(datum.Month === 'April', 0, if(datum.Month === 'May', 1,if(datum.Month === 'June',2,if(datum.Month === 'July',3,if(datum.Month === 'August',4,if(datum.Month === 'September',5,if(datum.Month === 'October',6,if(datum.Month === 'November',7,if(datum.Month === 'December',8,if(datum.Month === 'January',9,if(datum.Month === 'February',10,11)))))))))))",
"as": "FiscalOrder"
},
{
"window": [
{
"op": "sum",
"field": "Amount",
"as": "cumulative_total"
}
],
"sort": [
{
"field": "FiscalOrder",
"order": "ascending"
}
],
"groupby": ["Category"],
"frame": [null, 0]
},
{
"calculate": "format( datum['cumulative_total'], '(,.2s' )",
"as": "_subcategory_label"
},
{
"calculate": "datum['Total Amount'] > 500 ? 1 : 0",
"as": "hide_label_test"
}
],
"encoding": {
"x": {
"field": "Month",
"type": "nominal",
"sort": {
"field": "FiscalOrder",
"sort": "ascending"
},
"axis": {
"title": null,
"labelAngle": 0,
"labelPadding": 5
}
},
"y": {
"field": "cumulative_total",
"type": "quantitative",
"stack": "zero",
"axis": {"title": null}
},
"order": {
"field": "sortOrder",
"sort": "asscending",
"type": "quantitative"
}
},
"layer": [
{
"name": "STACKED_COLUMN_CHART",
"mark": {
"type": "bar",
"tooltip": true,
"size": 43,
"xOffset": 0,
"stroke": "black"
},
"encoding": {
"color": {
"field": "Category",
"sort": [
"Parts",
"Spares Allocation",
"Labor",
"Material",
"Fee"
],
"scale": {
"domain": [
"Parts",
"Spares Allocation",
"Labor",
"Material",
"Fee"
],
"range": [
"#5ec962",
"#21918c",
"#440154",
"#1E7B9F",
"#3b528b"
]
}
}
}
}
]
}

 

and here is the spec including text labels

 

{
"title": {
"align": "middle",
"orient": "top",
"offset": 5,
"text": "AOG Billing",
"font": "Segoe UI",
"fontSize": 24,
"fontWeight": "bold",
"fontStyle": "normal"
},
"data": {"name": "dataset"},
"transform": [
{
"calculate": "if(datum.Category === 'Parts', 0, if(datum.Category === 'Spares Allocation', 1,if(datum.Category === 'Labor', 2,if(datum.Category === 'Material', 3, 4))))",
"as": "sortOrder"
},
{
"calculate": "if(datum.Month === 'April', 0, if(datum.Month === 'May', 1,if(datum.Month === 'June',2,if(datum.Month === 'July',3,if(datum.Month === 'August',4,if(datum.Month === 'September',5,if(datum.Month === 'October',6,if(datum.Month === 'November',7,if(datum.Month === 'December',8,if(datum.Month === 'January',9,if(datum.Month === 'February',10,11)))))))))))",
"as": "FiscalOrder"
},
{
"window": [
{
"op": "sum",
"field": "Amount",
"as": "cumulative_total"
}
],
"sort": [
{
"field": "FiscalOrder",
"order": "ascending"
}
],
"groupby": ["Category"],
"frame": [null, 0]
},
{
"calculate": "format( datum['cumulative_total'], '(,.2s' )",
"as": "_subcategory_label"
},
{
"calculate": "datum['Total Amount'] > 500 ? 1 : 0",
"as": "hide_label_test"
}
],
"encoding": {
"x": {
"field": "Month",
"type": "nominal",
"sort": {
"field": "FiscalOrder",
"sort": "ascending"
},
"axis": {
"title": null,
"labelAngle": 0,
"labelPadding": 5
}
},
"y": {
"field": "cumulative_total",
"type": "quantitative",
"stack": "zero",
"axis": {"title": null}
},
"order": {
"field": "sortOrder",
"sort": "asscending",
"type": "quantitative"
}
},
"layer": [
{
"name": "STACKED_COLUMN_CHART",
"mark": {
"type": "bar",
"tooltip": true,
"size": 43,
"xOffset": 0,
"stroke": "black"
},
"encoding": {
"color": {
"field": "Category",
"sort": [
"Parts",
"Spares Allocation",
"Labor",
"Material",
"Fee"
],
"scale": {
"domain": [
"Parts",
"Spares Allocation",
"Labor",
"Material",
"Fee"
],
"range": [
"#5ec962",
"#21918c",
"#440154",
"#1E7B9F",
"#3b528b"
]
}
}
}
},
{
"name": "ROLE_LABEL",
"mark": {
"type": "text",
"color": "white",
"fontSize": {
"expr": "datum['hide_label_test'] === 0 ? 0:10"
}
},
"encoding": {
"text": {
"field": "_subcategory_label"
},
"y": {
"field": "cumulative_total",
"bandPosition": 0.5
}
}
},
{
"name": "PERIOD_TOTAL",
"transform": [
{
"aggregate": [
{
"op": "sum",
"field": "cumulative_total",
"as": "_total"
}
],
"groupby": ["Month"]
}
],
"mark": {
"type": "text",
"color": "black",
"fontSize": 12,
"fontWeight": "bold",
"yOffset": -10
},
"encoding": {
"text": {
"field": "_total",
"format": ",.2s"
},
"y": {
"field": "_total",
"type": "quantitative"
}
}
}
]
}

 

thank you in advance

1 ACCEPTED SOLUTION

I was able to resolve the text labels.

View solution in original post

3 REPLIES 3
v-yilong-msft
Community Support
Community Support

Hi @NishPatel ,

Based on the data and information you provided, here is my analysis and proposed comments.

In the text layer, make sure you are using the correct fields to add labels. For example, you can use the "Monthly Specific Values" field to add individual labels and the "Monthly Cumulative Sum" field to add total labels.

In the text layer, select the appropriate sorting method. You can sort by date, month, or other fields to ensure that the labels are in the same order as the bars.

vyilongmsft_0-1707881869660.png

Your data contains date fields, and you can use a date hierarchy to ensure that the labels are in the correct chronological order.

vyilongmsft_1-1707882111382.png

 

 

How to Get Your Question Answered Quickly 

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Yilong Zhou

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Like I mentioned, When I dont apply text layer, it sorts correctly. As soon a I apply text layer, it messes up the sorting eventhough the text labels are displayed correctly. 

I was able to resolve the text labels.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.