DataViewUtils
DataViewUtils
是一组函数和类,用于简化对 Power BI 视觉对象的 DataView 对象的分析。
安装
若要安装包,请在目录中运行以下命令和当前的自定义视觉对象:
npm install powerbi-visuals-utils-dataviewutils --save
上面的命令安装此包,并将它作为依赖项添加到 package.json
文件。
DataViewWildcard
DataViewWildcard
提供了 createDataViewWildcardSelector
函数来支持属性的条件格式。
createDataViewWildcardSelector
会返回一个选择器;在根据 dataviewWildcardMatchingOption (InstancesAndTotals (default), InstancesOnly, TotalsOnly)
定义将如何应用格式窗格中的条件格式条目时,需要使用此选择器。
示例:
import { dataViewWildcard } from "powerbi-visuals-utils-dataviewutils";
let selector = dataViewWildcard.createDataViewWildcardSelector(dataViewWildcard.DataViewWildcardMatchingOption.InstancesAndTotals);
// returns {data: [{dataViewWildcard:{matchingOption: 0}}]};
DataRoleHelper
DataRoleHelper
提供了用于检查 dataView 对象的角色的函数。
该模块提供了以下函数:
getMeasureIndexOfRole
此函数按角色名称查找度量值并返回其索引。
function getMeasureIndexOfRole(grouped: DataViewValueColumnGroup[], roleName: string): number;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewValueColumnGroup = powerbi.DataViewValueColumnGroup;
import { dataRoleHelper } from "powerbi-visuals-utils-dataviewutils";
// ...
// This object is actually a part of the dataView object.
let columnGroup: DataViewValueColumnGroup[] = [{
values: [
{
source: {
displayName: "Microsoft",
roles: {
"company": true
}
},
values: []
},
{
source: {
displayName: "Power BI",
roles: {
"product": true
}
},
values: []
}
]
}];
dataRoleHelper.getMeasureIndexOfRole(columnGroup, "product");
// returns: 1
getCategoryIndexOfRole
此函数按角色名称查找类别并返回其索引。
function getCategoryIndexOfRole(categories: DataViewCategoryColumn[], roleName: string): number;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewCategoryColumn = powerbi.DataViewCategoryColumn;
import { dataRoleHelper } from "powerbi-visuals-utils-dataviewutils";
// ...
// This object is actually a part of the dataView object.
let categoryGroup: DataViewCategoryColumn[] = [
{
source: {
displayName: "Microsoft",
roles: {
"company": true
}
},
values: []
},
{
source: {
displayName: "Power BI",
roles: {
"product": true
}
},
values: []
}
];
dataRoleHelper.getCategoryIndexOfRole(categoryGroup, "product");
// returns: 1
hasRole
此函数检查是否在元数据中定义了提供的角色。
function hasRole(column: DataViewMetadataColumn, name: string): boolean;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewMetadataColumn = powerbi.DataViewMetadataColumn;
import { dataRoleHelper } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let metadata: DataViewMetadataColumn = {
displayName: "Microsoft",
roles: {
"company": true
}
};
DataRoleHelper.hasRole(metadata, "company");
// returns: true
hasRoleInDataView
此函数检查是否在 dataView 中定义了提供的角色。
function hasRoleInDataView(dataView: DataView, name: string): boolean;
示例:
import powerbi from "powerbi-visuals-api";
import DataView = powerbi.DataView;
import { dataRoleHelper } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let dataView: DataView = {
metadata: {
columns: [
{
displayName: "Microsoft",
roles: {
"company": true
}
},
{
displayName: "Power BI",
roles: {
"product": true
}
}
]
}
};
DataRoleHelper.hasRoleInDataView(dataView, "product");
// returns: true
hasRoleInValueColumn
此函数检查是否在值列中定义了提供的角色。
function hasRoleInValueColumn(valueColumn: DataViewValueColumn, name: string): boolean;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewValueColumn = powerbi.DataViewValueColumn;
import { dataRoleHelper } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let valueColumn: DataViewValueColumn = {
source: {
displayName: "Microsoft",
roles: {
"company": true
}
},
values: []
};
dataRoleHelper.hasRoleInValueColumn(valueColumn, "company");
// returns: true
DataViewObjects
DataViewObjects
提供了用于提取对象的值的函数。
该模块提供了以下函数:
getValue
此函数返回特定对象的值。
function getValue<T>(objects: DataViewObjects, propertyId: DataViewObjectPropertyIdentifier, defaultValue?: T): T;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewObjectPropertyIdentifier = powerbi.DataViewObjectPropertyIdentifier;
import { dataViewObjects } from "powerbi-visuals-utils-dataviewutils";
let property: DataViewObjectPropertyIdentifier = {
objectName: "microsoft",
propertyName: "bi"
};
// This object is actually a part of the dataView object.
let objects: powerbi.DataViewObjects = {
"microsoft": {
"windows": 5,
"bi": "Power"
}
};
dataViewObjects.getValue(objects, property);
// returns: Power
getObject
此函数从指定对象返回对象。
function getObject(objects: DataViewObjects, objectName: string, defaultValue?: IDataViewObject): IDataViewObject;
示例:
import { dataViewObjects } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let objects: powerbi.DataViewObjects = {
"microsoft": {
"windows": 5,
"bi": "Power"
}
};
dataViewObjects.getObject(objects, "microsoft");
/* returns: {
"bi": "Power",
"windows": 5
}*/
getFillColor
此函数返回对象的纯色。
function getFillColor(objects: DataViewObjects, propertyId: DataViewObjectPropertyIdentifier, defaultColor?: string): string;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewObjectPropertyIdentifier = powerbi.DataViewObjectPropertyIdentifier;
import { dataViewObjects } from "powerbi-visuals-utils-dataviewutils";
let property: DataViewObjectPropertyIdentifier = {
objectName: "power",
propertyName: "fillColor"
};
// This object is actually part of the dataView object.
let objects: powerbi.DataViewObjects = {
"power": {
"fillColor": {
"solid": {
"color": "yellow"
}
},
"bi": "Power"
}
};
dataViewObjects.getFillColor(objects, property);
// returns: yellow
getCommonValue
此通用函数检索特定对象的颜色或值。
function getCommonValue(objects: DataViewObjects, propertyId: DataViewObjectPropertyIdentifier, defaultValue?: any): any;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewObjectPropertyIdentifier = powerbi.DataViewObjectPropertyIdentifier;
import { dataViewObjects } from "powerbi-visuals-utils-dataviewutils";
let colorProperty: DataViewObjectPropertyIdentifier = {
objectName: "power",
propertyName: "fillColor"
};
let biProperty: DataViewObjectPropertyIdentifier = {
objectName: "power",
propertyName: "bi"
};
// This object is actually part of the dataView object.
let objects: powerbi.DataViewObjects = {
"power": {
"fillColor": {
"solid": {
"color": "yellow"
}
},
"bi": "Power"
}
};
dataViewObjects.getCommonValue(objects, colorProperty); // returns: yellow
dataViewObjects.getCommonValue(objects, biProperty); // returns: Power
DataViewObject
DataViewObject
提供了用于提取对象的值的函数。
该模块提供了以下函数:
getValue
此函数按属性名称返回对象的值。
function getValue<T>(object: IDataViewObject, propertyName: string, defaultValue?: T): T;
示例:
import { dataViewObject } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let object: powerbi.DataViewObject = {
"windows": 5,
"microsoft": "Power BI"
};
dataViewObject.getValue(object, "microsoft");
// returns: Power BI
getFillColorByPropertyName
此函数按属性名称返回对象的纯色。
function getFillColorByPropertyName(object: IDataViewObject, propertyName: string, defaultColor?: string): string;
示例:
import { dataViewObject } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let object: powerbi.DataViewObject = {
"windows": 5,
"fillColor": {
"solid": {
"color": "green"
}
}
};
dataViewObject.getFillColorByPropertyName(object, "fillColor");
// returns: green
converterHelper
converterHelper
提供了用于检查 dataView 属性的函数。
该模块提供了以下函数:
categoryIsAlsoSeriesRole
此函数检查类别是否也是系列。
function categoryIsAlsoSeriesRole(dataView: DataViewCategorical, seriesRoleName: string, categoryRoleName: string): boolean;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewCategorical = powerbi.DataViewCategorical;
import { converterHelper } from "powerbi-visuals-utils-dataviewutils";
// ...
// This object is actually part of the dataView object.
let categorical: DataViewCategorical = {
categories: [{
source: {
displayName: "Microsoft",
roles: {
"power": true,
"bi": true
}
},
values: []
}]
};
converterHelper.categoryIsAlsoSeriesRole(categorical, "power", "bi");
// returns: true
getSeriesName
此函数返回系列的名称。
function getSeriesName(source: DataViewMetadataColumn): PrimitiveValue;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewMetadataColumn = powerbi.DataViewMetadataColumn;
import { converterHelper } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let metadata: DataViewMetadataColumn = {
displayName: "Microsoft",
roles: {
"power": true,
"bi": true
},
groupName: "Power BI"
};
converterHelper.getSeriesName(metadata);
// returns: Power BI
isImageUrlColumn
此函数检查列是否包含图像 URL。
function isImageUrlColumn(column: DataViewMetadataColumn): boolean;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewMetadataColumn = powerbi.DataViewMetadataColumn;
import { converterHelper } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let metadata: DataViewMetadataColumn = {
displayName: "Microsoft",
type: {
misc: {
imageUrl: true
}
}
};
converterHelper.isImageUrlColumn(metadata);
// returns: true
isWebUrlColumn
此函数检查列是否包含 Web URL。
function isWebUrlColumn(column: DataViewMetadataColumn): boolean;
示例:
import powerbi from "powerbi-visuals-api";
import DataViewMetadataColumn = powerbi.DataViewMetadataColumn;
import { converterHelper } from "powerbi-visuals-utils-dataviewutils";
// This object is actually a part of the dataView object.
let metadata: DataViewMetadataColumn = {
displayName: "Microsoft",
type: {
misc: {
webUrl: true
}
}
};
converterHelper.isWebUrlColumn(metadata);
// returns: true
hasImageUrlColumn
此函数检查 dataView 是否有包含图像 URL 的列。
function hasImageUrlColumn(dataView: DataView): boolean;
示例:
import DataView = powerbi.DataView;
import converterHelper = powerbi.extensibility.utils.dataview.converterHelper;
// This object is actually part of the dataView object.
let dataView: DataView = {
metadata: {
columns: [
{
displayName: "Microsoft"
},
{
displayName: "Power BI",
type: {
misc: {
imageUrl: true
}
}
}
]
}
};
converterHelper.hasImageUrlColumn(dataView);
// returns: true
DataViewObjectsParser
DataViewObjectsParser
提供了分析格式设置面板的属性的最简单方法。
类提供以下方法:
getDefault
此静态方法返回 DataViewObjectsParser 的实例。
static getDefault(): DataViewObjectsParser;
示例:
import { dataViewObjectsParser } from "powerbi-visuals-utils-dataviewutils";
// ...
dataViewObjectsParser.getDefault();
// returns: an instance of the DataViewObjectsParser
parse
此方法分析格式设置面板的属性并返回 DataViewObjectsParser
的实例。
static parse<T extends DataViewObjectsParser>(dataView: DataView): T;
示例:
import powerbi from "powerbi-visuals-api";
import IVisual = powerbi.extensibility.IVisual;
import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
import { dataViewObjectsParser } from "powerbi-visuals-utils-dataviewutils";
/**
* This class describes formatting panel properties.
* Name of the property should match its name described in the capabilities.
*/
class DataPointProperties {
public fillColor: string = "red"; // This value is a default value of the property.
}
class PropertiesParser extends dataViewObjectsParser.DataViewObjectsParser {
/**
* This property describes a group of properties.
*/
public dataPoint: DataPointProperties = new DataPointProperties();
}
export class YourVisual extends IVisual {
// implementation of the IVisual.
private propertiesParser: PropertiesParser;
public update(options: VisualUpdateOptions): void {
// Parses properties.
this.propertiesParser = PropertiesParser.parse<PropertiesParser>(options.dataViews[0]);
// You can use the properties after parsing
console.log(this.propertiesParser.dataPoint.fillColor); // returns "red" as default value, it will be updated automatically after any change of the formatting panel.
}
}
enumerateObjectInstances
重要
enumerateObjectInstances
在 API 版本 5.1 中已弃用。 它已被 getFormattingModel 取代。
此外,请参阅 FormattingModel 实用工具。
此静态方法枚举属性并返回 VisualObjectInstanceEnumeration
的实例。
在视觉对象的 enumerateObjectInstances
方法中执行。
static enumerateObjectInstances(dataViewObjectParser: dataViewObjectsParser.DataViewObjectsParser, options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration;
示例:
import powerbi from "powerbi-visuals-api";
import IVisual = powerbi.extensibility.IVisual;
import EnumerateVisualObjectInstancesOptions = powerbi.EnumerateVisualObjectInstancesOptions;
import VisualObjectInstanceEnumeration = powerbi.VisualObjectInstanceEnumeration;
import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
import { dataViewObjectsParser } from "powerbi-visuals-utils-dataviewutils";
/**
* This class describes formatting panel properties.
* Name of the property should match its name described in the capabilities.
*/
class DataPointProperties {
public fillColor: string = "red";
}
class PropertiesParser extends dataViewObjectsParser.DataViewObjectsParser {
/**
* This property describes a group of properties.
*/
public dataPoint: DataPointProperties = new DataPointProperties();
}
export class YourVisual extends IVisual {
// implementation of the IVisual.
private propertiesParser: PropertiesParser;
public update(options: VisualUpdateOptions): void {
// Parses properties.
this.propertiesParser = PropertiesParser.parse<PropertiesParser>(options.dataViews[0]);
}
/**
* This method will be executed only if the formatting panel is open.
*/
public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration {
return PropertiesParser.enumerateObjectInstances(this.propertiesParser, options);
}
}