Chart.js 折れ線グラフ

折れ線グラフは、ワークシートの列または行に配置されたデータを折れ線グラフに描画できます。

折れ線グラフは、時間の経過(一般的な比率設定による)に伴って変化する連続データを表示できます。そのため、等しい時間間隔でのデータの傾向を表示するのに非常に適しています。

折れ線グラフtype属性はline、type はグラフのタイプを記述します。

const config = {
  type: 'line',
  data: data,
};

次に、簡単な折れ線グラフを作成します:

例

const ctx = document.getElementById('myChart');
const labels = ['1月', '2月', '3月','4月', '5月', '6月', '7月'];  // X軸上の対応するラベルを設定
const data = {
  labels: labels,
  datasets: [{
    label: '私の最初の折れ線グラフ',
    data: [65, 59, 80, 81, 56, 55, 40],
    fill: false,
    borderColor: 'rgb(75, 192, 192)', // 線の色を設定
    tension: 0.1
  }]
};
const config = {
  type: 'line', // グラフのタイプを設定
  data: data,
};
const myChart = new Chart(ctx, config);

試してみる »

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

次に、折れ線グラフを拡張し、オプションを追加して、次のように設定します:

例

const ctx = document.getElementById('myChart');
const labels = ['1月', '2月', '3月','4月', '5月', '6月', '7月'];  // X軸上の対応するラベルを設定
const data = {
  labels: labels,
  datasets: [{
    label: '私の最初の折れ線グラフ',
    data: [65, 59, 80, 81, 56, 55, 40],
    fill: false,
    borderColor: 'rgb(75, 192, 192)', // 線の色を設定
        backgroundColor: ['rgba(179, 0, 33, 0.5)'],// 点の塗りつぶし色を設定
        pointStyle: 'circle',     // 点のタイプを円に設定
    pointRadius: 6,    // 円の半径を設定
    pointHoverRadius: 10, // マウスを乗せた時の円の半径を設定
    tension: 0.1
  }]
};
const config = {
  type: 'line', // グラフのタイプを設定
  data: data,
  options: {
    responsive: true,  // グラフをレスポンシブに設定

    interaction: {  // 各点のインタラクションを設定
      intersect: false,
    },
    scales: {  // X軸とY軸を設定
      x: {
        display: true,
        title: {
          display: true,
          text: '日付'
        }
      },
      y: {
        display: true,
        title: {
          display: true,
          text: '得票数'
        }
      }
    }
  }
};
const myChart = new Chart(ctx, config);

試してみる »

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

垂直折れ線グラフ

垂直折れ線グラフは、水平折れ線グラフの変形です。

垂直折れ線グラフでは、オプションオブジェクトの indexAxis プロパティをyに設定する必要があり、indexAxis プロパティのデフォルト値はx。

例

const ctx = document.getElementById('myChart');
const labels = ['1月', '2月', '3月','4月', '5月', '6月', '7月'];  // X軸上の対応するラベルを設定
const data = {
  labels: labels,
  datasets: [{
    label: '私の最初の折れ線グラフ',
    data: [65, 59, 80, 81, 56, 55, 40],
    fill: false,
    borderColor: 'rgb(75, 192, 192)', // 線の色を設定
        backgroundColor: ['rgba(179, 0, 33, 0.5)'],// 点の塗りつぶし色を設定
        pointStyle: 'circle',     // 点のタイプを円に設定
    pointRadius: 6,    // 円の半径を設定
    pointHoverRadius: 10, // マウスを乗せた時の円の半径を設定
    tension: 0.1
  }]
};
const config = {
  type: 'line', // グラフのタイプを設定
  data: data,
  options: {
    indexAxis: 'y', // 垂直折れ線グラフを設定
    responsive: true,  // グラフをレスポンシブに設定

    interaction: {  // 各点のインタラクションを設定
      intersect: false,
    },
    scales: {  // X軸とY軸を設定
      x: {
                beginAtZero: true,// X軸を0から開始するように設定
        display: true,
        title: {
          display: true,
          text: '日付'
        }
      },
      y: {
        display: true,
        title: {
          display: true,
          text: '得票数'
        }
      }
    }
  }
};
const myChart = new Chart(ctx, config);

試してみる »

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

その他の拡張