天天看點

Chart.js 散點圖

散點圖是由兩組資料構成多個坐标點,考察坐标點的分布,判斷兩變量之間是否存在某種關聯或總結坐标點的分布模式。

資料結構:

data: [{
        x: 10,
        y: 20
    }, {
        x: 15,
        y: 10
    }]      

散點圖 type 屬性為 scatter。

const config = {
  type: 'scatter',
  data: data,
  options: {
    scales: {
      x: {
        type: 'linear',
        position: 'bottom'
      }
    }
  }
};
      

接下來我們建立一個簡單的散點圖:

執行個體

const ctx = document.getElementById('myChart');

const data = {

  datasets: [{

    label: '散點圖執行個體',

    data: [{

      x: -10,

      y: 0

    }, {

      x: 0,

      y: 10

    }, {

      x: 10,

      y: 5

    }, {

      x: 0.5,

      y: 5.5

    }],

    backgroundColor: 'rgb(255, 99, 132)'

  }],

};

const config = {

  type: 'scatter',

  data: data,

  options: {

    responsive: true, // 設定圖表為響應式,根據螢幕視窗變化而變化

    maintainAspectRatio: false,// 保持圖表原有比例

    scales: {

      x: {

        type: 'linear',

        position: 'bottom'

      }

    }

  }

};

const myChart = new Chart(ctx, config);

以上執行個體輸出結果為: