ChartUtils 是一組介面和方法,可用於建立 Power BI 視覺效果的軸、資料標籤和圖例。
安裝
若要安裝套件,您應該在具有目前視覺效果的目錄中執行下列命令:
npm install powerbi-visuals-utils-chartutils --save
軸協助程式
座標軸協助程式 (公用程式中的 axis 物件) 會提供函式來簡化具有座標軸的操作。
此課程模組提供下列函式:
getRecommendedNumberOfTicksForXAxis
此函式會根據圖表的寬度,傳回建議的刻度數目。
function getRecommendedNumberOfTicksForXAxis(availableWidth: number): number;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
axis.getRecommendedNumberOfTicksForXAxis(1024);
// returns: 8
getRecommendedNumberOfTicksForYAxis
此函式會根據圖表的高度,傳回建議的刻度數目。
function getRecommendedNumberOfTicksForYAxis(availableWidth: number);
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
axis.getRecommendedNumberOfTicksForYAxis(100);
// returns: 3
getBestNumberOfTicks
根據最小值、最大值和量值中繼資料與最大刻度計數,取得最佳的刻度計數。
function getBestNumberOfTicks(
min: number,
max: number,
valuesMetadata: DataViewMetadataColumn[],
maxTickCount: number,
isDateTime?: boolean
): number;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
var dataViewMetadataColumnWithIntegersOnly: powerbi.DataViewMetadataColumn[] = [
{
displayName: "col1",
isMeasure: true,
type: ValueType.fromDescriptor({ integer: true })
},
{
displayName: "col2",
isMeasure: true,
type: ValueType.fromDescriptor({ integer: true })
}
];
var actual = axis.getBestNumberOfTicks(
0,
3,
dataViewMetadataColumnWithIntegersOnly,
6
);
// returns: 4
getTickLabelMargins
此函式會傳回刻度標籤的邊界。
function getTickLabelMargins(
viewport: IViewport,
yMarginLimit: number,
textWidthMeasurer: ITextAsSVGMeasurer,
textHeightMeasurer: ITextAsSVGMeasurer,
axes: CartesianAxisProperties,
bottomMarginLimit: number,
properties: TextProperties,
scrollbarVisible?: boolean,
showOnRight?: boolean,
renderXAxis?: boolean,
renderY1Axis?: boolean,
renderY2Axis?: boolean
): TickLabelMargins;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
axis.getTickLabelMargins(
plotArea,
marginLimits.left,
TextMeasurementService.measureSvgTextWidth,
TextMeasurementService.estimateSvgTextHeight,
axes,
marginLimits.bottom,
textProperties,
/*scrolling*/ false,
showY1OnRight,
renderXAxis,
renderY1Axis,
renderY2Axis
);
// returns: xMax, yLeft, yRight, stackHeigh;
isOrdinal
檢查字串是否為 Null、未定義,或空白。
function isOrdinal(type: ValueTypeDescriptor): boolean;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
let type = ValueType.fromDescriptor({ misc: { barcode: true } });
axis.isOrdinal(type);
// returns: true
isDateTime
檢查值是否為 DateTime 類型。
function isDateTime(type: ValueTypeDescriptor): boolean;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
axis.isDateTime(ValueType.fromDescriptor({ dateTime: true }));
// returns: true
getCategoryThickness
使用 D3 比例來取得實際的類別粗細。
function getCategoryThickness(scale: any): number;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
let range = [0, 100];
let domain = [0, 10];
let scale = d3.scale
.linear()
.domain(domain)
.range(range);
let actualThickness = axis.getCategoryThickness(scale);
invertOrdinalScale
此函式會反轉序數比例。 如果 x < scale.range()[0],則會傳回 scale.domain()[0]。 否則,會傳回 scale.domain() 中 <= x 的最大項目。
function invertOrdinalScale(scale: d3.scale.Ordinal<any, any>, x: number);
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
let domain: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
pixelSpan: number = 100,
ordinalScale: d3.scale.ordinal = axis.createOrdinalScale(
pixelSpan,
domain,
0.4
);
axis.invertOrdinalScale(ordinalScale, 49);
// returns: 4
findClosestXAxisIndex
此函式會尋找並傳回最接近的 X 軸索引。
function findClosestXAxisIndex(
categoryValue: number,
categoryAxisValues: AxisHelperCategoryDataPoint[]
): number;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
/**
* Finds the index of the category of the given x coordinate given.
* pointX is in non-scaled screen-space, and offsetX is in render-space.
* offsetX does not need any scaling adjustment.
* @param {number} pointX The mouse coordinate in screen-space, without scaling applied
* @param {number} offsetX Any left offset in d3.scale render-space
* @return {number}
*/
private findIndex(pointX: number, offsetX?: number): number {
// we are using mouse coordinates that do not know about any potential CSS transform scale
let xScale = this.scaleDetector.getScale().x;
if (!Double.equalWithPrecision(xScale, 1.0, 0.00001)) {
pointX = pointX / xScale;
}
if (offsetX) {
pointX += offsetX;
}
let index = axis.invertScale(this.xAxisProperties.scale, pointX);
if (this.data.isScalar) {
// When we have scalar data the inverted scale produces a category value, so we need to search for the closest index.
index = axis.findClosestXAxisIndex(index, this.data.categoryData);
}
return index;
}
diffScaled
此函式會計算並傳回比例中值的差額。
function diffScaled(
scale: d3.scale.Linear<any, any>,
value1: any,
value2: any
): number;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
var scale: d3.scale.Linear<number, number>,
range = [0, 999],
domain = [0, 1, 2, 3, 4, 5, 6, 7, 8, 999];
scale = d3.scale.linear()
.range(range)
.domain(domain);
return axis.diffScaled(scale, 0, 0));
// returns: 0
createDomain
此函式會為座標軸建立值定義域。
function createDomain(
data: any[],
axisType: ValueTypeDescriptor,
isScalar: boolean,
forcedScalarDomain: any[],
ensureDomain?: NumberRange
): number[];
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
var cartesianSeries = [
{
data: [
{ categoryValue: 7, value: 11, categoryIndex: 0, seriesIndex: 0 },
{
categoryValue: 9,
value: 9,
categoryIndex: 1,
seriesIndex: 0
},
{
categoryValue: 15,
value: 6,
categoryIndex: 2,
seriesIndex: 0
},
{ categoryValue: 22, value: 7, categoryIndex: 3, seriesIndex: 0 }
]
}
];
var domain = axis.createDomain(
cartesianSeries,
ValueType.fromDescriptor({ text: true }),
false,
[]
);
// returns: [0, 1, 2, 3]
getCategoryValueType
此函式會取得類別資料行的 ValueType。 如果類型不存在,則預設值為 Text。
function getCategoryValueType(
data: any[],
axisType: ValueTypeDescriptor,
isScalar: boolean,
forcedScalarDomain: any[],
ensureDomain?: NumberRange
): number[];
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
var cartesianSeries = [
{
data: [
{ categoryValue: 7, value: 11, categoryIndex: 0, seriesIndex: 0 },
{
categoryValue: 9,
value: 9,
categoryIndex: 1,
seriesIndex: 0
},
{
categoryValue: 15,
value: 6,
categoryIndex: 2,
seriesIndex: 0
},
{ categoryValue: 22, value: 7, categoryIndex: 3, seriesIndex: 0 }
]
}
];
axis.getCategoryValueType(
cartesianSeries,
ValueType.fromDescriptor({ text: true }),
false,
[]
);
// returns: [0, 1, 2, 3]
createAxis
此函式會建立包含比例的 D3 軸。 可以是垂直或水平,且必須是 datetime、numeric 或 text。
function createAxis(options: CreateAxisOptions): IAxisProperties;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
// ...
var dataPercent = [0.0, 0.33, 0.49];
var formatStringProp: powerbi.DataViewObjectPropertyIdentifier = {
objectName: "general",
propertyName: "formatString"
};
let metaDataColumnPercent: powerbi.DataViewMetadataColumn = {
displayName: "Column",
type: ValueType.fromDescriptor({ numeric: true }),
objects: {
general: {
formatString: "0 %"
}
}
};
var os = axis.createAxis({
pixelSpan: 100,
dataDomain: [dataPercent[0], dataPercent[2]],
metaDataColumn: metaDataColumnPercent,
formatString: valueFormatter.getFormatString(
metaDataColumnPercent,
formatStringProp
),
outerPadding: 0.5,
isScalar: true,
isVertical: true
});
applyCustomizedDomain
此函式集會設定自訂的定義域,但不會在未設定任何內容時變更。
function applyCustomizedDomain(customizedDomain: any[], forcedDomain: any[]): any[];
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
let customizedDomain = [undefined, 20],
existingDomain = [0, 10];
axis.applyCustomizedDomain(customizedDomain, existingDomain);
// returns: {0:0, 1:20}
combineDomain
如果設定了其中一個值,則此函式會將強制定義域與實際的定義域結合。 forcedDomain 是第一要務。 如果有任何參考點需要該定義域,則加以擴充。
function combineDomain(
forcedDomain: any[],
domain: any[],
ensureDomain?: NumberRange
): any[];
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
let forcedYDomain = this.valueAxisProperties
? [this.valueAxisProperties["secStart"], this.valueAxisProperties["secEnd"]]
: null;
let xDomain = [minX, maxX];
axis.combineDomain(forcedYDomain, xDomain, ensureXDomain);
powerOfTen
此函式會指出數字是否為 10 的乘冪。
function powerOfTen(d: any): boolean;
範例:
import { axis } from "powerbi-visuals-utils-chartutils";
// ...
axis.powerOfTen(10);
// returns: true
DataLabelManager
DataLabelManager 有助於建立和維護標籤。 其使用錨點或矩形來排列標籤項目。 可能會自動偵測衝突,以重新放置或隱藏項目。
DataLabelManager 類別提供下列方法:
hideCollidedLabels
這個方法會根據標籤大小和重疊,在畫布上排列標籤的位置和可見度。
function hideCollidedLabels(
viewport: IViewport,
data: any[],
layout: any,
addTransform: boolean = false
hideCollidedLabels?: boolean
): LabelEnabledDataPoint[];
範例:
let dataLabelManager = new DataLabelManager();
let filteredData = dataLabelManager.hideCollidedLabels(
this.viewport,
values,
labelLayout,
true,
true
);
IsValid
這個靜態方法會檢查提供的矩形是否有效 (也就是寬度和高度為正數)。
function isValid(rect: IRect): boolean;
範例:
let rectangle = {
left: 150,
top: 130,
width: 120,
height: 110
};
DataLabelManager.isValid(rectangle);
// returns: true
DataLabelUtils
DataLabelUtils 提供用來處理資料標籤的公用程式。
此方法會提供下列函式、介面與類別:
getLabelPrecision
此函式會根據提供的格式計算精確度。
function getLabelPrecision(precision: number, format: string): number;
getLabelFormattedText
此函式會根據提供的格式傳回格式精確度。
function getLabelFormattedText(options: LabelFormattedTextOptions): string;
範例:
import { dataLabelUtils } from "powerbi-visuals-utils-chartutils";
// ...
let options: LabelFormattedTextOptions = {
text: "some text",
fontFamily: "sans",
fontSize: "15",
fontWeight: "normal"
};
dataLabelUtils.getLabelFormattedText(options);
enumerateDataLabels
此函式會傳回資料標籤的 VisualObjectInstance。
function enumerateDataLabels(
options: VisualDataLabelsSettingsOptions
): VisualObjectInstance;
enumerateCategoryLabels
此函式會將類別資料標籤的 VisualObjectInstance 新增至列舉物件。
function enumerateCategoryLabels(
enumeration: VisualObjectInstanceEnumerationObject,
dataLabelsSettings: VisualDataLabelsSettings,
withFill: boolean,
isShowCategory: boolean = false,
fontSize?: number
): void;
createColumnFormatterCacheManager
此函式會傳回可讓您快速存取格式化標籤的快取管理員。
function createColumnFormatterCacheManager(): IColumnFormatterCacheManager;
範例:
import { dataLabelUtils } from "powerbi-visuals-utils-chartutils";
// ...
let value: number = 200000;
labelSettings.displayUnits = 1000000;
labelSettings.precision = 1;
let formattersCache = DataLabelUtils.createColumnFormatterCacheManager();
let formatter = formattersCache.getOrCreate(null, labelSettings);
let formattedValue = formatter.format(value);
// formattedValue == "0.2M"
Legend 服務
Legend 服務會提供協助程式介面,以用來建立和管理 Power BI 視覺效果的 Power BI 圖例。
此課程模組提供下列函式與介面:
createLegend
此協助程式函式可簡化 Power BI 自訂視覺效果圖例的建立工作。
function createLegend(
legendParentElement: HTMLElement, // top visual element, container in which legend will be created
isScrollable: boolean = false, // indicates that legend could be scrollable or not
legendPosition: LegendPosition = LegendPosition.Top // Position of the legend inside of legendParentElement container
): ILegend;
範例:
public constructor(options: VisualConstructorOptions) {
this.visualInitOptions = options;
this.layers = [];
var element = this.element = options.element;
var viewport = this.currentViewport = options.viewport;
var hostServices = options.host;
//... some other init calls
this.legend = createLegend(
element,
true);
}
ILegend
這個介面會實作圖例建立所需的所有方法。
export interface ILegend {
getMargins(): IViewport;
isVisible(): boolean;
changeOrientation(orientation: LegendPosition): void; // processing legend orientation
getOrientation(): LegendPosition; // get information about current legend orientation
drawLegend(data: LegendData, viewport: IViewport); // all legend rendering code is placing here
/**
* Reset the legend by clearing it
*/
reset(): void;
}
drawLegend
此函式會使用指定的 SVG 文字屬性來測量文字的高度。
function drawLegend(data: LegendData, viewport: IViewport): void;
範例:
private renderLegend(): void {
if (!this.isInteractive) {
let legendObjectProperties = this.data.legendObjectProperties;
if (legendObjectProperties) {
let legendData = this.data.legendData;
LegendData.update(legendData, legendObjectProperties);
let position = <string>legendObjectProperties[legendProps.position];
if (position)
this.legend.changeOrientation(LegendPosition[position]);
this.legend.drawLegend(legendData, this.parentViewport);
} else {
this.legend.changeOrientation(LegendPosition.Top);
this.legend.drawLegend({ dataPoints: [] }, this.parentViewport);
}
}
}