Formázási eszközök
A formázási műveletek osztályokat, interfészeket és metódusokat tartalmaznak az értékek formázásához. Bővítő metódusokat is tartalmaz a sztringek feldolgozásához és az SVG-/HTML-dokumentumok szövegméretének méréséhez.
Szövegmérési szolgáltatás
A modul a következő függvényeket és interfészeket biztosítja:
TextProperties felület
Ez a felület a szöveg gyakori tulajdonságait ismerteti.
interface TextProperties {
text?: string;
fontFamily: string;
fontSize: string;
fontWeight?: string;
fontStyle?: string;
fontVariant?: string;
whiteSpace?: string;
}
measureSvgTextWidth
Ez a függvény adott SVG-szövegtulajdonságokkal méri a szöveg szélességét.
function measureSvgTextWidth(textProperties: TextProperties, text?: string): number;
Példa a következő használatára measureSvgTextWidth
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
import TextProperties = textMeasurementService.TextProperties;
// ...
let textProperties: TextProperties = {
text: "Microsoft PowerBI",
fontFamily: "sans-serif",
fontSize: "24px"
};
textMeasurementService.measureSvgTextWidth(textProperties);
// returns: 194.71875
measureSvgTextRect
Ez a függvény egy süllyesztett értéket ad vissza a megadott SVG-szövegtulajdonságokkal.
function measureSvgTextRect(textProperties: TextProperties, text?: string): SVGRect;
Példa a következő használatára measureSvgTextRect
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
import TextProperties = textMeasurementService.TextProperties;
// ...
let textProperties: TextProperties = {
text: "Microsoft PowerBI",
fontFamily: "sans-serif",
fontSize: "24px"
};
textMeasurementService.measureSvgTextRect(textProperties);
// returns: { x: 0, y: -22, width: 194.71875, height: 27 }
measureSvgTextHeight
Ez a függvény adott SVG-szövegtulajdonságokkal méri a szöveg magasságát.
function measureSvgTextHeight(textProperties: TextProperties, text?: string): number;
Példa a következő használatára measureSvgTextHeight
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
import TextProperties = textMeasurementService.TextProperties;
// ...
let textProperties: TextProperties = {
text: "Microsoft PowerBI",
fontFamily: "sans-serif",
fontSize: "24px"
};
textMeasurementService.measureSvgTextHeight(textProperties);
// returns: 27
estimateSvgTextBaselineDelta
Ez a függvény adott SVG-szövegtulajdonságok alapkonfigurációját adja vissza.
function estimateSvgTextBaselineDelta(textProperties: TextProperties): number;
Példa:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
import TextProperties = textMeasurementService.TextProperties;
// ...
let textProperties: TextProperties = {
text: "Microsoft PowerBI",
fontFamily: "sans-serif",
fontSize: "24px"
};
textMeasurementService.estimateSvgTextBaselineDelta(textProperties);
// returns: 5
estimateSvgTextHeight
Ez a függvény megbecsüli a szöveg magasságát adott SVG-szövegtulajdonságokkal.
function estimateSvgTextHeight(textProperties: TextProperties, tightFightForNumeric?: boolean): number;
Példa a következő használatára estimateSvgTextHeight
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
import TextProperties = textMeasurementService.TextProperties;
// ...
let textProperties: TextProperties = {
text: "Microsoft PowerBI",
fontFamily: "sans-serif",
fontSize: "24px"
};
textMeasurementService.estimateSvgTextHeight(textProperties);
// returns: 27
Lásd például az egyéni vizualizációs kódot.
measureSvgTextElementWidth
Ez a függvény az svgElement szélességét méri.
function measureSvgTextElementWidth(svgElement: SVGTextElement): number;
Példa a measureSvgTextElementWidth használatára:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
// ...
let svg: D3.Selection = d3.select("body").append("svg");
svg.append("text")
.text("Microsoft PowerBI")
.attr({
"x": 25,
"y": 25
})
.style({
"font-family": "sans-serif",
"font-size": "24px"
});
let textElement: D3.Selection = svg.select("text");
textMeasurementService.measureSvgTextElementWidth(textElement.node());
// returns: 194.71875
getMeasurementProperties
Ez a függvény lekéri az adott DOM-elem szövegmérési tulajdonságait.
function getMeasurementProperties(element: Element): TextProperties;
Példa a következő használatára getMeasurementProperties
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
// ...
let element: JQuery = $(document.createElement("div"));
element.text("Microsoft PowerBI");
element.css({
"width": 500,
"height": 500,
"font-family": "sans-serif",
"font-size": "32em",
"font-weight": "bold",
"font-style": "italic",
"white-space": "nowrap"
});
textMeasurementService.getMeasurementProperties(element.get(0));
/* returns: {
fontFamily:"sans-serif",
fontSize: "32em",
fontStyle: "italic",
fontVariant: "",
fontWeight: "bold",
text: "Microsoft PowerBI",
whiteSpace: "nowrap"
}*/
getSvgMeasurementProperties
Ez a függvény lekéri az adott SVG-szövegelem szövegmérési tulajdonságait.
function getSvgMeasurementProperties(svgElement: SVGTextElement): TextProperties;
Példa a következő használatára getSvgMeasurementProperties
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
// ...
let svg: D3.Selection = d3.select("body").append("svg");
let textElement: D3.Selection = svg.append("text")
.text("Microsoft PowerBI")
.attr({
"x": 25,
"y": 25
})
.style({
"font-family": "sans-serif",
"font-size": "24px"
});
textMeasurementService.getSvgMeasurementProperties(textElement.node());
/* returns: {
"text": "Microsoft PowerBI",
"fontFamily": "sans-serif",
"fontSize": "24px",
"fontWeight": "normal",
"fontStyle": "normal",
"fontVariant": "normal",
"whiteSpace": "nowrap"
}*/
getDivElementWidth
Ez a függvény egy div elem szélességét adja vissza.
function getDivElementWidth(element: JQuery): string;
Példa a következő használatára getDivElementWidth
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
// ...
let svg: Element = d3.select("body")
.append("div")
.style({
"width": "150px",
"height": "150px"
})
.node();
textMeasurementService.getDivElementWidth(svg)
// returns: 150px
getTailoredTextOrDefault
Összehasonlítja egy címke szövegméretét a rendelkezésre álló méretekkel, és három pontot jelenít meg, ha a rendelkezésre álló méret kisebb.
function getTailoredTextOrDefault(textProperties: TextProperties, maxWidth: number): string;
Példa a következő használatára getTailoredTextOrDefault
:
import { textMeasurementService } from "powerbi-visuals-utils-formattingutils";
import TextProperties = textMeasurementService.TextProperties;
// ...
let textProperties: TextProperties = {
text: "Microsoft PowerBI!",
fontFamily: "sans-serif",
fontSize: "24px"
};
textMeasurementService.getTailoredTextOrDefault(textProperties, 100);
// returns: Micros...
Sztringkiterjesztések
A modul a következő függvényeket biztosítja:
endsWith
Ez a függvény ellenőrzi, hogy egy sztring részsztringgel végződik-e.
function endsWith(str: string, suffix: string): boolean;
Példa a következő használatára endsWith
:
import { stringExtensions } from "powerbi-visuals-utils-formattingutils";
// ...
stringExtensions.endsWith("Power BI", "BI");
// returns: true
equalIgnoreCase
Ez a függvény a sztringeket hasonlítja össze, figyelmen kívül hagyva a kis- és nagybetűket.
function equalIgnoreCase(a: string, b: string): boolean;
Példa a következő használatára equalIgnoreCase
:
import { stringExtensions } from "powerbi-visuals-utils-formattingutils";
// ...
stringExtensions.equalIgnoreCase("Power BI", "power bi");
// returns: true
startsWith
Ez a függvény ellenőrzi, hogy egy sztring részsztringgel kezdődik-e.
function startsWith(a: string, b: string): boolean;
Példa a következő használatára startsWith
:
import { stringExtensions } from "powerbi-visuals-utils-formattingutils";
// ...
stringExtensions.startsWith("Power BI", "Power");
// returns: true
contains
Ez a függvény ellenőrzi, hogy egy sztring tartalmaz-e megadott részstringet.
function contains(source: string, substring: string): boolean;
Példa a metódus használatára contains
:
import { stringExtensions } from "powerbi-visuals-utils-formattingutils";
// ...
stringExtensions.contains("Microsoft Power BI Visuals", "Power BI");
// returns: true
isNullOrEmpty
Ellenőrzi, hogy egy sztring null vagy nem definiált vagy üres-e.
function isNullOrEmpty(value: string): boolean;
Példa a metódusra isNullOrEmpty
:
import { stringExtensions } from "powerbi-visuals-utils-formattingutils";
// ...
stringExtensions.isNullOrEmpty(null);
// returns: true
Értékformázás
A modul a következő függvényeket, interfészeket és osztályokat biztosítja:
IValueFormatter
Ez a felület a formázó nyilvános módszereit és tulajdonságait ismerteti.
interface IValueFormatter {
format(value: any): string;
displayUnit?: DisplayUnit;
options?: ValueFormatterOptions;
}
IValueFormatter.format
Ez a metódus formázja a megadott értéket.
function format(value: any, format?: string, allowFormatBeautification?: boolean): string;
Példák a következőre IValueFormatter.format
:
Az ezer formátum
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({ value: 1001 });
iValueFormatter.format(5678);
// returns: "5.68K"
A millió formátum
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({ value: 1e6 });
iValueFormatter.format(1234567890);
// returns: "1234.57M"
A milliárd formátum
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({ value: 1e9 });
iValueFormatter.format(1234567891236);
// returns: 1234.57bn
A trillió formátum
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({ value: 1e12 });
iValueFormatter.format(1234567891236);
// returns: 1.23T
A kitevő formátuma
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({ format: "E" });
iValueFormatter.format(1234567891236);
// returns: 1.234568E+012
A kultúraválasztó
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let valueFormatterUK = valueFormatter.create({ cultureSelector: "en-GB" });
valueFormatterUK.format(new Date(2007, 2, 3, 17, 42, 42));
// returns: 02/03/2007 17:42:42
let valueFormatterUSA = valueFormatter.create({ cultureSelector: "en-US" });
valueFormatterUSA.format(new Date(2007, 2, 3, 17, 42, 42));
// returns: 2/3/2007 5:42:42 PM
A százalékos formátum
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({ format: "0.00 %;-0.00 %;0.00 %" });
iValueFormatter.format(0.54);
// returns: 54.00 %
A dátumformátum
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let date = new Date(2016, 10, 28, 15, 36, 0),
iValueFormatter = valueFormatter.create({});
iValueFormatter.format(date);
// returns: 10/28/2016 3:36:00 PM
A logikai formátum
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({});
iValueFormatter.format(true);
// returns: True
A testre szabott pontosság
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
let iValueFormatter = valueFormatter.create({ value: 0, precision: 3 });
iValueFormatter.format(3.141592653589793);
// returns: 3.142
Lásd például az egyéni vizualizációs kódot.
ValueFormatterOptions
Ez az interfész az IValueFormattert és a create
függvény beállításait ismertetioptions
.
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
import ValueFormatterOptions = valueFormatter.ValueFormatterOptions;
interface ValueFormatterOptions {
/** The format string to use. */
format?: string;
/** The data value. */
value?: any;
/** The data value. */
value2?: any;
/** The number of ticks. */
tickCount?: any;
/** The display unit system to use */
displayUnitSystemType?: DisplayUnitSystemType;
/** True if we are formatting single values in isolation (e.g. card), as opposed to multiple values with a common base (e.g. chart axes) */
formatSingleValues?: boolean;
/** True if we want to trim off unnecessary zeroes after the decimal and remove a space before the % symbol */
allowFormatBeautification?: boolean;
/** Specifies the maximum number of decimal places to show*/
precision?: number;
/** Detect axis precision based on value */
detectAxisPrecision?: boolean;
/** Specifies the column type of the data value */
columnType?: ValueTypeDescriptor;
/** Specifies the culture */
cultureSelector?: string;
}
létrehozás
Ez a metódus létrehozza az IValueFormatter egy példányát.
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
import create = valueFormatter.create;
function create(options: ValueFormatterOptions): IValueFormatter;
Példa a létrehozás használatára
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
valueFormatter.create({});
// returns: an instance of IValueFormatter.
format
Másik lehetőség az érték formázására létrehozás IValueFormatter
nélkül. Dinamikus formátumú sztringgel rendelkező esetek esetén hasznos
import { format } from "powerbi-visuals-utils-formattingutils";
import format = valueFormatter.format;
function format(value: any, format?: string, allowFormatBeautification?: boolean, cultureSelector?: string): string;
Példa formátum használatára
import { valueFormatter } from "powerbi-visuals-utils-formattingutils";
const value = 12
const format = '¥ #,0'
valueFormatter.format(value, format);
// returns: formatted value as string (¥ 12)