Chart.js - Cartesian 軸



對於任何圖表或圖形,軸都是一個組成部分。基本上,軸用於確定我們的資料如何對映到圖表上的畫素值。笛卡爾圖使用 1 個或多個 X 軸和 1 個或多個 Y 軸將資料點對映到二維 (2-D) 畫布。這些軸稱為笛卡爾軸。

笛卡爾軸使用“options.scales.axis”名稱空間。使用笛卡爾軸的語法如下所示 −

scales: {
   x: {
      grid: {
         color: 'orange',
         borderColor: 'orange',
      }
   }
}

示例

我們來看一個使用笛卡爾軸建立圖表的示例 −

<!DOCTYPE>
<html>
<head>
   <meta charset- "UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1" />
   <title>chart.js</title>
</head>
<body>
   <canvas id="chartId" aria-label="chart" height="300" width="580"></canvas>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js"></script>
   </script>
   <script>
      var chrt = document.getElementById("chartId").getContext("2d");
      var chartId = new Chart(chrt, {
         type: 'bar',
         data: {
            labels: ["HTML", "CSS", "JAVASCRIPT", "CHART.JS", "JQUERY", "BOOTSTRP"],
            datasets: [{
               label: "online tutorial subjects",
               data: [20, 40, 30, 35, 30, 20],
               backgroundColor: ['coral', 'aqua', 'pink', 'lightgreen', 'lightblue', 'crimson'],
               borderColor: ['black'],
               borderWidth: 1,
               pointRadius: 4,
            }],
         },
         options: {
            responsive: false,
            indexAxis: 'y',
            scales: {
               x: {
                  grid: {
                     color: 'orange',
                     borderColor: 'orange',
                  }
               }
            }
         },
      });
   </script>
</body>
</html>

輸出

Cartesian Axis
廣告
© . All rights reserved.