パラレル座標ライブラリ比較
3つのアプローチで多変量データのパターンや相関を可視化し、実装方法とスタイルを比較
このページでは、Nivo・D3.js・React + SVGを使った平行座標プロット(Parallel Coordinates)の実装パターンをインタラクティブなデモとコードで確認できます。
1. Nivo(@nivo/parallel-coordinates)
@nivo/parallel-coordinatesパッケージでパラレル座標をネイティブサポート。variablesに軸の定義を渡すだけで各軸のスケールを自動正規化して描画。ラインの色・透明度・曲線の種類(linear・monotoneX)などのカスタマイズが豊富。ホバー時のハイライトも標準対応。
'use client';
import { ResponsiveParallelCoordinates } from '@nivo/parallel-coordinates';
const data = [
{ product: 'A', sales: 85, cost: 42, profit: 43, satisfaction: 4.2 },
{ product: 'B', sales: 62, cost: 38, profit: 24, satisfaction: 3.8 },
{ product: 'C', sales: 91, cost: 55, profit: 36, satisfaction: 4.5 },
{ product: 'D', sales: 48, cost: 25, profit: 23, satisfaction: 3.5 },
{ product: 'E', sales: 75, cost: 48, profit: 27, satisfaction: 4.0 },
];
const variables = [
{ key: 'sales', type: 'linear' as const, min: 0, max: 100, ticksPosition: 'before' },
{ key: 'cost', type: 'linear' as const, min: 0, max: 70, ticksPosition: 'before' },
{ key: 'profit', type: 'linear' as const, min: 0, max: 60, ticksPosition: 'before' },
{ key: 'satisfaction', type: 'linear' as const, min: 1, max: 5, ticksPosition: 'before' },
];
export function NivoParallelCoordinates() {
return (
<div style={{ height: 300 }}>
<ResponsiveParallelCoordinates
data={data}
variables={variables}
margin={{ top: 50, right: 60, bottom: 50, left: 60 }}
colors={{ scheme: 'category10' }}
strokeWidth={2}
lineOpacity={0.5}
curve="monotoneX"
/>
</div>
);
}AIプロンプトテンプレート
React + Tailwind CSSで、Nivo(@nivo/parallel-coordinates)を使ったパラレル座標チャートを実装してください。 - 使用ライブラリ: @nivo/parallel-coordinates の ResponsiveParallelCoordinates コンポーネント - サンプルデータ: 自動車スペック10車種(馬力・燃費・重量・加速性能・価格)を用意すること - インタラクティブ: ホバー時に対象ラインをハイライト表示すること(標準搭載) - 軸: variables プロパティに id・min・max・label を定義して軸ごとに独立したスケールを自動設定すること - レスポンシブ: ResponsiveParallelCoordinates を使って親要素の幅に応じてサイズが変わるよう対応すること - スタイリング: colors・curve・lineOpacity・lineWidth を設定し、curve="monotoneX" で滑らかなラインにすること
このプロンプトはあくまでたたき台です。AIの回答はモデルやバージョン、会話の文脈によって毎回異なります。意図通りに動かない場合は、条件を追記・修正してお使いください。
2. D3.js(カスタム実装)
d3-scaleのscaleLinearで各軸を独立して正規化し、d3-shapeのline()でラインのパスを作って、SVG描画をReactで制御するカスタム実装。d3-brushを組み合わせれば、各軸にブラシ操作(範囲選択によるフィルタリング)も追加できる。実装コストは最も高いが、インタラクティブなデータ探索ツールとして最大の表現力を発揮。
scalePoint、値の縦位置は軸ごとのscaleLinearで求め、目盛りもticks()で切りのいい値になる。 d3-brushと組み合わせれば軸ごとの範囲フィルタリングも追加できる(このデモでは未実装)。'use client';
import { useState } from 'react';
import { scaleLinear, scalePoint } from 'd3-scale';
import { line } from 'd3-shape';
const data = [
{ name: '車種A', horsepower: 130, fuelEfficiency: 18, weight: 1800, acceleration: 12, price: 280 },
{ name: '車種B', horsepower: 165, fuelEfficiency: 14, weight: 2100, acceleration: 10, price: 420 },
{ name: '車種C', horsepower: 95, fuelEfficiency: 24, weight: 1400, acceleration: 16, price: 180 },
{ name: '車種D', horsepower: 210, fuelEfficiency: 10, weight: 2400, acceleration: 8, price: 680 },
{ name: '車種E', horsepower: 110, fuelEfficiency: 21, weight: 1600, acceleration: 14, price: 220 },
{ name: '車種F', horsepower: 185, fuelEfficiency: 12, weight: 2200, acceleration: 9, price: 520 },
{ name: '車種G', horsepower: 75, fuelEfficiency: 28, weight: 1200, acceleration: 18, price: 150 },
{ name: '車種H', horsepower: 145, fuelEfficiency: 16, weight: 1950, acceleration: 11, price: 350 },
{ name: '車種I', horsepower: 230, fuelEfficiency: 9, weight: 2600, acceleration: 7, price: 780 },
{ name: '車種J', horsepower: 88, fuelEfficiency: 26, weight: 1350, acceleration: 17, price: 165 },
];
type AxisKey = Exclude<keyof (typeof data)[number], 'name'>;
const axes: { key: AxisKey; label: string; min: number; max: number }[] = [
{ key: 'horsepower', label: '馬力', min: 60, max: 250 },
{ key: 'fuelEfficiency', label: '燃費', min: 5, max: 32 },
{ key: 'weight', label: '重量', min: 1000, max: 2800 },
{ key: 'acceleration', label: '加速', min: 5, max: 22 },
{ key: 'price', label: '価格', min: 100, max: 850 },
];
const COLORS = [
'#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6',
'#06b6d4', '#f97316', '#84cc16', '#ec4899', '#6366f1',
];
const WIDTH = 680;
const HEIGHT = 320;
const margin = { top: 40, right: 30, bottom: 30, left: 30 };
// 軸の横位置は scalePoint で等間隔に並べる
const x = scalePoint<AxisKey>()
.domain(axes.map((ax) => ax.key))
.range([margin.left, WIDTH - margin.right]);
// 値 → 縦位置は、軸ごとに範囲が違うので軸ごとの scaleLinear で求める
const yScales = axes.map((ax) =>
scaleLinear().domain([ax.min, ax.max]).range([HEIGHT - margin.bottom, margin.top]),
);
// [x, y] の配列を SVG のパスにする(.curve() を足せば曲線にもできる)
const toPath = line();
// d3 は計算(軸の位置・スケール・パス)だけを担当し、SVG は React の JSX で描く
export function D3ParallelCoordinates() {
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
return (
<svg viewBox="0 0 680 320" className="w-full" style={{ fontFamily: 'sans-serif' }}>
{data.map((row, i) => {
const points = axes.map((ax, j): [number, number] => [x(ax.key) ?? 0, yScales[j](row[ax.key])]);
const isHovered = hoveredIndex === i;
const isOther = hoveredIndex !== null && !isHovered;
return (
<path
key={row.name}
d={toPath(points) ?? ''}
fill="none"
stroke={COLORS[i]}
strokeWidth={isHovered ? 3 : 1.5}
strokeOpacity={isOther ? 0.1 : isHovered ? 1 : 0.6}
style={{ cursor: 'pointer' }}
onMouseEnter={() => setHoveredIndex(i)}
onMouseLeave={() => setHoveredIndex(null)}
/>
);
})}
{axes.map((ax, j) => {
const axisX = x(ax.key) ?? 0;
const y = yScales[j];
return (
<g key={ax.key}>
<line x1={axisX} x2={axisX} y1={margin.top} y2={HEIGHT - margin.bottom} stroke="#6b7280" strokeWidth={1.5} />
{/* 目盛りは scale.ticks() で切りのいい値にする */}
{y.ticks(5).map((tick) => (
<g key={tick}>
<line x1={axisX - 4} x2={axisX + 4} y1={y(tick)} y2={y(tick)} stroke="#6b7280" />
<text x={axisX + 8} y={y(tick) + 4} fontSize={9} fill="#6b7280">
{tick}
</text>
</g>
))}
<text x={axisX} y={margin.top - 10} fontSize={11} fontWeight={600} fill="#374151" textAnchor="middle">
{ax.label}
</text>
</g>
);
})}
{hoveredIndex !== null && (
<text x={WIDTH / 2} y={HEIGHT - 4} fontSize={11} fontWeight={600} fill={COLORS[hoveredIndex]} textAnchor="middle">
{data[hoveredIndex].name}
</text>
)}
</svg>
);
}AIプロンプトテンプレート
React + Tailwind CSSで、D3.js(カスタム実装)を使ったパラレル座標チャートを実装してください。 - 使用ライブラリ: d3-scale(scalePoint で軸の横位置、軸ごとの scaleLinear で値を縦位置に変換)と d3-shape(line で各データのパスを生成)。SVG は React の JSX で描くこと - サンプルデータ: 自動車スペック10車種(馬力・燃費・重量・加速性能・価格)を用意すること - インタラクティブ: ホバー時に対象ラインを強調表示し他ラインを薄くするハイライト処理をuseStateで実装すること - 軸: 各軸に縦線・目盛り(scale.ticks() の値)・ラベルを表示すること。あとから d3-brush で軸ごとの範囲選択を足せるよう、軸ごとのスケールを配列で持っておくこと - レスポンシブ: viewBox を使って親要素の幅に応じてサイズが変わるよう対応すること - スタイリング: 各データ系列を識別しやすい10色のカラーパレットで色分けすること
このプロンプトはあくまでたたき台です。AIの回答はモデルやバージョン、会話の文脈によって毎回異なります。意図通りに動かない場合は、条件を追記・修正してお使いください。
3. カスタムReact実装(SVG)
外部ライブラリに依存せずReact + SVGでカスタム実装。各軸のmin・maxから値を0〜1に正規化し、SVGのpolylineで各データポイントを結ぶシンプルな実装。バンドルサイズへの影響がなく、基本的なパラレル座標を最小限のコードで実現できる。
(value - min) / (max - min) で 各軸を0〜1に正規化し、polyline で描画するだけの最小実装。'use client';
// 外部依存ゼロのReact + SVGカスタム実装
const data = [
{ sales: 85, cost: 42, profit: 43, satisfaction: 4.2 },
{ sales: 62, cost: 38, profit: 24, satisfaction: 3.8 },
{ sales: 91, cost: 55, profit: 36, satisfaction: 4.5 },
{ sales: 48, cost: 25, profit: 23, satisfaction: 3.5 },
];
const axes = [
{ key: 'sales', label: '売上', min: 0, max: 100 },
{ key: 'cost', label: 'コスト', min: 0, max: 70 },
{ key: 'profit', label: '利益', min: 0, max: 60 },
{ key: 'satisfaction', label: '満足度', min: 1, max: 5 },
];
const colors = ['#3b82f6', '#10b981', '#8b5cf6', '#f97316'];
const normalize = (v: number, min: number, max: number, h: number) =>
h - ((v - min) / (max - min)) * h;
export function CustomParallelCoordinates() {
const w = 400, h = 200, mx = 40, my = 30;
const chartW = w - mx * 2;
const chartH = h - my * 2;
const step = chartW / (axes.length - 1);
return (
<svg viewBox={`0 0 ${w} ${h + my}`} className="w-full">
<g transform={`translate(${mx}, ${my})`}>
{data.map((d, i) => (
<polyline key={i}
points={axes.map((ax, j) =>
`${j * step},${normalize((d as any)[ax.key], ax.min, ax.max, chartH)}`
).join(' ')}
fill="none" stroke={colors[i]} strokeWidth={2} opacity={0.7} />
))}
{axes.map((ax, j) => (
<g key={ax.key} transform={`translate(${j * step}, 0)`}>
<line y1={0} y2={chartH} stroke="#9ca3af" />
<text y={-6} textAnchor="middle" fontSize={10} fill="#374151">{ax.label}</text>
</g>
))}
</g>
</svg>
);
}AIプロンプトテンプレート
React + Tailwind CSSで、カスタムReact実装(SVG)を使ったパラレル座標チャートを実装してください。 - 使用ライブラリ: 外部ライブラリなし。React + SVGのみで軸の正規化・グリッド配置・ライン描画をすべて自前実装すること - サンプルデータ: 自動車スペック10車種(馬力・燃費・重量・加速性能・価格)を用意すること - インタラクティブ: なし(表示専用の最小実装) - 軸: (value - min) / (max - min) で各軸を0〜1に正規化し、SVGのpolylineで各データポイントを結ぶこと - レスポンシブ: viewBox を使って親要素の幅に応じてサイズが変わるよう対応すること - スタイリング: 各データ系列を識別しやすい色で strokeOpacity を設定してラインを描画すること
このプロンプトはあくまでたたき台です。AIの回答はモデルやバージョン、会話の文脈によって毎回異なります。意図通りに動かない場合は、条件を追記・修正してお使いください。
ライブラリ比較
| アプローチ | レンダリング | 対応状況 | ブラシ操作 | おすすめ用途 |
|---|---|---|---|---|
| Nivo | SVG | ◎ 専用パッケージ | △ | デザイン重視のダッシュボード |
| D3.js カスタム | SVG | ◎ 完全カスタム | ◎ | インタラクティブな分析ツール |
| カスタムReact実装 | SVG | ◯ 基本機能 | ✕ | 軽量・シンプルな表示用途 |
選択のポイント
- •Nivo: @nivo/parallel-coordinatesが最もバランスが良く、導入コストを抑えつつ高品質な表示を実現。閲覧メインのダッシュボードに最適。
- •D3.js: ブラシ操作によるインタラクティブなフィルタリングが必要な分析ツールに選択。実装コストは最も高いが表現力も最大。
- •カスタムReact実装: 外部依存を排除したい場合やシンプルな表示のみ必要な場合に。最小限のコードで基本的なパラレル座標を実現できる。
典型的な使用例
- 🚗製品スペック比較: 複数の製品を多軸のスペックで同時に比較・パターン発見
- 👥顧客セグメント分析: 年齢・購買頻度・単価・LTVなど複数指標でセグメントの特徴を把握
- 🔬多変量データ探索: 機械学習の特徴量エンジニアリングでの変数間の相関パターン確認
- 📊パフォーマンス評価: 複数のKPIを持つチーム・個人の総合評価を視覚的に比較
⚠️ 使用時の注意点
- •軸ごとにスケールが異なるため、必ず正規化(0〜1)して描画する
- •軸の順序によってパターンの見え方が大きく変わるため、相関が強い軸を隣接させると読みやすい
- •データ件数が多すぎるとラインが重なって見づらくなるため、透明度を下げるか代表的なデータに絞る
- •ラインの色でカテゴリーを区別する場合、色覚多様性に配慮したカラーパレットを選択する
平行座標プロット(Parallel Coordinates)は、複数の変数を並列に並べた縦軸上に各データを折れ線で結んで多次元データを可視化するグラフ。
機械学習のパラメータ比較・製品スペックの多変量分析・クラスタリング結果の可視化など、多次元データのパターン探索に使われる。
- 基本型(ブラシ選択なし)
- ブラシ選択型(範囲フィルタ)
- カテゴリ色分け型
- 軸並べ替え型
- インタラクティブ型
📊 使用しているサンプルデータ(クリックで表示)
自動車の5つのスペック指標(馬力・燃費・重量・加速・価格)を持つ10車種のデータを使用しています。各指標は異なるスケールを持ちます。
関連する逆引きリファレンス
- → 色・スタイルのカスタマイズ(borderColor・fillOpacity など)
- → ツールチップのカスタマイズ(カスタムHTML・フォーマッタ)
- → 軸(Axis)の設定(ラベルフォーマット・対数スケール・複数軸)
- → レスポンシブ対応(コンテナ追従・アスペクト比・リサイズ)
- → アニメーション・トランジション(初期描画・データ更新・イージング)
- → 凡例(Legend)の配置・スタイル(位置・アイコン・系列トグル)
- → データラベルの表示(値・位置・フォーマッタ)
- → クリックイベント・インタラクション(クリック・ホバー・ブラシ選択)
- → グリッド線のカスタマイズ(表示制御・線スタイル・リファレンスライン)
同じカテゴリの他のページ
エリアチャート(Area Chart)の実装比較
Recharts・Nivo・Chart.js・ApexChartsによるエリアチャート実装比較。fillOpacity・グラデーション・tooltip・レスポンシブ対応などのデモ・コード付き。
ストリームグラフ(Streamgraph)の実装比較
ApexCharts・Mantine Charts・Nivo・D3.jsのストリームグラフ実装を比較。type="stream"・stackOffsetWiggle・inside-outと積み上げ面の切り替えのデモ・コード付き。
ワッフルチャート(Waffle Chart)の実装比較
Mantine Charts・Nivo・自前のSVGのワッフルチャート実装を比較。WaffleChart・@nivo/waffle・cellComponentの人型アイコン・「100人中◯人」と内訳の切り替えのデモ・コード付き。