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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
paramjit_s
Frequent Visitor

Custom Bar Chart - Data Point Colours

I am creating a custom bar chart using sampleBarChart to show duration of my tests, in addition in need to show passed and failed tests using different colours for each bar. 

 

These are my data roles in capabilities.json . The Legend data role will contain the result for each test.  

 

    "dataRoles": [
        {
            "displayName": "Category Data",
            "name": "category",
            "kind": "Grouping"
        },
        {
            "displayName": "Measure Data",
            "name": "measure",
            "kind": "Measure"
        }, 
        {
            "displayName": "Legend", 
            "name": "series", 
            "kind": "Grouping"
        }
    ],

 

Current data view mapping. 

 

 "dataViewMappings": [
        {
            "conditions": [
                {
                    "category": {
                        "max": 1
                    },
                    "measure": {
                        "max": 1
                    }
                }
            ],
            "categorical": {
                "categories": {
                    "for": {
                        "in": "category"
                    }
                },
                "values": {
                    "group": {
                        "by": "series", 
                        "select": [
                            { "for": {
                                "in": "measure"
                            }}
                        ]
                    }
                
                }
            }
        }
    ],

 

Current code doesn't plot any bars on the chart.  However in the dataview of the custom visual online this is what it shows. 

 dataview.png

Under categories each test ID is shown seperately and under values it groups the durations into two groups passed and failed.

 

I have not made any other changes to the code. If someone could please help me show the duration of each test and set colour of each bar according to whether that particular test has passed or failed. I have been able to achieve this in the native column chart of Power Bi but I need to be able to do the same in this custom visual because i need to make more changes to this chart.

 
11 REPLIES 11
v-viig
Community Champion
Community Champion

Hello @paramjit_s

 

You need to update visualTransform function in order to enumerate groups as well (categorical.values.grouped()).

Every category should be assigned to a proper group.

 

Please let me know if you have any further questions.

 

Ignat Vilesov,

Software Engineer

 

Microsoft Power BI Custom Visuals

pbicvsupport@microsoft.com

Hi @v-viig

 

Thanks for replying.

I did figure out that I need to make changes to the visualTransform function and eumerate groups into it. Howvever, I am unable to figure out how exactly to do that. 

Is it possible for you to give me an example on how I would assign a group to a category, so that I can try to apply that to my project.

 

Thanks again for the help. 


Paramjit S

 

 

Please take a look at this sample code.

 

Ignat Vilesov,

Software Engineer

 

Microsoft Power BI Custom Visuals

pbicvsupport@microsoft.com

Thank you very much for the sample code. It was a great help in my project.

 

There's a slight issue I am having, the bars are grouped in a way in which when I try to perform implict sort on them (by date), they are sorted while remaining in their respective groups.

 

example.png

 

 

 

 

 

 

 

 

 

Could you please guide me on how to solve this issue?

 

Thank You

Paramjit S

 

 

How do you sort columns? Could you please share your snippet code?

 

Ignat Vilesov,

Software Engineer

 

Microsoft Power BI Custom Visuals

pbicvsupport@microsoft.com

I just added another data role and then applied implicit sorting on it in capabilities.json. 

 

This is an example of data that goes into this datarole, 2017-03-17 12:27:00.000

 

{
      "displayName": "Sort By", 
      "name": "sort", 
      "kind": "Measure"
}
"sorting": {
        "implicit": {
            "clauses": [
                {
                    "role": "sort",
                    "direction": 1
                }
            ]
        }
    }

Did you include this column into dataViewMappings.categorical.categories?

It should look like this:

"categorical": {
    "categories": {
        "select": [
            {
                "for": {
                    "in": "category"
                }
            },
            {
                "for": {
                    "in": "sort"
                }
            }
        ]
    },
    "values": {
        "group": {
            "by": "series",
            "select": [
                {
                    "for": {
                        "in": "measure"
                    }
                }
            ]
        }
    }
}

Ignat Vilesov,

Software Engineer

 

Microsoft Power BI Custom Visuals

pbicvsupport@microsoft.com

Yes I did include it in the dataViewMappings like how you did above but unfortunately it is still not sorting the columns quite right. They are being sorted while still remaining in their groups, I need to seperate them somehow.

 

Paramjit S

I apologize. There's a mistake in my example above.

This is the fixed one:

{
    "dataRoles": [
        {
            "displayName": "Category Data",
            "name": "category",
            "kind": "Grouping"
        },
        {
            "displayName": "Measure Data",
            "name": "measure",
            "kind": "Measure"
        },
        {
            "displayName": "Legend",
            "name": "series",
            "kind": "Grouping"
        },
        {
            "displayName": "Sort order",
            "name": "sort",
            "kind": "Measure"
        }
    ],
    "dataViewMappings": [
        {
            "conditions": [
                {
                    "category": {
                        "max": 1
                    },
                    "measure": {
                        "max": 1
                    }
                }
            ],
            "categorical": {
                "categories": {
                    "for": {
                        "in": "category"
                    }
                },
                "values": {
                    "group": {
                        "by": "series",
                        "select": [
                            {
                                "for": {
                                    "in": "measure"
                                }
                            },
                            {
                                "for": {
                                    "in": "sort"
                                }
                            }
                        ]
                    }
                }
            }
        }
    ],
    "sorting": {
        "implicit": {
            "clauses": [
                {
                    "role": "sort",
                    "direction": 1
                }
            ]
        }
    }
}

Ignat Vilesov,

Software Engineer

 

Microsoft Power BI Custom Visuals

pbicvsupport@microsoft.com

Unfortunately that still does not solve the problem I am having. 

Just to clarify the problem isn't to do with sorting the columns. It is that when the columns are created in the visualTransform  function they are grouped in such a way so that when I try to sort them they are being sorted while remaining in their respective groups (passed, failed & incomplete).  

 

eg.pngThis is what the custom chart looks like for one of the tests. Blue represents failed, yellow incomplete and red passed. The columns are actually sorted in this chart by date but with the current code they are only being sorted within their groups and not as a whole chart.

   

 

 

 

 

eg2.png

This is the native PowerBi column chart representing the same test as above. The columns in this chart are sorted using the same field as the above chart. So I need to make my custom graph look like this one. 

 

 

 

 

 

 

Thank You 

Paramjit S

 

 

I think that you should implement sorting for barChartDataPoints in visualTransform function.

In other words, you need to keep sorting index in BarChartDataPoint and use the sorting index to sort barChartDataPoints.

 

Ignat Vilesov,

Software Engineer

 

Microsoft Power BI Custom Visuals

pbicvsupport@microsoft.com

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.