色・スタイルのカスタマイズ
データ可視化ライブラリの色・スタイル設定は、ブランドカラーへの統一や、値の大小をビジュアルで伝える条件付き色分けなど、グラフの「伝わりやすさ」に直結する重要な設定。
ブランドガイドラインへの色合わせ、ヒートマップ的な値連動カラー、グラデーションによる美しい仕上げ、ポジティブ/ネガティブを色で区別するなど。
主なバリエーション
- •個別要素への色指定(棒・点・スライスごとに異なる色)
- •カラーパレット/テーマの設定(系列ごとの色セット)
- •グラデーション(LinearGradient・RadialGradient)
- •透明度(fillOpacity・opacity)の調整
- •ボーダー色・太さの設定
- •条件付き色分け(値の大小・閾値による色切り替え)
ライブラリ横断比較
| 機能 | Recharts | ECharts | Plotly.js | Chart.js | Nivo | ApexCharts |
|---|---|---|---|---|---|---|
| 個別要素の色指定 | ○ Cell | ○ itemStyle | ○ marker.color[] | ○ backgroundColor[] | ○ colors fn | ○ distributed |
| カラーパレット/テーマ | △ colors prop | ○ color[]+テーマ | ○ colorway | △ 組み込み無し | ○ colorScheme | ○ テーマ設定 |
| グラデーション | ○ SVG defs | ○ type:linear | ○ colorscale | ○ canvas API | △ theme経由 | ○ fill.gradient |
| 透明度(opacity) | ○ fillOpacity | ○ opacity | ○ marker.opacity | ○ rgba指定 | ○ theme設定 | ○ fill.opacity |
| ボーダー色・太さ | ○ stroke | ○ borderColor | ○ line.color | ○ borderColor | ○ borderColor | ○ stroke |
| 条件付き色分け | ○ Cell+fn | ○ visualMap | ○ colorscale | ○ fn指定 | ○ colors fn | △ distributed |
○ = 対応 △ = 部分対応・制限あり × = 非対応
ライブラリ別コード例
各ライブラリで色・スタイルをカスタマイズする際の設定部分を抜粋しています。 動くデモは各比較ページでご確認ください。
Recharts
import { BarChart, Bar, Cell } from 'recharts';
// Cell コンポーネントで個別要素の色を指定
const colors = ['#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6'];
<BarChart data={data}>
<Bar dataKey="value" stroke="#1d4ed8" strokeWidth={1} fillOpacity={0.85}>
{data.map((_, i) => (
<Cell key={i} fill={colors[i % colors.length]} />
))}
</Bar>
</BarChart>
// SVGグラデーション + fillOpacity でフェード効果
<BarChart data={data}>
<defs>
<linearGradient id="grad" x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor="#3b82f6" stopOpacity={0.9} />
<stop offset="95%" stopColor="#3b82f6" stopOpacity={0.3} />
</linearGradient>
</defs>
<Bar dataKey="value" fill="url(#grad)" />
</BarChart>
// 条件付き色分け(値で色を切り替え)
<Bar dataKey="value">
{data.map((entry, i) => (
<Cell key={i} fill={entry.value > 100 ? '#10b981' : '#ef4444'} />
))}
</Bar>ECharts
// ECharts: カラーパレット・グラデーション・個別色指定
const option = {
// グローバルカラーパレット
color: ['#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6'],
series: [{
type: 'bar',
// 個別データへの色指定
data: [
{ value: 120, itemStyle: { color: '#ef4444' } },
{ value: 200, itemStyle: { color: '#3b82f6' } },
{ value: 150 }, // パレットから自動割り当て
],
// グラデーション(全バーに適用)
itemStyle: {
color: {
type: 'linear',
x: 0, y: 0, x2: 0, y2: 1,
colorStops: [
{ offset: 0, color: '#3b82f6' },
{ offset: 1, color: '#93c5fd' },
],
},
borderColor: '#1d4ed8',
borderWidth: 1,
opacity: 0.85,
},
}],
// visualMap で条件付き色分け(値の範囲でグラデーション)
visualMap: {
min: 0, max: 300,
inRange: { color: ['#93c5fd', '#1d4ed8'] },
show: false,
},
};Plotly.js
// Plotly.js: marker.color で色・スタイルを制御
const data = [{
type: 'bar',
x: ['1月', '2月', '3月', '4月', '5月'],
y: [100, 200, 150, 250, 180],
marker: {
// 個別色指定(配列)
color: ['#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6'],
// カラースケールでグラデーション(値に連動)
// color: [100, 200, 150, 250, 180],
// colorscale: 'Blues',
// showscale: true,
opacity: 0.85,
line: {
color: '#1d4ed8',
width: 1.5,
},
},
}];
// 条件付き色分け
const colors = data[0].y.map(v => v > 180 ? '#10b981' : '#3b82f6');Chart.js
// Chart.js: backgroundColor / borderColor で色を設定
const data = {
labels: ['1月', '2月', '3月', '4月', '5月'],
datasets: [{
label: '売上',
// 個別色指定(配列)
backgroundColor: ['#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6'],
borderColor: ['#1d4ed8', '#059669', '#d97706', '#dc2626', '#7c3aed'],
borderWidth: 1,
// 透明度は rgba で指定
// backgroundColor: 'rgba(59,130,246,0.8)',
}],
};
// グラデーションは canvas API で作成
// const createGradient = (ctx, chartArea) => {
// const gradient = ctx.createLinearGradient(0, chartArea.top, 0, chartArea.bottom);
// gradient.addColorStop(0, 'rgba(59,130,246,0.9)');
// gradient.addColorStop(1, 'rgba(59,130,246,0.1)');
// return gradient;
// };
// 条件付き色分け(関数でも指定可)
// backgroundColor: (ctx) => ctx.parsed.y > 180 ? '#10b981' : '#3b82f6',Nivo
import { ResponsiveBar } from '@nivo/bar';
<ResponsiveBar
data={data}
keys={['sales', 'target']}
// カラースキームを指定
colorScheme="tableau10"
// または個別色指定
colors={['#3b82f6', '#10b981']}
// 条件付き色分け(関数)
// colors={({ id, data }) => data[`${id}Color`]}
// ボーダー
borderColor={{ from: 'color', modifiers: [['darker', 1.4]] }}
borderWidth={1}
// テーマでグローバルなスタイル設定
theme={{
background: '#ffffff',
text: { fill: '#374151', fontSize: 12 },
grid: { line: { stroke: '#e5e7eb', strokeWidth: 1 } },
axis: {
ticks: { line: { stroke: '#d1d5db' } },
},
}}
// fillOpacity 相当は theme 内の透明度設定で対応
/>ApexCharts
// ApexCharts: colors / fill で色・スタイルを制御
const options = {
// カラーパレット
colors: ['#3b82f6', '#10b981', '#f59e0b'],
fill: {
// グラデーション
type: 'gradient',
gradient: {
shade: 'light',
type: 'vertical',
shadeIntensity: 0.5,
gradientToColors: ['#93c5fd', '#6ee7b7', '#fcd34d'],
opacityFrom: 0.9,
opacityTo: 0.4,
},
// 単色の場合は type: 'solid', opacity: 0.85
},
stroke: {
colors: ['#1d4ed8', '#059669', '#d97706'],
width: 1,
},
// 条件付き色分け(colors に関数は非対応。dataLabels や annotations で代替)
// 値によって色を変えたい場合は distributed: true + 各系列に color を設定
plotOptions: {
bar: { distributed: true },
},
};まとめ・選び方のヒント
- •SVGグラデーションで細かく制御したい → Recharts(defs タグに linearGradient を直接書ける)
- •値に連動した色分け(ヒートマップ的) → ECharts(visualMap が強力)・Plotly.js(colorscale)
- •カラースキームを手軽に切り替えたい → Nivo(colorScheme prop で d3-scale-chromatic のスキームを直接指定)・ApexCharts
- •borderColor(Nivo の ResponsiveRadar など) → 検索流入が多い設定。Nivo では borderColor prop に from:color+darker modifier が使える
- •fillOpacity(Nivo の ResponsiveRadar など) → Recharts は fillOpacity prop、Nivo は fillOpacity prop で直接指定可能