数据可视化组件开发
visuals
当用户希望在项目中使用图表、图形、数据网格或其他数据可视化形式时使用。使用 VegaVisual 和 DataGrid 组件创建这些可视化,利用共享的 DataTable 输入、格式化、主题和交互能力。涵盖每个可视化默认获得的 VisualContainer 外观框架、宿主应用消费的 onInteraction 选择/点击事件、用于分层叠加和参考线的命名多表数据输入,以及 Vega-Lite 原生选择功能。
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 23
- Forks
- 5
01 前言 Front Matter
- name
- visuals
- description
- Use when user wants to incorporate charts, graphs, data grid, or other visual representations of data into their project. Use VegaVisual and DataGrid components to create these visuals, utilizing the shared DataTable input, formatting, theme, and interactivity. Covers the VisualContainer chrome every visual gets by default, the onInteraction selection/click events host apps consume, named multi-table data input for layered overlays and reference lines, and Vega-Lite native selections.
02 资源文件 7
- 参考文档references/data-grid-visual.md6.5 KB
- 参考文档references/data-table.md5.6 KB
- 参考文档references/flint-authoring.md12.1 KB
- 参考文档references/formatting.md6.4 KB
- 参考文档references/multi-data-input.md4.8 KB
- 参考文档references/vega-lite-visual.md4.7 KB
- 参考文档references/visual-container.md3.7 KB
03 SKILL.md 正文
Visuals
Types of visuals
There are 2 different types of visuals that can be used in a project:
- Charts and Graphs: These are used to represent data in a visual format, such as bar charts, line charts, pie charts, etc. These are built using vega-lite, see references/vega-lite-visual.md for more details.
- Data Grids: These are used to display tabular data in a structured format, allowing for sorting, filtering, and pagination. See references/data-grid-visual.md for more details.
Packages & Imports
The visual components are provided by three packages. Always use these package imports when creating visuals.
| Package | Primary exports | Example import |
|---|---|---|
@microsoft/fabric-visuals |
VegaVisual, types: VisualizationSpec, VegaLiteConfig, VegaVisualProps |
import { VegaVisual } from "@microsoft/fabric-visuals" |
@microsoft/fabric-datagrid |
DataGrid, types: GridColumnDef, Row, CellValue, DataGridProps, DataGridTheme, SortConfig |
import { DataGrid } from "@microsoft/fabric-datagrid" |
@microsoft/fabric-visuals-core |
isDataTable, convertDataTableToRows, design tokens |
import { isDataTable } from "@microsoft/fabric-visuals-core" |
The DataTable type (used by both components) is defined in @microsoft/fabric-visuals-core and re-exported by the visual packages' type definitions.
Data Format
The chart and data grid components share a unified data prop of type DataTable (from @microsoft/fabric-visuals-core). This structured format carries column metadata (displayName, format, semanticType) that the components use for axis titles, grid headers, number formatting, and tooltips.
Using DataTable: Pass a DataTable via the data prop. The visual uses its column metadata for formatting, axis titles, and tooltips.
Static/inline data: For static data, transformed data, or plain arrays, put data: { values: [...] } directly in the Vega-Lite spec and omit the data prop.
Multiple tables in one visual: the data prop also accepts a Record<string, DataTable> for specs that bind separate layers to more than one dataset by name such as layered overlays, reference lines, and axis spines. See references/multi-data-input.md.
import { useThemeContext } from "@/hooks/theme.context";
import { VegaVisual } from "@microsoft/fabric-visuals";
import { DataGrid } from "@microsoft/fabric-datagrid";
import type { DataTable } from "@microsoft/fabric-visuals-core";
const { theme } = useThemeContext();
// Charts — pass a DataTable and Vega-Lite spec
<VegaVisual spec={vegaLiteSpec} data={dataTable} theme={theme} />
// Grids — displayName becomes column headers, format applies to cells
<DataGrid data={dataTable} theme={theme} />
// Static/inline data — no data prop needed
const inlineSpec = {
data: { values: [{ x: 1, y: 2 }, { x: 3, y: 4 }] },
mark: "point",
encoding: { ... },
};
<VegaVisual spec={inlineSpec} theme={theme} />
For the DataTable schema and ColumnDef fields, see references/data-table.md.
VisualContainer defaults for every visual
VegaVisual and DataGrid wrap themselves in a VisualContainer by default. The container draws the chrome — border, padding, header (title/subtitle) — plus its built-in actions, all enabled by default. Do not add a wrapper of your own unless absolutely required.
<VegaVisual
spec={vegaLiteSpec}
data={dataTable}
theme={theme}
header={{ title: "Revenue by Region", subtitle: "Last 12 months" }}
/>
<DataGrid data={dataTable} theme={theme} header={{ title: "Top Products" }} />
Anything the default container doesn't cover — custom actions, programmatic capture, the same chrome around a non-visual etc: references/visual-container.md.
Formatting & Theme
- Formatting rules: Number formatting, color palettes, chart-specific encoding rules, highlighting guidelines, and a default theme. See references/formatting.md.
Custom visuals
Always use the above mentioned ways to create visual when possible. If the user's request doesn't allow creation using the above methods, ask the user if they are ok with using another library for creating the visual. If they are ok with it, use the library to create the visual. If they are not ok with it, then build that visual from scratch using HTML, CSS, and JS/TS. Make sure to ask the user for any specific requirements they have for the visual, such as colors, labels, etc.
Interactivity
Both VegaVisual and DataGrid expose an onInteraction prop that emits structured, predicate-based events when the user clicks a data point or row.
Always use the onInteraction prop on VegaVisual and DataGrid to surface user selections. The component only emits the selection; the host app decides what it does — e.g. coordinating other visuals or queries on the page.
A visual renders a layered subset by binding two named datasets to two layers (
data={{ all, highlighted }}) — see references/multi-data-input.md. The component renders whatever tables it is handed.
import type { InteractionEvent } from "@microsoft/fabric-visuals-core";
function handleInteraction(source: string, events: InteractionEvent[]) {
for (const event of events) {
if (event.action === "select") {
// event.selections describes the clicked data as predicates
} else if (event.action === "clear") {
// user deselected (re-clicked same item or clicked empty space)
}
}
}
<VegaVisual spec={spec} data={dataTable} theme={theme}
onInteraction={(events) => handleInteraction("salesChart", events)} />
<DataGrid data={dataTable} theme={theme}
onInteraction={(events) => handleInteraction("detailTable", events)} />
Key concepts:
- Clicking a different data point emits a new
select(replaces the prior selection — no precedingclear). - Re-clicking the same item or background space in a vega-lite visual emits
clear. - Predicates include all fields from the datum; consumers filter to the ones that are relevant to them.
04 同仓库更多 Skills
schema-discovery
Explore Power BI semantic model schemas using DAX INFO functions. Progressive metadata discovery strategy, scope estimation, and narrowing techniques for tables, columns, measures, and relationships.
query-design
Separates DAX data-fetching from TypeScript presentation. Guides when to use DAX vs. TypeScript vs. Vega-Lite for aggregation, total rows, FORMAT(), SELECTCOLUMNS, BLANK handling, filtering, multi-grain queries, cross-filtering and cross-highlight subset/overlay queries, and format strings.
Playwright 浏览器自动化测试
playwright-cli
自动化浏览器交互、测试网页,并使用 Playwright 开展测试。
Web 应用接入 Fabric 数据源
fabric-sdk
如何使用 @microsoft/fabric-app-data 在运行时将 Web 应用程序连接到 Fabric 数据源。构建基于浏览器的应用,通过 FabricClient API 使用 DAX 查询语义模型。
Fabric 数据源连接管理
fabric-cli
如何使用 fabric-app-data CLI 管理 Fabric 数据源连接。在 fabric.yaml 文件中通过配置档案维护工作区/项目 ID,通过 ID 或 Fabric 门户 URL 添加连接,并生成 TypeScript 配置以供 @microsoft/fabric-app-data 使用。
DAX 查询编写与测试
dax-authoring
针对 Power BI 语义模型编写和测试 DAX 查询。涵盖 DAX 语法规则、查询模式、时间智能,以及使用 Fabric CLI query 命令的迭代测试工作流。