Chart.js 混合グラフ

Chart.js は、2つ以上の異なるグラフタイプを組み合わせた混合グラフを作成できます。例えば、棒グラフと折れ線グラフの組み合わせです。

混合グラフを作成する際、各データセットでグラフタイプを指定します。

混合図type属性はscatter。

棒グラフtype属性はbar、折れ線グラフtype属性はline、type はグラフタイプを表します。

const mixedChart = new Chart(ctx, {
    data: {
        datasets: [{
            type: 'bar',
            label: '柱形图数据集',
            data: [45, 49,52, 48]
        }, {
            type: 'line',
            label: '折线图数据集',
            data: [50, 40, 45, 49],
        }],
        labels: ['一月份', '二月份', '三月份', '四月份']
    },
    options: options
});
 

次に、簡単な混合グラフを作成します。

例

const ctx = document.getElementById('myChart');
const data = {
  labels: [
    '1月',
    '2月',
    '3月',
    '4月'
  ],
  datasets: [{
    type: 'bar',
    label: '棒グラフデータセット',
    data: [45, 49,52, 48],
    borderColor: 'rgb(255, 99, 132)',
    backgroundColor: 'rgba(255, 99, 132, 0.2)'
  }, {
    type: 'line',
    label: '折れ線グラフデータセット',
    data: [50, 40, 45, 49],
    fill: false,
    borderColor: 'rgb(54, 162, 235)'
  }]
};
const config = {
  type: 'scatter',
  data: data,
  options: {
    responsive: true, // グラフをレスポンシブ設定にし、画面ウィンドウの変化に応じて変化させます
    maintainAspectRatio: false,// グラフの元の比率を維持します
    scales: {
      y: {
        beginAtZero: true
      }
    }
  }
};
const myChart = new Chart(ctx, config);

試してみる »

上記の例の出力結果は次のとおりです:

その他の拡張