Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

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>

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

yuki24
Ruby committer, Ruby on Rails contributor, Kaminari maintainer, creator of the did_you_mean gem
http://www.yukinishijima.net
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away