如何使用js库使用两个输入x和y值绘制图形

How to draw the graph using two input x and y value using js library?

本文关键字:输入 两个 图形 绘制 js 何使用      更新时间:2023-09-26

<!DOCTYPE html>
<html>
<head>
  <title>math.js | plot</title>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/mathjs/3.2.1/math.min.js"></script>
  <!-- load http://maurizzzio.github.io/function-plot/ -->
  <script src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
  <script src="https://wzrd.in/standalone/function-plot@1.14.0"></script>
  <style>
    input[type=text] {
      width: 300px;
    }
    input {
      padding: 6px;
    }
    body, html, input {
      font-family: sans-serif;
      font-size: 11pt;
    }
    form {
      margin: 20px 0;
    }
  </style>
</head>
<body>
<form id="form">
  <label for="eq">Enter an equation:</label>
  <input type="text" id="eq" value="4 * sin(x) + 5 * cos(x/2)  " />
  <input type="submit" value="Draw" />
</form>
<div id="plot"style="position:absolute;top:20%;"></div>
<script>
  function draw() {
    try {
      functionPlot({
        target: '#plot',
        data: [{
          fn: document.getElementById('eq').value,
          sampler: 'builtIn',  // this will make function-plot use the evaluator of math.js
          graphType: 'polyline'
        }]
      });
    }
    catch (err) {
      console.log(err);
      alert(err);
    }
  }
  document.getElementById('form').onsubmit = function (event) {
    event.preventDefault();
    draw();
  };
  draw();
</script>
</body>
</html>

我有一些代码在键入方程来绘制图形相同的概念如何绘制x和y值中的两个输入值来绘制图形请帮助我更正我的代码。

例如:x=10;y=5输出根据输入值绘制图形。

它运行良好。你的数字太高了。尝试4*sin(0.479425538604203(+5*cos(x/2*x+12(