[graphTableライブラリ] 折れ線グラフを表示する

説明

graphTableライブラリで折れ線グラフを表示するには、あらかじめデータの元になるテーブルを作成しておきます。$()にグラフを生成するテーブルを指定した後、graphTable()メソッドを呼び出します。グラフの横幅はwidth、縦幅はheightで指定します。テーブルのセルのデータに単位が付いている場合にはdataTransformでセル内のデータを純粋な数値に直します。これはreplace()メソッドを使って単位を削除し、その値を戻り値にします。

サンプルプログラム

【HTML】
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Sample</title>
<link rel="stylesheet" href="css/loopedSlider.css" type="text/css" media="all">
<link rel="stylesheet" href="css/main.css" type="text/css" media="all">
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.flot.js"></script>
<script type="text/javascript" src="js/jquery.graphTable-0.2.js"></script>
<script type="text/javascript" src="js/sample.js"></script>
</head>
<body>
<h1>テーブルからグラフを生成</h1>
<table class="toGraph">
<caption>商品売り上げグラフ</caption>
<thead>
<tr><th>商品名</th>
<th>ココア</th><th>コーヒー</th><th>おしるこ</th>
</tr>
</thead>
<tbody>
<tr><th>9</th>
<td>10万個</td><td>20万個</td><td>30万個</td>
</tr>
<tr><th>10</th>
<td>110万個</td><td>120万個</td><td>130万個</td>
</tr>
<tr><th>11</th>
<td>70万個</td><td>40万個</td><td>150万個</td>
</tr>
<tr><th>12</th>
<td>20万個</td><td>80万個</td><td>110万個</td>
</tr>
</tbody>
</table>
</body>
</html>

【sample.js】
$(function(){
$(".toGraph").graphTable({series: "columns",
width:500,
height:200,
dataTransform: function(s) { return(s.replace("万個","")); }
});
});
サンプルを実行
[戻る]