dak ブログ

python、rubyなどのプログラミング、MySQL、サーバーの設定などの備忘録。レゴの写真も。

flotでの日付の目盛

2012-05-25 00:16:00 | javascript
日毎のデータをflotでグラフ描画する際には、plot() の第3引数で xaxis の指定を行います。
日付モードにするには mode: "time" にし、表示形式を timeformat で指定します。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>日付の目盛</title>
<script type="text/javascript" src="flot/jquery.js"></script>
<script type="text/javascript" src="flot/jquery.flot.js"></script>
</head>
<body>
日付の目盛<br>
<div id="graph_area" style="width:600px;height:300px"></div>
<script type="text/javascript">
$(function () {
      var time = new Date();

      // x軸は日数
      var data1 = [[0, 3], [1, 8], [2, 5], [3, 13]];

      for (var i = 0; i < data1.length; i++) {
        // getTime() はミリ秒
        data1[i][0] = time.getTime() + data1[i][0] * 24 * 60 * 60 * 1000;
      }

      $.plot($("#graph_area"),
             [
              {
                  data: data1,
                      label: "UU",
                      lines: { show: true },
                      points: { show: true },
                      },
              ]
             ,
             {
                  xaxis: { mode: "time", timeformat: "%m/%d" },
             }
             );
  });
</script>

</body>
</html>