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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Aiya
Regular Visitor

Any sample with source for setting bar color by "Fx" in custom visual

Is there any custom visual which has this settings and source code for reference ? 
or how to create this by code in the format panel ?

Thanks

1 REPLY 1
johnbasha33
Super User
Super User

Hi  
Short answer: yesโ€”use the Formatting Model API and enable conditional formatting (the โ€œFxโ€ button) on a color property by setting the propertyโ€™s instanceKind to ConstantOrRule, and wiring selectors correctly. Microsoftโ€™s Sample Bar Chart shows this pattern, and the docs walk through it step-by-step.
capabilities.json โ€” define a fill property (only color supports Fx)

{
"objects": {
"colorSelector": {
"displayName": "Data colors",
"properties": {
"fill": { "type": { "fill": { "solid": { "color": true } } } }
}
}
}
}

getFormattingModel โ€” surface a ColorPicker that supports Fx

import powerbi from "powerbi-visuals-api";
import * as dataViewWildcard from "powerbi-visuals-utils-dataviewutils";

public getFormattingModel(): powerbi.visuals.FormattingModel {
const card: powerbi.visuals.FormattingCard = {
displayName: "Data colors",
uid: "dataColorsCard",
groups: [{ displayName: undefined, uid: "dataColorsGroup", slices: [] }]
};

this.barDataPoints.forEach((p, i) => {
(card.groups[0] as powerbi.visuals.FormattingGroup).slices.push({
uid: `color${i}`,
displayName: p.category, // label in the pane
control: {
type: powerbi.visuals.FormattingComponent.ColorPicker,
properties: {
descriptor: {
objectName: "colorSelector",
propertyName: "fill",
// Fx needs a wildcard selector + an alt selector per point:
selector: dataViewWildcard.createDataViewWildcardSelector(
dataViewWildcard.DataViewWildcardMatchingOption.InstancesAndTotals
),
altConstantValueSelector: p.selectionId.getSelector(),
// <- enables the Fx button next to the color swatch
instanceKind: powerbi.VisualEnumerationInstanceKinds.ConstantOrRule
},
value: { value: p.color } // default color when no rule is set
}
}
});
});

return { cards: [card] };
}

This is the same pattern shown in Microsoftโ€™s โ€œAdd conditional formattingโ€ article and the SampleBarChart tutorial. Microsoft LearnGitHub

  1. Reading the chosen color/rule
    If the user picks a constant color, youโ€™ll get it back as usual via your formatting settings service or enumerateObjectInstances. If they set an Fx rule, Power BI evaluates it and your visual receives the computed color in the data view/objects for the matching data point (or via your settings service), so you just apply fill when drawing bars. Community and docs note that the wildcard/alt selectors are what bind rules to instances. Microsoft LearnStack Overflow

    Useful references (source + docs):

    • Docs: Add conditional formatting (Fx) to custom visuals (includes code using instanceKind and selectors). Microsoft Learn

    • Sample code: PowerBI-visuals-sampleBarChart โ†’ Tutorial/ConditionalFormatting.md and barChart.ts. GitHub

      Library: powerbi-visuals-utils-formattingmodel (recommended for the new Format pane). GitHubMicrosoft Learn+1

      Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!

      @Aiya

Helpful resources

Announcements
Users online (27)