7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dygraph.js を使って関数を描画

Last updated at Posted at 2012-03-16

ここでいう関数とは、数学の関数のこと。

headタグ内
<script type="text/javascript" src="js/dygraph-combined.js"></script>
<script type="text/javascript">
  function plot(graphArea, fromX, toX, fn) {
    var graph = document.getElementById(graphArea);
    var width = parseInt(graph.style.width);
    var xs = 1.0 * (toX - fromX) / width;

    var data = [];
    for (var i = 0; i < width; i++) {
      var x = fromX + i * xs;
      var y = fn(x);
      var row = [x];
      if (y.length > 0) {
        for (var j = 0; j < y.length; j++) row.push(y[j]);
      } else {
        row.push(y);
      }
      data.push(row);
    }

    g = new Dygraph(graph, data);
  }
</script>
bodyタグ内
<div id="graph" style="margin: 10px auto; width:512px; height:400px;"></div>
<script type="text/javascript">
  plot("graph", -1.5, 1.5, function(x){
    return Math.cos(x);
  });
</script>

これを使ってグラフを描くと…

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?