Skip to content

Commit

Permalink
Add on object formatting support
Browse files Browse the repository at this point in the history
  • Loading branch information
Shafeeq committed Dec 18, 2023
1 parent 4d39741 commit ecadce2
Show file tree
Hide file tree
Showing 9 changed files with 1,410 additions and 1,187 deletions.
49 changes: 40 additions & 9 deletions Tutorial/ColorPalette.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,47 @@ interface BarChartDataPoint {
`colorPalette` is a service that manages the colors used on your visual. An instance of it is available on `IVisualHost`.

## Assigning Color to Data Points
We defined `visualTransform` as a construct to convert `dataView` to a view model Bar Chart can use.
Since we iterate through the data points in `visualTransform` it is also the ideal place to assign colors.
We defined `createSelectorDataPoints` as a construct to convert options `dataView` Bar Chart data points that will be used in visual view.
Since we iterate through the data points in `createSelectorDataPoints` it is also the ideal place to assign colors.

```typescript
let colorPalette: IColorPalette = host.colorPalette; // host: IVisualHost
for (let i = 0, len = Math.max(category.values.length, dataValue.values.length); i < len; i++) {
barChartDataPoints.push({
category: category.values[i],
value: dataValue.values[i],
color: colorPalette.getColor(category.values[i]).value,
});
function createSelectorDataPoints(options: VisualUpdateOptions, host: IVisualHost): BarChartDataPoint[] {
let barChartDataPoints: BarChartDataPoint[] = []
const dataViews = options.dataViews;
if (!dataViews
|| !dataViews[0]
|| !dataViews[0].categorical
|| !dataViews[0].categorical.categories
|| !dataViews[0].categorical.categories[0].source
|| !dataViews[0].categorical.values
) {
return barChartDataPoints;
}

const categorical = dataViews[0].categorical;
const category = categorical.categories[0];
const dataValue = categorical.values[0];

const colorPalette: ISandboxExtendedColorPalette = host.colorPalette;
const strokeColor: string = getColumnStrokeColor(colorPalette);
const strokeWidth: number = getColumnStrokeWidth(colorPalette.isHighContrast);

for (let i = 0, len = Math.max(category.values.length, dataValue.values.length); i < len; i++) {
const color: string = getColumnColorByIndex(category, i, colorPalette);

const selectionId: ISelectionId = host.createSelectionIdBuilder()
.withCategory(category, i)
.createSelectionId();

barChartDataPoints.push({
color,
strokeColor,
strokeWidth,
selectionId,
value: dataValue.values[i],
category: `${category.values[i]}`,
});
}
return barChartDataPoints;
}
```
19 changes: 8 additions & 11 deletions Tutorial/ConditionalFormatting.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Adding conditional formatting to your Visual
[Conditional formatting](https://docs.microsoft.com/en-us/power-bi/visuals/service-tips-and-tricks-for-color-formatting#conditional-formatting-for-visualizations) of custom properties is supported by updating `VisualObjectInstance` object's properties as enumerated under `enumerateObjectInstances` method.

See [commit](https://github.com/microsoft/powerbi-visuals-api/commit/8fe88399c5ba82feeec4541ce5bf8e02a3ecd15a) for what was added at this step.
[Conditional formatting](https://docs.microsoft.com/en-us/power-bi/visuals/service-tips-and-tricks-for-color-formatting#conditional-formatting-for-visualizations) of custom formatting properties is supported by setting formatting property `instanceKind` in `getFormattingModel` method.
For more info on conditional formatting click [here](https://learn.microsoft.com/en-us/power-bi/developer/visuals/conditional-format?tabs=getFormattingModel)

Conditional formatting can only be applied to the following property types:
* Color
Expand All @@ -10,25 +9,23 @@ Conditional formatting can only be applied to the following property types:
* Web URL

## Add a conditional color formatting entry in the format pane
To add the conditional color formatting button in the format pane for the desired object, under the `enumerateObjectInstances` method, make the following change:
To add the conditional color formatting button in the format pane for the desired object, under the `getFormattingModel` method, make the following change:

Via `propertyInstanceKind` property of enumerated `VisualObjectInstance`, list all the properties that you'd like to have the conditional formatting entry applied to in the format pane.
Define `instanceKind` property of required formatting property `descriptor` with the appropriate value.
Use `VisualEnumerationInstanceKinds` enum to declare the type of the desired format (constant, rule or both).

```typescript
// List your conditional formatting properties
propertyInstanceKind: {
fill: VisualEnumerationInstanceKinds.ConstantOrRule
}
instanceKind: powerbi.VisualEnumerationInstanceKinds.ConstantOrRule
```
![](images/ConditionalFormattingEntry.png)

## Define how conditional formatting behaves
Using `createDataViewWildcardSelector` declared under `powerbi-visuals-utils-dataviewutils`, specify whether conditional formatting will be applied to instances, totals, or both. For more information, see [DataViewWildcard](https://docs.microsoft.com/en-us/power-bi/developer/visuals/utils-dataview#dataviewwildcard).

In `enumerateObjectInstances`, make the following changes to the objects you want to apply conditional formatting to:
In `BarChartFormattingSettingsModel`, make the following changes to the formatting properties you want to apply conditional formatting to:

* Replace the `VisualObjectInstance`'s `selector` value with a `dataViewWildcard.createDataViewWildcardSelector()` call. Specify the desired option from `DataViewWildcardMatchingOption` enum to define whether conditional formatting is applied to instances, totals, or both.
* Replace the formatting property `descriptor`'s `selector` value with a `dataViewWildcard.createDataViewWildcardSelector()` call. Specify the desired option from `DataViewWildcardMatchingOption` enum to define whether conditional formatting is applied to instances, totals, or both.

* Add the `altConstantValueSelector` property having the value previously defined for the `selector` property.

Expand All @@ -39,6 +36,6 @@ selector: dataViewWildcard.createDataViewWildcardSelector(dataViewWildcard.DataV
// Add this property with the value previously defined for the selector property
altConstantValueSelector: barDataPoint.selectionId.getSelector()
```
See [commit](https://github.com/Microsoft/PowerBI-visuals-sampleBarChart/commit/956923b641bb1eacb613bf55a91f77725bc42431) for how conditional formatting was applied to sample bar chart.
See [commit](https://github.com/Microsoft/PowerBI-visuals-sampleBarChart) for how conditional formatting was applied to sample bar chart.

![](images/CondFormatSupport.png)
73 changes: 70 additions & 3 deletions capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,67 @@
}
}
},
"directEditTest": {
"properties": {
"show": {
"displayName": "Show",
"type": {
"bool": true
}
},
"textProperty": {
"displayName": "Text",
"type": {
"text": true
}
},
"fontFamily": {
"type": {
"formatting": {
"fontFamily": true
}
}
},
"fontSize": {
"type": {
"formatting": {
"fontSize": true
}
}
},
"bold": {
"type": {
"formatting": {
"fontSize": true
}
}
},
"italic": {
"type": {
"formatting": {
"fontSize": true
}
}
},
"underline": {
"type": {
"formatting": {
"fontSize": true
}
}
},
"fill": {
"displayName": "Color",
"type": {
"fill": {
"solid": {
"color": true
}
}
}
}
}
},
"colorSelector": {
"displayName": "Data Colors",
"properties": {
Expand Down Expand Up @@ -149,12 +210,18 @@
"default": true,
"canvas": true
},
"roles": ["Tooltips"],
"roles": [
"Tooltips"
],
"supportEnhancedTooltips": true
},
"supportsLandingPage": false,
"supportsOnObjectFormatting": true,
"enablePointerEventsFormatMode": true,
"drilldown": {
"roles": ["category"]
"roles": [
"category"
]
},
"privileges": []
}
}
Loading

0 comments on commit ecadce2

Please sign in to comment.