ChartJS 我尝试在JavaScript中以任意Angular 旋转椭圆,但结果甚至不是椭圆

wmomyfyw  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(145)

我想在图表中显示菲涅耳区,我希望在其中旋转一个任意Angular 的椭圆。我已经给出了开始和结束的x和y坐标,我希望椭圆不是在中心旋转,而是在开始点旋转。我已经从下面的链接中获得帮助,但我仍然无法得到一个正确的椭圆旋转一个Angular 。任何帮助将不胜感激。此外,x坐标需要格式化以将椭圆聚集在一起。但我也无法管理这一点。
Drawing (Fresnel) ellipse and major axis

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

    <style>
      .chart-container {
        width: 600px;
      }
    </style>
  </head>
  <body>
    <div class="chart-container">
      <canvas id="myChart"></canvas>
    </div>
    <script>
      var step = (2 * Math.PI) / 30;
      console.log("step", step);
      f = 217.25; // frequency
      x1 = 0;
      x2 = 15;
      y2 = 250;
      y1 = 130;
      fr = f * Math.pow(10, 6); // frequency in Hz
      c = 2.997925 * Math.pow(10, 8); // speed of light
      lambda = c / fr;
      xarr = [];
      yarr = [];
      a = (1 / 2) * Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); // radius of major axis
      r = Math.sqrt((lambda * a) / 2); // radius of the minor axis
      w = Math.atan2(y2 - y1, x2 - x1);
      console.log(w);
      for (i = 0; i <= 2 * Math.PI; i += step) {
        xval = a * Math.cos(i);
        yval = r * Math.sin(i);
        xnew = (x1 + x2) / 2 + xval * Math.cos(w) - yval * Math.sin(w);
        ynew = (y1 + y2) / 2 + xval * Math.sin(w) + yval * Math.cos(w);
        xarr.push(xnew);
        yarr.push(ynew);
      }
      console.log(xarr);
      const data = {
        labels: xarr,
        datasets: [
          {
            data: yarr,
            borderColor: "rgba(0,0,0,1)",
          },
        ],
      };
      const config = {
        type: "line",
        data: data,
        options: {
          scales: {
            x: {
              ticks: {
                maxTicksLimit: 5,
                beginAtZero: true,
              },
            },
            y: {
              beginAtZero: true,
              maxTicksLimit: 5,
              grid: {
                display: true,
              },
            },
          },
        },
      };
      const mychart = new Chart(document.getElementById("myChart"), config);
    </script>
  </body>
</html>````
m1m5dgzv

m1m5dgzv1#

您需要将x轴设为缐性刻度,以符合您的使用案例。预设情况下,它是类别坐标轴,也就是说它只会将标签数组当做标签。如果您将刻度设为缐性刻度,它会将数据Map到坐标轴上的正确点:

options: {
  scales: {
    x: {
      type: 'linear'
    }
  }
}

示例:
第一个

相关问题