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

标签 javascript jquery html css

<!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)

关于javascript - 如何使用 js 库使用两个输入 x 和 y 值绘制图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38911116/

相关文章:

javascript - 如何加入npm命令?

Javascript 回调,引用自己的对象

javascript - 如何使用 javascript/jquery 仅显示具有特定类的 li?

html - 图像下方有很多空白

javascript - 有条件激活的 rxjs 可观察速率限制器

javascript - 如何修复 : Navbar styling issue

javascript - 需要加载更多按钮以将所有元素向下移动页面

jquery - 如果 autowidth :true is used,如何删除免费的 jqgrid 自己的水平滚动条

javascript - 在我的 html 中使用 Node 函数

javascript - 如何使用 localscroll.js 进行水平导航