共用方式為


依目標資料欄位排序視覺效果

在 Power BI 中,您可以依不同的資料欄位排序視覺效果,來變更視覺效果的外觀。 您可以藉由變更視覺效果的排序方式來強調所要傳達的資訊。 無論您是使用數值資料 (例如銷售數字) 或文字資料 (例如州名稱),都能以想要的方式來排序視覺效果。 Power BI 提供更多的排序彈性,以及可供您使用的快速功能表。 若要深入瞭解,請參閱 變更在 Power BI 報表中排序圖表的方式

顯示 Power BI 視覺效果的螢幕擷取畫面,其中開啟選項功能表並醒目提示排序命令。

您可以使用 visual.sortBy API 來變更依其中一個資料欄位排序視覺效果的方式。 您也可以控制排序的方向。

如何排序視覺效果

Power BI Client VisualDescriptor 類別會將 sortBy 方法定義為:

visual.sortBy(request: ISortByVisualRequest): Promise<void>

ISortByVisualRequest介面包含排序要求的定義:

export interface ISortByVisualRequest {
    orderBy: ITarget;
    direction: SortDirection;
}
  • orderBy,排序的目標資料欄位。 您可以在視覺效果 的選項功能表 的 [ 排序依據 ] 功能表命令底下,找到可排序視覺效果的資料欄位,深入瞭解 如何使用目標來選取要處理的資料欄位

    顯示 Power BI 視覺效果的螢幕擷取畫面,其中已開啟選項功能表,並展開 [排序依據] 子功能表。

  • direction,排序的方向。 列舉 SortDirection 會將排序方向 Ascending 定義為 或 Descending

    enum SortDirection {
        Ascending = 1,
        Descending = 2,
    }
    

範例

若要取得報表的頁面,請尋找使用中的頁面,並取得視覺效果。 找到視覺效果的唯一名稱 VisualContainer1 ,並依 Total Category Volume 資料表上的 SalesFact 量值遞減排序:

let pages = await report.getPages();

// Retrieve active page
var activePage = pages.find(function (page) { return page.isActive });

let visuals = await activePage.getVisuals();

// Retrieve target visual (replace "VisualContainer1" with requested visual name)
var visual = visuals.find(function (visual) { return visual.name === "VisualContainer1" });

const request = {
    // Set the target data field of the sort
    orderBy: {
        table: "SalesFact",
        measure: "Total Category Volume"
    },
    direction: models.SortDirection.Descending
};

await visual.sortBy(request);

若要依資料行目標排序視覺效果:

const request = {
  // Set the target data field of the sort
    orderBy: {
        table: "Store",
        column: "Name"
    },
    direction: models.SortDirection.Ascending
};

await visual.sortBy(request);

下一步