BarChart
Chart.jsの高度にカスタマイズ可能な棒グラフ・ラッパー。
基本的な使い方
デフォルトでは、`BarChart` コンポーネントは、横方向のプレースホルダーである月間収益データセットをレンダリングします。
Monthly Revenue
astro
---
import BarChart from '@/components/data-display/BarChart.astro';
---
<BarChart title="Monthly Revenue" class="w-full h-64" /> カスタムデータ
カスタムの `data` オブジェクトと `options` オブジェクトを渡すことで、複数のデータセットの棒グラフを含む、Chart.js の設定を完全に制御できます。
Quarterly Sales by Category
astro
---
import type { ChartData, ChartOptions } from 'chart.js';
const salesData: ChartData<'bar'> = {
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
datasets: [
{
label: 'Hardware',
data: [45000, 52000, 48000, 61000],
backgroundColor: '#0ea5e9',
borderRadius: 4,
barPercentage: 0.6,
},
{
label: 'Software',
data: [32000, 39000, 41000, 55000],
backgroundColor: '#6366f1',
borderRadius: 4,
barPercentage: 0.6,
}
]
};
const salesOptions: ChartOptions<'bar'> = {
indexAxis: 'y',
plugins: {
legend: {
display: true,
position: 'top',
labels: { usePointStyle: true, padding: 20 }
}
},
scales: {
x: { grid: { color: '#e4e4e7', tickBorderDash: [5, 5] }, border: { display: false } },
y: { grid: { display: false }, border: { display: false } }
}
};
---
<BarChart title="Quarterly Sales by Category" data={salesData} options={salesOptions} class="w-full h-72" /> APIリファレンス
| プロパティ | 説明 | タイプ | デフォルト |
|---|---|---|---|
| class | グラフコンテナに適用する追加のCSSクラス。 | string | - |
| title | 上部に表示されるグラフのタイトル。 | string | - |
| borderRadius | 棒の角丸半径。 | number | 4 |
| data | 有効な Chart.js データオブジェクト。 | ChartData<'bar'> | - |
| options | 有効な Chart.js オプションオブジェクト。 | ChartOptions<'bar'> | - |