Important
這項功能目前處於 公開預覽版。
自訂視覺化讓你能在 AI/BI 儀表板中自訂圖表,超越內建的視覺化類型。 自訂視覺化則使用 Vega-Lite 函式庫來渲染 JSON 規範中的圖表。
自訂視覺化可以渲染超越內建選項的專業圖表類型。 以下系統發育樹採用力導向的佈局,將相關紀錄圍繞共享的群組和家族中心分組:
要逐步建立圖表,請參閱 範例視覺化。 欲參考其他圖表類型的規格,請參見 更多圖表規格。
建立自訂視覺化
要建立自訂視覺化:
- 選擇一個資料集。
- 在視覺化設定窗格中,選擇進階視覺化區塊下的自訂視覺化。
- 在 欄位 區塊中,加入你想使用的欄位。 每個欄位都有獨特的 名稱。 在你的 Vega-Lite 規格中參照欄位時,請使用這些名稱。
- 在 Vega-Lite 規範 編輯器中輸入你的 Vega-Lite JSON 規範。
參考資料集欄位
可透過以下其中一種方式參照 Vega-Lite 規範中的欄位:
請使用
"field": "{columnName}"。 以下範例將xField欄位指派至x軸:"encoding": { "x": { "field": "xField", "type": "quantitative" } }在表達式中,使用
datum["{columnName}"]或datum.{columnName}。 以下範例示範如何從x和r欄位定義新的angle欄位:{ "calculate": "datum.r * cos(datum.angle)", "as": "x" }
更多資訊請參閱 datum Vega 表達式文件。
繼承儀表板主題
自訂視覺化會自動適應儀表板主題,包括光暗模式。 以下圖表元素可繼承主題值,且不更改你的規格:
- 軸、圖例、標題和標題字型都與儀表板設定的字型相符。
- 軸線與目前使用中的模式儀表板的格線顏色相符。
- 圖表背景在小工具的主題背景之上呈透明狀態。
你在規格 config 區塊中定義的設定優先於繼承的預設值。
表達式中的參考主題值
若要使用具主題感知的值來設定標記樣式,請在 Vega-Lite 表達式({ "expr": "..." })中參照下列訊號:
| 訊號 | Description |
|---|---|
colors |
主動模式的預先解析色彩標記。 使用這些參數表示常見的值,如 colors.textPrimary.default、 colors.gridColor、 colors.markHighlightColor和 。 不要將這些標記為 [mode];它們已經被解決。 |
mode |
目前使用中的色彩模式,為 'light' 或 'dark'。 用它來為提供各模式變體的 dashboardTheme 欄位建立索引。 |
dashboardTheme |
由儀表板擁有者設定的完整主題,包括字型()、resolvedFontSettings分類調色盤(visualizationColors)、以及各模式顏色(gridLineColor)。 具有各模式變體的欄位需要索引 [mode] 。 |
colors 和 dashboardTheme 訊號彼此獨立。 訊號 colors 提供已解析為使用中的模式的便利權杖,同時 dashboardTheme 揭露完整的擁有者設定主題。 先使用 colors,再使用 dashboardTheme 來設定字型、完整調色盤,或任何 colors 未提供的值。
以下範例示範如何參考主題值:
使用儀表板的正體字體作為文字標記:
{ "expr": "dashboardTheme.resolvedFontSettings.fieldValue.fontFamily" }使用儀表板標題的顏色來表示主動模式:
{ "expr": "dashboardTheme.resolvedFontSettings.fieldTitle.fontColor[mode]" }使用儀表板分類調色盤中的顏色:
{ "expr": "dashboardTheme.visualizationColors[0]" }
注意
colors 代幣已針對目前使用中的模式完成解析。 跳 [mode] 過索引並使用 colors.markHighlightColor,而非 colors.markHighlightColor[mode]。 在 dashboardTheme 下具有各模式變體的欄位,例如 dashboardTheme.gridLineColor[mode],需要 [mode] 索引。
預設互動
自訂視覺化支援交叉篩選、選取醒目提示及向下鑽取,無需更改您的 Vega-Lite 規格。
-
按一下即可交叉篩選:當檢視者點擊標記時,選取會篩選儀表板上使用相同資料集的其他小工具。 所選內容包含了小工具配置中 欄位 區塊的所有非聚合欄位。 彙總的指標,如
SUM(...)或AVG(...),不會納入選取。 如果每個欄位都是彙總,點擊標記不會交叉過濾。 -
高亮選取:當檢視者選擇標記時,未選取的標記會變暗。填充標記,如
bar、rect、square、geoshape、arc、circle,以及填充point標記,也會以主題高亮顏色呈現輪廓。 線、面積、軌跡、尺規線、勾選和未填點標記僅調暗。 如果你的規格已經在標記或其編碼上設定了fillOpacity、opacity、stroke或strokeWidth,你的值會優先。 - 以滑鼠右鍵進行鑽研:瀏覽者可以在標記上按一下滑鼠右鍵,然後按一下 Drill to,以開啟依該標記篩選的另一個頁面。 鑽孔式選取與交叉濾波器選取使用相同的欄位。 若要設定目標頁面,請參閱 使用鑽研來篩選資料。
儀表板僅將預設交叉篩選選取新增至具有單一視圖的規格:頂層 mark,且沒有 layer、concat、facet、hconcat、vconcat 或 repeat。 其他規格則請自行宣告選擇參數。 請參閱 依選取內容篩選其他小工具。 當你宣告參數時,控制面板會在規格的每個視圖中標示該選擇。 穿透分析也適用於分層與多視圖規格。
預設互動性不會新增工具提示。 若要顯示提示,請在標記上設定 "tooltip": true 或新增 tooltip 編碼。
關閉預設交叉篩選
如果你的規格本身定義了點擊互動,請關閉預設交叉篩選,讓儀表板依照規格原樣呈現:
- 在視覺化設定面板中開啟自訂視覺化。
- 關閉 開啟預設交叉濾波器。
當設定關閉時,儀表板不會新增選擇參數或醒目提示編碼到你的規格中。 以下行為保持不變:
- 你宣告的參數
databricks_mark_selection仍然會交叉過濾儀表板。 - 以滑鼠右鍵執行鑽研分析仍然可用。
根據所選項目篩選其他小工具
要控制點擊選擇哪些欄位,或從分層或多視圖規格交叉過濾,請宣告一個名稱為 databricks_mark_selection 的保留點選擇參數。 渲染器會偵測到這個名稱,並將選取的名稱與儀表板的交叉濾波狀態連結起來。 當你的規格宣告這個參數時,儀表板會用它來取代預設的交叉濾波器選擇。 向下鑽研也會使用參數列出的欄位。
"params": [
{
"name": "databricks_mark_selection",
"select": { "type": "point", "fields": ["categoryName"] }
}
]
下列為適用需求:
- 參數
name必須恰好databricks_mark_selection為 。 其他名稱被視為規則參數,不會驅動交叉濾波。 -
select.type必須是point。 區間(筆刷)選取不支援用作交叉篩選來源。 帶有select.type: "interval"的參數會顯示警告 不支援區間交叉篩選,且不會交叉篩選。 -
select.fields必須列出小工具設定中欄位區段中定義的欄位名稱,而非原始欄位名稱。 - 僅在
select.fields中列出維度(分組)欄位。 聚合的度量,如SUM(...)或AVG(...),無法驅動交叉濾波器。 - 要在多個欄位中選擇,請將它們一併列出:
"fields": ["categoryName", "regionName"]。
醒目提示選取的標記
控制面板預設會反白顯示選取的標記。 詳見 預設互動。 若要自訂高亮,請在填色型標記(例如 bar、arc 或 rect)上使用 stroke 和 strokeWidth 條件,並將 color 編碼繫結至你的欄位。 使用 { "expr": "colors.markHighlightColor" } 作為筆劃,讓醒目提示在淺色和深色模式下都能清晰可辨。
以下範例示範在點擊某個長條時,如何依據 categoryName 篩選儀表板的其餘部分。 選取的長條會套用符合主題的描邊,而未選取的長條則會變暗,但仍保留其色彩編碼。
JSON 規範
{
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"data": { "name": "databricks_query" },
"width": "container",
"height": "container",
"config": { "autosize": { "type": "fit", "contains": "padding" } },
"params": [
{
"name": "databricks_mark_selection",
"select": { "type": "point", "fields": ["categoryName"] }
}
],
"mark": { "type": "bar", "stroke": null },
"encoding": {
"x": { "field": "categoryName", "type": "nominal" },
"y": { "field": "salesValue", "type": "quantitative" },
"color": { "field": "categoryName", "type": "nominal" },
"fillOpacity": {
"condition": { "param": "databricks_mark_selection", "value": 1 },
"value": 0.3
},
"stroke": {
"condition": {
"param": "databricks_mark_selection",
"empty": false,
"value": { "expr": "colors.markHighlightColor" }
},
"value": null
},
"strokeWidth": {
"condition": { "param": "databricks_mark_selection", "empty": false, "value": 2 },
"value": 0
}
}
}
自動調整圖表大小
要讓圖表調整大小以符合其容器,請在規格的頂層加入以下設定:
"width": "container",
"height": "container",
"config": {
"autosize": {
"type": "fit",
"contains": "padding"
}
}
範例視覺化
以下範例將逐步說明從簡單的分層圖表到更進階的親緣樹,建立自訂視覺化。
帶有滾動均值的分層圖表
此範例建立一個分層圖表,利用 Databricks 樣本資料集的天氣資料,繪製原始溫度點並以滾動平均線覆蓋。
請建立一個包含以下查詢的資料集:
SELECT date, temperature AS temp_max FROM samples.accuweather.historical_hourly_imperial WHERE city_name = 'singapore' ORDER BY date;在視覺化設定窗格中,在 進階選項中選擇 自訂視覺化。
選擇你在前一步建立的資料集。
在 欄位 區塊中,新增日期欄位欄位,並將其 名稱 設為
date。新增一個溫度欄位,並將其 名稱 設為
temp_max。將以下規範複製到 Vega-Lite 規範 編輯器中。 如果 x 軸被裁切,請參考 自動調整圖表大小。
JSON 規範
{ "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "width": "container", "height": "container", "config": { "autosize": { "type": "fit", "contains": "padding" } }, "data": { "name": "databricks_query" }, "transform": [ { "window": [{ "field": "temp_max", "op": "mean", "as": "rolling_mean" }], "frame": [-15, 15] } ], "encoding": { "x": { "field": "date", "type": "temporal", "title": "Date" }, "y": { "type": "quantitative", "scale": { "zero": false }, "axis": { "title": "Max temperature and rolling mean" } } }, "layer": [ { "mark": { "type": "point", "opacity": 0.3 }, "encoding": { "y": { "field": "temp_max", "title": "Max temperature" } } }, { "mark": { "type": "line", "color": "red", "size": 3 }, "encoding": { "y": { "field": "rolling_mean", "title": "Rolling mean of max temperature" } } } ] }
系統發生樹
此範例使用一小組樣本資料來建立本頁頂端所示的系統發生樹。 Vega-Lite 會在你提供的座標上繪製點,但不會計算網路圖本身的節點位置。 這個計算在查詢中進行,資料到達圖表前,查詢會預先計算每個點的 x 和 y 座標。 查詢會輸出三種記錄類型,規範會將它們渲染為獨立的圖層:path 列繪製彎曲的分支,node 列繪製葉片圓圈,以及 label 列用於定位群組標籤。
下載範例資料。 每一列代表一個化石發現,其挖掘地點座標由
dig_longitude和dig_latitude提供。按一下側邊欄中的
[儀表板]。點擊 建立儀表板。
按一下 資料 索引標籤。
點選 新增資料,再點 上傳資料。
將下載的檔案放入從檔案上傳建立或修改資料表窗格。
選擇你想存放資料表的 資料目錄 和 架構,然後輸入資料表名稱。
點擊 [建立資料表]。 完整表格會自動加入為資料集。
點選新增 SQL 資料集,然後貼上該查詢。 在查詢的
FROM子句中,將目錄、結構和資料表名稱替換成你建立資料表時使用的名稱。請依照「 建立自訂視覺化」中的步驟建立自訂視覺化。 在欄位區塊中,請以每個欄位名稱作為名稱,新增以下欄位:
record_type、x、y、fossil_tag、point_order、suborder、roar_score、clade_label、genus_symbol、family_taxon和label_text。 用 規格說明 作為你的 Vega-Lite JSON。
SQL 查詢
WITH base AS (
SELECT
fossil_tag,
genus_symbol,
clade_label,
suborder,
family_taxon,
dig_longitude,
dig_latitude,
roar_score
FROM
`my_catalog`.`default`.`dino_table`
),
root_pt AS (
SELECT
AVG(dig_longitude) AS rx,
AVG(dig_latitude) AS ry
FROM
base
),
group_pts AS (
SELECT
suborder,
AVG(dig_longitude) AS gx,
AVG(dig_latitude) AS gy
FROM
base
GROUP BY
suborder
),
family_pts AS (
SELECT
suborder,
family_taxon,
AVG(dig_longitude) AS fx,
AVG(dig_latitude) AS fy
FROM
base
GROUP BY
suborder,
family_taxon
),
ctx AS (
SELECT
b.fossil_tag,
b.suborder,
b.family_taxon,
b.clade_label,
b.genus_symbol,
b.dig_longitude,
b.dig_latitude,
b.roar_score,
r.rx,
r.ry,
g.gx,
g.gy,
f.fx,
f.fy
FROM
base b
CROSS JOIN root_pt r
JOIN group_pts g
ON b.suborder = g.suborder
JOIN family_pts f
ON b.suborder = f.suborder
AND b.family_taxon = f.family_taxon
),
waypoints AS (
SELECT
fossil_tag,
suborder,
family_taxon,
clade_label,
genus_symbol,
roar_score,
0 AS point_order,
rx AS x,
ry AS y
FROM
ctx
UNION ALL
SELECT
fossil_tag,
suborder,
family_taxon,
clade_label,
genus_symbol,
roar_score,
1,
rx + 0.30 * (gx - rx),
ry + 0.30 * (gy - ry)
FROM
ctx
UNION ALL
SELECT
fossil_tag,
suborder,
family_taxon,
clade_label,
genus_symbol,
roar_score,
2,
gx + 0.15 * (fx - gx),
gy + 0.15 * (fy - gy)
FROM
ctx
UNION ALL
SELECT
fossil_tag,
suborder,
family_taxon,
clade_label,
genus_symbol,
roar_score,
3,
gx + 0.50 * (fx - gx) + 0.03 * (dig_longitude - fx),
gy + 0.50 * (fy - gy) + 0.03 * (dig_latitude - fy)
FROM
ctx
UNION ALL
SELECT
fossil_tag,
suborder,
family_taxon,
clade_label,
genus_symbol,
roar_score,
4,
fx + 0.15 * (dig_longitude - fx),
fy + 0.15 * (dig_latitude - fy)
FROM
ctx
UNION ALL
SELECT
fossil_tag,
suborder,
family_taxon,
clade_label,
genus_symbol,
roar_score,
5,
fx + 0.65 * (dig_longitude - fx),
fy + 0.65 * (dig_latitude - fy)
FROM
ctx
UNION ALL
SELECT
fossil_tag,
suborder,
family_taxon,
clade_label,
genus_symbol,
roar_score,
6,
dig_longitude,
dig_latitude
FROM
ctx
)
SELECT
fossil_tag,
suborder,
point_order,
x,
y,
family_taxon,
clade_label,
genus_symbol,
roar_score,
'path' AS record_type,
CAST(NULL AS STRING) AS label_text
FROM
waypoints
UNION ALL
SELECT
fossil_tag,
suborder,
99 AS point_order,
dig_longitude AS x,
dig_latitude AS y,
family_taxon,
clade_label,
genus_symbol,
roar_score,
'node' AS record_type,
CAST(NULL AS STRING) AS label_text
FROM
base
UNION ALL
SELECT
g.suborder AS fossil_tag,
g.suborder AS suborder,
100 AS point_order,
g.gx
+ (g.gx - r.rx)
* CASE LOWER(g.suborder)
WHEN 'titanosauria' THEN 0.10
WHEN 'hadrosauria' THEN 0.25
WHEN 'ornithopoda' THEN 1.30
WHEN 'stegosauria' THEN 0.73
ELSE 0.55
END AS x,
g.gy
+ (g.gy - r.ry)
* CASE LOWER(g.suborder)
WHEN 'titanosauria' THEN 0.10
WHEN 'hadrosauria' THEN 0.25
WHEN 'ornithopoda' THEN 1.30
WHEN 'stegosauria' THEN 0.73
ELSE 0.55
END AS y,
CAST(NULL AS STRING) AS family_taxon,
CAST(NULL AS STRING) AS clade_label,
CAST(NULL AS STRING) AS genus_symbol,
CAST(NULL AS DOUBLE) AS roar_score,
'label' AS record_type,
g.suborder AS label_text
FROM
group_pts g CROSS JOIN root_pt r
JSON 規範
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": { "name": "databricks_query" },
"width": "container",
"height": "container",
"background": "white",
"config": {
"autosize": { "type": "fit", "contains": "padding" },
"view": { "stroke": null }
},
"layer": [
{
"transform": [{ "filter": "datum.record_type === 'path'" }],
"mark": {
"type": "line",
"interpolate": "basis",
"strokeCap": "round",
"strokeJoin": "round"
},
"encoding": {
"x": {
"field": "x",
"type": "quantitative",
"axis": null,
"scale": { "zero": false, "nice": false }
},
"y": {
"field": "y",
"type": "quantitative",
"axis": null,
"scale": { "zero": false, "nice": false }
},
"detail": { "field": "fossil_tag", "type": "nominal" },
"order": { "field": "point_order", "type": "quantitative" },
"color": {
"field": "suborder",
"type": "nominal",
"scale": { "scheme": "tableau10" },
"legend": null
},
"opacity": { "value": 0.3 },
"strokeWidth": { "value": 0.8 }
}
},
{
"transform": [{ "filter": "datum.record_type === 'node'" }],
"mark": {
"type": "circle",
"opacity": 0.9,
"stroke": "white",
"strokeWidth": 0.3
},
"encoding": {
"x": {
"field": "x",
"type": "quantitative",
"axis": null,
"scale": { "zero": false, "nice": false }
},
"y": {
"field": "y",
"type": "quantitative",
"axis": null,
"scale": { "zero": false, "nice": false }
},
"color": {
"field": "suborder",
"type": "nominal",
"scale": { "scheme": "tableau10" },
"legend": { "title": "Suborder", "orient": "right" }
},
"size": {
"field": "roar_score",
"type": "quantitative",
"scale": { "domain": [0, 100], "range": [4, 150] },
"legend": null
},
"tooltip": [
{ "field": "clade_label", "title": "Clade" },
{ "field": "genus_symbol", "title": "Symbol" },
{ "field": "suborder", "title": "Suborder" },
{ "field": "family_taxon", "title": "Family" },
{ "field": "roar_score", "title": "Roar Score", "format": ".1f" }
]
}
},
{
"transform": [{ "filter": "datum.record_type === 'label'" }],
"mark": {
"type": "text",
"fontSize": 12,
"fontWeight": "bold",
"opacity": 0.85
},
"encoding": {
"x": {
"field": "x",
"type": "quantitative",
"axis": null,
"scale": { "zero": false, "nice": false }
},
"y": {
"field": "y",
"type": "quantitative",
"axis": null,
"scale": { "zero": false, "nice": false }
},
"text": { "field": "label_text", "type": "nominal" },
"color": {
"field": "suborder",
"type": "nominal",
"scale": { "scheme": "tableau10" },
"legend": null
}
}
}
]
}
更多圖表規格
以下規格展示了無法作為內建視覺化類型提供的圖表。 更多範例請參見 Vega-Lite 範例畫廊。
子彈符號表
在categoryField區段中定義 currentField、paceField、targetField 和 。
JSON 規範
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": "container",
"height": "container",
"data": { "name": "databricks_query" },
"config": {
"autosize": { "type": "fit", "contains": "padding" }
},
"transform": [
{
"fold": ["targetField", "paceField", "currentField"],
"as": ["measure_name", "measure_value"]
},
{
"calculate": "toNumber(datum.measure_value)",
"as": "measure_value"
},
{
"calculate": "{ \"targetField\": \"Target\", \"paceField\": \"Pace\", \"currentField\": \"Current\" }[datum.measure_name]",
"as": "measure_label"
},
{
"calculate": "indexof([\"Target\", \"Pace\", \"Current\"], datum.measure_label)",
"as": "measure_order"
}
],
"layer": [
{
"mark": "bar",
"params": [
{
"name": "legend_click",
"select": { "type": "point", "fields": ["measure_label"] },
"bind": "legend"
}
],
"encoding": {
"color": { "field": "measure_label" },
"opacity": { "value": 0 }
}
},
{
"transform": [{ "filter": { "param": "legend_click" } }],
"layer": [
{
"layer": [
{
"mark": { "type": "bar", "tooltip": true },
"encoding": { "color": { "field": "measure_label", "legend": null } },
"transform": [{ "filter": { "field": "measure_label", "oneOf": ["Pace"] } }]
},
{
"mark": { "type": "bar", "height": 7, "tooltip": true },
"encoding": { "color": { "field": "measure_label", "legend": null } },
"transform": [{ "filter": { "field": "measure_label", "oneOf": ["Current"] } }]
},
{
"mark": { "type": "tick", "tooltip": true, "thickness": 3 },
"encoding": { "color": { "field": "measure_label", "legend": null } },
"transform": [{ "filter": { "field": "measure_label", "oneOf": ["Target"] } }]
}
],
"encoding": {
"x": {
"field": "measure_value",
"type": "quantitative",
"stack": null,
"title": "Value",
"axis": { "orient": "bottom" }
},
"color": {
"scale": {
"domain": ["Target", "Pace", "Current"],
"range": ["#000000", "#bcbcbc", "#A66BBF"]
}
},
"order": {
"field": "measure_order",
"type": "quantitative",
"sort": "descending"
}
}
}
],
"encoding": {
"y": {
"field": "categoryField",
"type": "ordinal",
"title": "Category",
"axis": { "labelOverlap": true }
},
"tooltip": [
{ "field": "categoryField", "type": "nominal", "title": "Category" },
{ "field": "currentField", "type": "quantitative", "title": "Current" },
{ "field": "paceField", "type": "quantitative", "title": "Pace" },
{ "field": "targetField", "type": "quantitative", "title": "Target" }
]
}
}
]
}
量規
在$valueField區段定義 $totalField 和 。
JSON 規範
{
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"width": "container",
"height": "container",
"data": { "name": "databricks_query" },
"config": {
"concat": { "spacing": 0 },
"autosize": { "type": "fit", "contains": "padding" }
},
"params": [
{ "name": "ring_max", "expr": "min(width, height) / 2 - 16" },
{ "name": "ring_width", "expr": "max(12, (min(width, height) / 2) * 0.12)" },
{ "name": "ring_gap", "expr": "max(4, (min(width, height) / 2) * 0.03)" },
{ "name": "label_color", "value": "#000000" },
{ "name": "ring_background_opacity", "value": 0.3 },
{ "name": "ring0_percent", "value": 100 },
{ "name": "ring0_outer", "expr": "ring_max + 2" },
{ "name": "ring0_inner", "expr": "ring_max + 1" },
{ "name": "ring1_outer", "expr": "ring0_inner - ring_gap" },
{ "name": "ring1_inner", "expr": "ring1_outer - ring_width" },
{ "name": "ring1_middle", "expr": "(ring1_outer + ring1_inner) / 2" },
{ "name": "arc_size", "expr": "220" }
],
"transform": [
{ "as": "ratio", "calculate": "datum['$valueField'] / datum['$totalField']" },
{ "as": "_arc_start_degrees", "calculate": "360 - ( arc_size / 2 )" },
{ "as": "_arc_end_degrees", "calculate": "0 + ( arc_size / 2 )" },
{ "as": "_arc_start_radians", "calculate": "2 * 3.14 * ( datum['_arc_start_degrees'] - 360 ) / 360" },
{ "as": "_arc_end_radians", "calculate": "2 * 3.14 * datum['_arc_end_degrees'] / 360" },
{ "as": "_arc_total_radians", "calculate": "datum['_arc_end_radians'] - datum['_arc_start_radians']" },
{ "as": "_ring_start_radians", "calculate": "datum['_arc_start_radians']" },
{
"as": "_ring_end_radians",
"calculate": "datum['_arc_start_radians'] + ( datum['_arc_total_radians'] * datum['ratio'] )"
}
],
"layer": [
{
"mark": {
"type": "arc",
"color": "lightgrey",
"theta": { "expr": "datum['_arc_start_radians']" },
"radius": { "expr": "ring1_outer" },
"theta2": { "expr": "datum['_arc_end_radians']" },
"radius2": { "expr": "ring1_inner" },
"cornerRadius": 10
}
},
{
"name": "RING",
"mark": {
"type": "arc",
"theta": { "expr": "datum['_ring_start_radians']" },
"radius": { "expr": "ring1_outer" },
"theta2": { "expr": "datum['_ring_end_radians']" },
"radius2": { "expr": "ring1_inner" },
"cornerRadius": 10
},
"encoding": {
"color": {
"value": "#307E31",
"condition": [
{ "test": "datum['ratio'] < 0.33", "value": "#880808" },
{ "test": "datum['ratio'] < 0.66", "value": "#E49B0F" }
]
}
}
},
{
"mark": { "type": "text", "fontSize": 40 },
"encoding": {
"text": { "field": "$valueField" },
"color": {
"value": "#307E31",
"condition": [
{ "test": "datum['ratio'] < 0.33", "value": "#880808" },
{ "test": "datum['ratio'] < 0.66", "value": "#E49B0F" }
]
}
}
}
]
}
雷達圖
在$key區段定義 $value 和 。
JSON 規範
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": "container",
"height": "container",
"config": {
"autosize": { "type": "fit", "contains": "padding" }
},
"data": { "name": "databricks_query" },
"transform": [
{ "window": [{ "op": "row_number", "as": "category" }] },
{ "calculate": "datum.category - 1", "as": "category" },
{
"joinaggregate": [
{ "op": "count", "as": "numCategories" },
{ "op": "max", "field": "$value", "as": "maxValue" }
]
},
{ "calculate": "2 * PI * datum.category / datum.numCategories", "as": "angle" },
{ "calculate": "100 * datum['$value'] / datum.maxValue", "as": "r" },
{ "calculate": "datum.r * cos(datum.angle)", "as": "x" },
{ "calculate": "datum.r * sin(datum.angle)", "as": "y" },
{ "calculate": "110 * cos(datum.angle)", "as": "label_x" },
{ "calculate": "110 * sin(datum.angle)", "as": "label_y" }
],
"layer": [
{
"transform": [
{ "joinaggregate": [{ "op": "count", "as": "numCategories" }] },
{ "aggregate": [{ "op": "max", "field": "numCategories", "as": "numCategories" }] },
{ "calculate": "[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]", "as": "cats" },
{ "flatten": ["cats"], "as": ["cat"] },
{ "filter": "datum.cat <= datum.numCategories" },
{ "calculate": "2 * PI * datum.cat / datum.numCategories", "as": "angle" },
{ "calculate": "100 * cos(datum.angle)", "as": "x" },
{ "calculate": "100 * sin(datum.angle)", "as": "y" }
],
"mark": { "type": "line", "color": "#ddd", "strokeWidth": 1 },
"encoding": {
"x": { "field": "x", "type": "quantitative", "scale": { "domain": [-120, 120] }, "axis": null },
"y": { "field": "y", "type": "quantitative", "scale": { "domain": [-120, 120] }, "axis": null },
"order": { "field": "cat" }
}
},
{
"transform": [
{ "joinaggregate": [{ "op": "count", "as": "numCategories" }] },
{ "aggregate": [{ "op": "max", "field": "numCategories", "as": "numCategories" }] },
{ "calculate": "[20,40,60,80,100]", "as": "levels" },
{ "flatten": ["levels"], "as": ["level"] },
{ "calculate": "[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]", "as": "cats" },
{ "flatten": ["cats"], "as": ["cat"] },
{ "filter": "datum.cat <= datum.numCategories" },
{ "calculate": "2 * PI * datum.cat / datum.numCategories", "as": "angle" },
{ "calculate": "datum.level", "as": "r" },
{ "calculate": "datum.r * cos(datum.angle)", "as": "x" },
{ "calculate": "datum.r * sin(datum.angle)", "as": "y" }
],
"mark": { "type": "line", "color": "#ddd", "strokeWidth": 1 },
"encoding": {
"x": { "field": "x", "type": "quantitative" },
"y": { "field": "y", "type": "quantitative" },
"detail": { "field": "level" },
"order": { "field": "cat" }
}
},
{
"mark": { "type": "line", "color": "#9467bd", "strokeWidth": 2, "interpolate": "linear-closed" },
"encoding": {
"x": { "field": "x", "type": "quantitative" },
"y": { "field": "y", "type": "quantitative" },
"order": { "field": "category" }
}
},
{
"mark": { "type": "point", "filled": true, "size": 50, "color": "#9467bd" },
"encoding": {
"x": { "field": "x", "type": "quantitative" },
"y": { "field": "y", "type": "quantitative" }
}
},
{
"mark": { "type": "text", "fontSize": 14, "fontWeight": "bold" },
"encoding": {
"x": { "field": "label_x", "type": "quantitative" },
"y": { "field": "label_y", "type": "quantitative" },
"text": { "field": "$key", "type": "nominal" }
}
}
],
"view": { "stroke": null }
}
徑向圖表
在$valueField區段定義 $colorField 和 。
JSON 規範
{
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"width": "container",
"height": "container",
"config": {
"autosize": { "type": "fit", "contains": "padding" }
},
"data": { "name": "databricks_query" },
"transform": [
{
"aggregate": [{ "op": "sum", "field": "$valueField", "as": "total" }],
"groupby": ["$colorField"]
},
{
"window": [{ "op": "rank", "as": "rank" }],
"sort": [{ "field": "total", "order": "descending" }]
}
],
"layer": [
{
"mark": { "type": "arc", "innerRadius": 20, "stroke": "#fff" }
}
],
"encoding": {
"theta": {
"field": "total",
"type": "quantitative",
"scale": { "type": "sqrt" },
"stack": true,
"sort": "descending"
},
"radius": { "field": "total", "scale": { "type": "sqrt", "zero": true } },
"color": {
"field": "$colorField",
"type": "nominal",
"title": "Sub-Category",
"sort": { "field": "total", "order": "descending" },
"legend": { "orient": "right" }
},
"tooltip": [
{ "field": "$colorField", "type": "nominal", "title": "Sub-Category" },
{ "field": "total", "type": "quantitative", "title": "Sales" }
]
},
"view": { "stroke": null }
}
放射圖
在outerGroupField區段中定義 innerGroupField、sizeField 和 。
JSON 規範
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": "container",
"height": "container",
"data": { "name": "databricks_query" },
"config": {
"autosize": { "type": "fit", "contains": "padding" }
},
"transform": [
{ "calculate": "datum['outerGroupField']", "as": "OUTSIDE" },
{ "calculate": "datum['innerGroupField']", "as": "INSIDE" },
{ "calculate": "datum.OUTSIDE + '-' + datum.INSIDE", "as": "OUT_IN" },
{ "calculate": "toNumber(datum['sizeField'])", "as": "SIZE" }
],
"resolve": {
"scale": { "color": "independent" },
"legend": { "color": "independent" }
},
"layer": [
{
"mark": {
"type": "arc",
"tooltip": true,
"innerRadius": { "expr": "min(width, height)/9" },
"outerRadius": { "expr": "min(width, height)/3" }
},
"encoding": {
"theta": { "field": "SIZE", "type": "quantitative", "stack": true },
"color": {
"field": "OUT_IN",
"type": "ordinal",
"sort": "ascending",
"title": "Inner Grouping",
"scale": {
"range": [
"#1DF9B9",
"#1DE5B9",
"#1DD1B9",
"#1DBDB9",
"#1DA9B9",
"#3DF23B",
"#3DDA3B",
"#3DC23B",
"#3DAA3B",
"#3D923B"
]
}
},
"order": { "field": "OUT_IN", "sort": "ascending" },
"tooltip": [
{ "field": "OUTSIDE", "type": "nominal", "title": "Outer Grouping" },
{ "field": "INSIDE", "type": "nominal", "title": "Inner Grouping" },
{ "field": "SIZE", "type": "quantitative", "title": "Count" }
]
}
},
{
"transform": [
{
"aggregate": [{ "op": "sum", "field": "SIZE", "as": "total_users" }],
"groupby": ["OUTSIDE"]
}
],
"mark": {
"type": "arc",
"tooltip": true,
"innerRadius": { "expr": "min(width, height)/3" }
},
"encoding": {
"theta": {
"field": "total_users",
"type": "quantitative",
"stack": true,
"sort": "ascending",
"title": "Users Count"
},
"color": {
"field": "OUTSIDE",
"type": "ordinal",
"sort": "ascending",
"title": "Outer Grouping",
"scale": { "range": ["#1DD1B9", "#3DC23B"] }
},
"order": { "field": "OUTSIDE", "sort": "ascending" },
"tooltip": [
{ "field": "OUTSIDE", "type": "nominal", "title": "Outer Grouping" },
{ "field": "total_users", "type": "quantitative", "title": "Count" }
]
}
}
]
}
Limitations
- 不支援樹狀圖圖。 Vega-Lite 不支援樹狀圖。
- 交叉篩選器僅支援資料點選取。 間隔(筆刷)選擇不會交叉篩選。
- 預設交叉篩選器僅適用於單一視圖規格。 分層與多視圖規範必須宣告
databricks_mark_selection參數,才能進行交叉濾波。 - 自訂視覺化支援一個交叉濾波器選擇參數。 若一個規範宣告
databricks_mark_selection多次,則只有第一個會交叉篩選。 - 圖片標記僅支援 37 KB 或以下的 base64
data:內嵌圖片網址(例如data:image/png;base64,...),格式為 PNG、JPEG 或 WebP。 遠端影像 URL(https:或http:)、相對 URL、SVG 影像,以及由欄位或運算式驅動的影像 URL,皆不支援。