Blog Chart -- 內建 SVG 資料視覺化
為部落格文章產生相容於深色模式的內嵌 SVG 圖表。當 blog-write 和 blog-rewrite 識別出適合繪製圖表的資料時,會由內部呼叫。這不是一個獨立的、供使用者直接使用的命令。
樣式來源依據: references/visual-media.md
輸入格式
文章撰寫者或研究員傳入一個圖表請求:
Chart Request:
- Type: horizontal bar
- Title: "AI Citation Sources by Platform"
- Data: ChatGPT 43.8%, Perplexity 6.6%, Google AI Overviews 2.2%, Reddit 7.15%
- Source: Ahrefs, December 2025
- Platform: mdx (or html)
圖表類型選擇
根據資料模式選擇。務必多樣化——同一篇文章中不得重複使用同一種圖表類型。
| 資料模式 | 最佳圖表類型 |
|---|---|
| 前/後比較 | 分組長條圖 (Grouped bar chart) |
| 排名因素 / 相關性 | 棒棒糖圖 (Lollipop chart) |
| 整體部分 / 市佔率 | 圓環圖 (Donut chart) |
| 隨時間趨勢 | 折線圖 (Line chart) |
| 百分比改善 | 水平長條圖 (Horizontal bar chart) |
| 分佈 / 範圍 | 面積圖 (Area chart) |
| 多維度評分 | 雷達圖 (Radar chart) |
樣式規則(不可協商)
所有圖表必須在深色和淺色背景上都能正常顯示:
Text elements: fill="currentColor"
Grid lines: stroke="currentColor" opacity="0.08"
Axis lines: stroke="currentColor" opacity="0.3"
Background: transparent (no fill on root SVG)
Subtitle text: fill="currentColor" opacity="0.45"
Source text: fill="currentColor" opacity="0.35"
Label text: fill="currentColor" opacity="0.8"
色板
| 顏色 | Hex | 用途 |
|---|---|---|
| 橙色 | #f97316 | 主要 / 最高值 |
| 天藍色 | #38bdf8 | 次要 / 比較用 |
| 紫色 | #a78bfa | 第三層級 / 特殊類別 |
| 綠色 | #22c55e | 第四層級 / 正向指標 |
對於彩色元素內部的文字:fill="white" 搭配 fontWeight="800"。
標準 SVG 外殼 (HTML)
<svg
viewBox="0 0 560 380"
style="max-width: 100%; height: auto; font-family: 'Inter', system-ui, sans-serif"
role="img"
aria-label="Chart description with key data point"
>
<title>Chart Title</title>
<desc>Description for screen readers with all key data points and source</desc>
<!-- Chart content -->
<text x="280" y="372" text-anchor="middle" font-size="10" fill="currentColor" opacity="0.35">
Source: Source Name (Year)
</text>
</svg>
JSX/MDX 外殼 (camelCase 屬性)
<svg
viewBox="0 0 560 380"
style={{maxWidth: '100%', height: 'auto', fontFamily: "'Inter', system-ui, sans-serif"}}
role="img"
aria-label="Chart description"
>
<title>Chart Title</title>
<desc>Description for screen readers</desc>
{/* Chart content */}
<text x="280" y="372" textAnchor="middle" fontSize="10" fill="currentColor" opacity="0.35">
Source: Source Name (Year)
</text>
</svg>
JSX 屬性轉換 (MDX 必須)
| HTML | JSX |
|---|---|
stroke-width | strokeWidth |
stroke-dasharray | strokeDasharray |
stroke-linecap | strokeLinecap |
text-anchor | textAnchor |
font-size | fontSize |
font-weight | fontWeight |
font-family | fontFamily |
class | className |
style="..." | style={{...}} |
圖表類型建構
水平長條圖
最適合:百分比改善、單一指標比較。
- 定義圖表區域:x=80, y=40, width=440, height=280
- 計算長條高度:
chartHeight / dataCount - gap(gap=8) - 計算長條寬度:
(value / maxValue) * chartWidth - 定位長條:
y = chartY + index * (barHeight + gap) - 左側標籤(右對齊,x=75):類別名稱
- 長條末端數值標籤:百分比或數字
- 底部中央來源文字
分組長條圖
最適合:前/後比較、A vs B 比較。
- 沿 Y 軸定義組別,每組內有數個長條
- 使用 2 種顏色(主要 + 次要)代表兩個系列
- 頂部加入圖例:顏色方塊 + 系列標籤
- 組間間距 > 組內間距
圓環圖
最適合:整體部分、市佔率。
- 中心點:cx=280, cy=180, 外半徑=140, 內半徑=80
- 使用累計角度計算弧形區段
- 每個區段:
<path d="M... A... L... A... Z" fill="color" /> - 中心文字:總計或關鍵標籤
- 圖表下方圖例:顏色方塊 + 標籤 + 數值
折線圖
最適合:隨時間趨勢。
- X 軸:時間區段,等距分佈
- Y 軸:數值範圍,包含 4-5 條格線
- 繪製格線:
stroke="currentColor" opacity="0.08" - 標示資料點:
<circle cx=... cy=... r="4" fill="color" /> - 連接點:
<polyline points="..." fill="none" stroke="color" strokeWidth="2" /> - 可選:線下方區域填色,
opacity="0.1"
棒棒糖圖
最適合:排名因素、相關性。
- 水平方向(類似長條圖但使用圓形)
- 從軸線到資料點的細線:
stroke="currentColor" opacity="0.15" strokeWidth="1" - 資料點的圓形:
r="6"搭配填滿顏色 - 圓形旁的數值標籤
- Y 軸上的類別(左對齊)
面積圖
最適合:分佈、累積資料。
- 與折線圖相同,但線下方區域有填色
- 區域填色:
<path d="M... L... L... Z" fill="color" opacity="0.15" /> - 上方線條:
stroke="color" strokeWidth="2" fill="none" - 格線位於區域後方
雷達圖
最適合:多維度評分(5-7 個軸線)。
- 中心點:cx=280, cy=190
- 繪製同心多邊形作為網格(3-4 層級)
- 計算等角度間隔的軸線端點
- 在每個軸線上,根據數值比例繪製資料點
- 連接資料點形成填色多邊形:
fill="color" opacity="0.2" stroke="color" - 在每個軸線外緣標示標籤
輸出格式
將每個圖表包裹在 <figure> 元素中:
HTML:
<figure>
<svg viewBox="0 0 560 380" style="max-width: 100%; height: auto; font-family: 'Inter', system-ui, sans-serif" role="img" aria-label="[description]">
<title>[Chart Title]</title>
<desc>[Full description with data points for screen readers]</desc>
<!-- chart content -->
<text x="280" y="372" text-anchor="middle" font-size="10" fill="currentColor" opacity="0.35">
Source: [Source Name] ([Year])
</text>
</svg>
</figure>
MDX:
<figure className="chart-container" style={{margin: '2.5rem 0', textAlign: 'center', padding: '1.5rem', borderRadius: '12px'}}>
<svg viewBox="0 0 560 380" style={{maxWidth: '100%', height: 'auto', fontFamily: "'Inter', system-ui, sans-serif"}} role="img" aria-label="[description]">
<title>[Chart Title]</title>
<desc>[Full description]</desc>
{/* chart content with camelCase attributes */}
<text x="280" y="372" textAnchor="middle" fontSize="10" fill="currentColor" opacity="0.35">
Source: [Source Name] ([Year])
</text>
</svg>
</figure>
品質檢查清單(回傳前請確認)
- 無硬編碼的文字顏色(全部使用
currentColor) - 無白色/淺色背景(透明或無)
- 底部有來源歸屬文字
-
<svg>上包含role="img"和aria-label -
<svg>內部包含<title>和<desc> - 此篇文章中尚未使用該圖表類型
- 若為 MDX:所有屬性皆為 camelCase(屬性名稱中無連字號)
- 資料值與來源資料完全一致
- 色板僅使用核可的顏色
- ViewBox 為
0 0 560 380(標準)或經合理調整的替代值