Skip to content

Commit

Permalink
Add high contrast mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
kullJul committed Jul 18, 2024
1 parent 53aa6a3 commit 7cb8c8b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/behavior/visualBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ export class VisualBehavior implements IInteractiveBehavior {
!dataPoint.selected && this.options.hasHighlights);
});

this.options.bars.style("stroke-opacity", (dataPoint: MekkoChartColumnDataPoint) => {
return utils.getFillOpacity(
dataPoint.selected,
dataPoint.highlight,
!dataPoint.highlight && hasSelection,
!dataPoint.selected && this.options.hasHighlights);
});

const series: Selection<any, any, any, any> = this.options.mainGraphicsContext
.selectAll(MekkoChart.SeriesSelector.selectorName);

Expand Down
6 changes: 3 additions & 3 deletions src/columnChart/baseColumnChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ import * as axisType from "./../axis/type";

import IViewport = powerbi.IViewport;
import IVisualHost = powerbi.extensibility.visual.IVisualHost;
import IColorPalette = powerbi.extensibility.IColorPalette;
import ISandboxExtendedColorPalette = powerbi.extensibility.ISandboxExtendedColorPalette;
import ILocalizationManager = powerbi.extensibility.ILocalizationManager;
import DataViewCategorical = powerbi.DataViewCategorical;
import DataViewMetadataColumn = powerbi.DataViewMetadataColumn;
Expand Down Expand Up @@ -197,7 +197,7 @@ export class BaseColumnChart implements IColumnChart {

private data: MekkoColumnChartData;

private colorPalette: IColorPalette;
private colorPalette: ISandboxExtendedColorPalette;
private visualHost: IVisualHost;
private localizationManager: ILocalizationManager;

Expand Down Expand Up @@ -289,7 +289,7 @@ export class BaseColumnChart implements IColumnChart {
public static converter(
visualHost: IVisualHost,
categorical: DataViewCategorical,
colors: IColorPalette,
colors: ISandboxExtendedColorPalette,
is100PercentStacked: boolean = false,
isScalar: boolean = false,
supportsOverflow: boolean = false,
Expand Down
10 changes: 7 additions & 3 deletions src/converterStrategy/baseConverterStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { legendInterfaces } from "powerbi-visuals-utils-chartutils";
import * as formattingUtils from "./../formattingUtils";
import { max, sum, min } from "d3-array";

import IColorPalette = powerbi.extensibility.IColorPalette;
import ISandboxExtendedColorPalette = powerbi.extensibility.ISandboxExtendedColorPalette;
import IVisualHost = powerbi.extensibility.visual.IVisualHost;
import DataViewCategorical = powerbi.DataViewCategorical;
import DataViewMetadataColumn = powerbi.DataViewMetadataColumn;
Expand Down Expand Up @@ -65,6 +65,7 @@ export class BaseConverterStrategy implements ConverterStrategy {
private static YColumnName: string = "Y";

private static SortField: string = "categoryValue";
private static DefaultLegendLabelColor: string = "black";

private dataView: DataViewCategorical;
private visualHost: IVisualHost;
Expand All @@ -79,7 +80,7 @@ export class BaseConverterStrategy implements ConverterStrategy {
}

// eslint-disable-next-line max-lines-per-function
public getLegend(colorPalette: IColorPalette, settingsModel: VisualFormattingSettingsModel): LegendSeriesInfo {
public getLegend(colorPalette: ISandboxExtendedColorPalette, settingsModel: VisualFormattingSettingsModel): LegendSeriesInfo {
const legend: MekkoLegendDataPoint[] = [];
const seriesSources: DataViewMetadataColumn[] = [];
const seriesObjects: DataViewObjects[][] = [];
Expand Down Expand Up @@ -171,7 +172,7 @@ export class BaseConverterStrategy implements ConverterStrategy {
}

legend.push({
color,
color: colorPalette.isHighContrast ? colorPalette.background.value : color,
label,
markerShape: LegendIcon.circle,
identity: selectionId,
Expand All @@ -193,10 +194,13 @@ export class BaseConverterStrategy implements ConverterStrategy {
: "";
}

const labelColor: string = colorPalette.isHighContrast ? colorPalette.foreground.value : BaseConverterStrategy.DefaultLegendLabelColor;

const legendData: ILegendData = {
title: legendTitle,
dataPoints: legend,
grouped: grouped,
labelColor: labelColor
};

return {
Expand Down
4 changes: 2 additions & 2 deletions src/dataInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import PrimitiveValue = powerbi.PrimitiveValue;
import NumberRange = powerbi.NumberRange;
import IViewport = powerbi.IViewport;
import IVisualHost = powerbi.extensibility.visual.IVisualHost;
import IColorPalette = powerbi.extensibility.IColorPalette;
import ISandboxExtendedColorPalette = powerbi.extensibility.ISandboxExtendedColorPalette;
import DataViewMetadataColumn = powerbi.DataViewMetadataColumn;
import CustomVisualOpaqueIdentity = powerbi.visuals.CustomVisualOpaqueIdentity;
import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructorOptions;
Expand Down Expand Up @@ -128,7 +128,7 @@ export interface ILegendGroup extends ILegend {

export interface IMekkoChartVisualHost {
updateLegend(data: ILegendData): void;
getSharedColors(): IColorPalette;
getSharedColors(): ISandboxExtendedColorPalette;
triggerRender(suppressAnimations: boolean): void;
}

Expand Down
22 changes: 22 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import powerbi from "powerbi-visuals-api";
import ISandboxExtendedColorPalette = powerbi.extensibility.ISandboxExtendedColorPalette;
import ISelectionId = powerbi.visuals.ISelectionId;

import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
Expand Down Expand Up @@ -398,6 +399,7 @@ export class ValueAxisSettings extends FormattingSettingsSimpleCard {
export class DataPointSettings extends FormattingSettingsSimpleCard {
public name: string = "dataPoint";
public displayNameKey:string = "Visual_Data_Colors";
public defaultStrokeColor: string = "transparent";

public defaultColor = new formattingSettings.ColorPicker({
name: "defaultColor",
Expand Down Expand Up @@ -457,6 +459,26 @@ export class VisualFormattingSettingsModel extends FormattingSettingsModel {
this.drillControl
];

public setHighContrastMode(colorPalette: ISandboxExtendedColorPalette): void {
const isHighContrastMode: boolean = colorPalette.isHighContrast;
if (isHighContrastMode){
const foregroundColor: string = colorPalette.foreground.value;
const backgroundColor: string = colorPalette.background.value;

this.columnBorder.color.value.value = backgroundColor;
this.labels.color.value.value = foregroundColor;
this.categoryAxis.labelColor.value.value = foregroundColor;
this.valueAxis.labelColor.value.value = foregroundColor;
this.dataPoint.defaultStrokeColor = foregroundColor;
}

this.dataPoint.visible = !isHighContrastMode;
this.columnBorder.color.visible = !isHighContrastMode;
this.labels.color.visible = !isHighContrastMode;
this.categoryAxis.labelColor.visible = !isHighContrastMode;
this.valueAxis.labelColor.visible = !isHighContrastMode;
}

public setDataPointColorPickerSlices(layers: IColumnChart[]) {
for (let i: number = 0; i < layers.length; i++) {
for (const series of (<BaseColumnChart>layers[i]).getData().series) {
Expand Down
10 changes: 10 additions & 0 deletions src/visual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructor
import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
import IVisual = powerbi.extensibility.visual.IVisual;
import ILocalizationManager = powerbi.extensibility.ILocalizationManager;
import ISandboxExtendedColorPalette = powerbi.extensibility.ISandboxExtendedColorPalette;

import {
MekkoColumnChartData,
Expand Down Expand Up @@ -290,6 +291,7 @@ export class MekkoChart implements IVisual {
public static OuterPaddingRatio: number = 0.4;
public static InnerPaddingRatio: number = 0.2;
public static TickLabelPadding: number = 2;
public static DefaultAxisLinesColor: string = "black";

private rootElement: Selection;
private legendParent: Selection;
Expand Down Expand Up @@ -350,6 +352,7 @@ export class MekkoChart implements IVisual {
private formattingSettingsService: FormattingSettingsService;
private localizationManager: ILocalizationManager;
private selectionManager: ISelectionManager;
private colorPalette: ISandboxExtendedColorPalette;

public settingsModel: VisualFormattingSettingsModel;

Expand All @@ -360,6 +363,7 @@ export class MekkoChart implements IVisual {
public init(options: VisualConstructorOptions) {
this.visualInitOptions = options;
this.visualHost = options.host;
this.colorPalette = options.host.colorPalette;

select("body").style(
"-webkit-tap-highlight-color", "transparent"
Expand Down Expand Up @@ -687,6 +691,7 @@ export class MekkoChart implements IVisual {

if (this.dataViews && this.dataViews.length > 0) {
this.settingsModel = this.formattingSettingsService.populateFormattingSettingsModel(VisualFormattingSettingsModel, this.dataViews[0]);
this.settingsModel.setHighContrastMode(this.colorPalette);
}

for (let layerIndex: number = 0, length: number = this.layers.length; layerIndex < length; layerIndex++) {
Expand Down Expand Up @@ -965,6 +970,10 @@ export class MekkoChart implements IVisual {
if (this.layerLegendData.grouped) {
legendData.grouped = true;
}

if (this.layerLegendData.labelColor){
legendData.labelColor = this.layerLegendData.labelColor;
}
}
}

Expand Down Expand Up @@ -1102,6 +1111,7 @@ export class MekkoChart implements IVisual {
dataPoints: reducedLegends[index].data
};


LegendData.update(legendData, legendProperties);
legend.drawLegend(legendData, this.currentViewport);
});
Expand Down
1 change: 1 addition & 0 deletions src/visualStrategy/baseVisualStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ export class BaseVisualStrategy implements IVisualStrategy {
hasSelection,
data.hasHighlights)
)
.style("stroke", settingsModel.dataPoint.defaultStrokeColor)
.attr("height", layout.shapeLayout.height)
.attr("width", layout.shapeLayout.width)
.attr("x", layout.shapeLayout.x)
Expand Down
2 changes: 2 additions & 0 deletions style/visual.less
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
}

.series .column {
stroke-width: 2px;

&:focus {
outline: none;
}
Expand Down

0 comments on commit 7cb8c8b

Please sign in to comment.