jQueryのjqPlotプラグインで、科目別得点推移の縦棒グラフを描くチュートリアル。
実装例(サンプル)
実装例(サンプル)の動作について
凡例内の科目名をクリックすると、その科目の縦棒(バー)をフェードアウトで、非表示にする。再度クリックすると、その科目の縦棒(バー)をフェードインで、表示する。
実装例(サンプル)のソースコード
読み込み
パスは、それぞれ、アップロードした場所を指定する。
<script language="javascript" type="text/javascript" src="jquery-1.7.1.min.js"></script>
<!--[if lt IE 9]>
<script language="javascript" type="text/javascript" src="excanvas.min.js"></script>
<![endif]-->
<script language="javascript" type="text/javascript" src="jquery.jqplot.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.barRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.categoryAxisRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.enhancedLegendRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.pointLabels.min.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.jqplot.min.css" />
<!--[if lt IE 9]>
<script language="javascript" type="text/javascript" src="excanvas.min.js"></script>
<![endif]-->
<script language="javascript" type="text/javascript" src="jquery.jqplot.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.barRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.categoryAxisRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.enhancedLegendRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.pointLabels.min.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.jqplot.min.css" />
読み込むファイル。
- jquery-1.7.1.min.js
- jquery.jqplot.min.js
- jqplot.barRenderer.min.js
- jqplot.categoryAxisRenderer.min.js
- jqplot.enhancedLegendRenderer.min.js
- jqplot.pointLabels.min.js
- jquery.jqplot.min.css
JavaScript
<script>
jQuery( function() {
Kokugo = [ [ 1, 23 ], [ 2, 34 ], [ 3, 52 ], [ 4, 47 ], [ 5, 78 ], [ 6, 66 ] ];
Sansuu = [ [ 1, 65 ], [ 2, 72 ], [ 3, 74 ], [ 4, 63 ], [ 5, 85 ], [ 6, 90 ] ];
Rika = [ [ 1, 83 ], [ 2, 81 ], [ 3, 79 ], [ 4, 88 ], [ 5, 78 ], [ 6, 100 ] ];
Shakai = [ [ 1, 81 ], [ 2, 86 ], [ 3, 71 ], [ 4, 69 ], [ 5, 58 ], [ 6, 82 ] ];
jQuery . jqplot(
'jqPlot-sample',
[
Kokugo, Sansuu, Rika, Shakai
],
{
title: '科目別得点推移の縦棒グラフ',
showMarker: false,
seriesDefaults: {
renderer: jQuery . jqplot . BarRenderer,
pointLabels: {
show: true,
location: 's',
ypadding: -3,
escapeHTML: false,
formatString: '<b style="color: red;">%d</b>'
}
},
axes: {
xaxis: {
label: '月',
renderer: jQuery . jqplot . CategoryAxisRenderer,
},
yaxis: {
label: '点数',
ticks: [ 0, 25, 50, 75, 100 ],
tickOptions: {
formatString: '%d'
}
}
},
series: [
{
label: '国語'
},
{
label: '算数'
},
{
label: '理科'
},
{
label: '社会'
}
],
legend: {
show: true,
placement: 'outsideGrid',
location: 'n',
renderer: jQuery . jqplot . EnhancedLegendRenderer,
rendererOptions: {
numberColumns: 4
}
}
}
);
} );
</script>
jQuery( function() {
Kokugo = [ [ 1, 23 ], [ 2, 34 ], [ 3, 52 ], [ 4, 47 ], [ 5, 78 ], [ 6, 66 ] ];
Sansuu = [ [ 1, 65 ], [ 2, 72 ], [ 3, 74 ], [ 4, 63 ], [ 5, 85 ], [ 6, 90 ] ];
Rika = [ [ 1, 83 ], [ 2, 81 ], [ 3, 79 ], [ 4, 88 ], [ 5, 78 ], [ 6, 100 ] ];
Shakai = [ [ 1, 81 ], [ 2, 86 ], [ 3, 71 ], [ 4, 69 ], [ 5, 58 ], [ 6, 82 ] ];
jQuery . jqplot(
'jqPlot-sample',
[
Kokugo, Sansuu, Rika, Shakai
],
{
title: '科目別得点推移の縦棒グラフ',
showMarker: false,
seriesDefaults: {
renderer: jQuery . jqplot . BarRenderer,
pointLabels: {
show: true,
location: 's',
ypadding: -3,
escapeHTML: false,
formatString: '<b style="color: red;">%d</b>'
}
},
axes: {
xaxis: {
label: '月',
renderer: jQuery . jqplot . CategoryAxisRenderer,
},
yaxis: {
label: '点数',
ticks: [ 0, 25, 50, 75, 100 ],
tickOptions: {
formatString: '%d'
}
}
},
series: [
{
label: '国語'
},
{
label: '算数'
},
{
label: '理科'
},
{
label: '社会'
}
],
legend: {
show: true,
placement: 'outsideGrid',
location: 'n',
renderer: jQuery . jqplot . EnhancedLegendRenderer,
rendererOptions: {
numberColumns: 4
}
}
}
);
} );
</script>
HTML
<div id="jqPlot-sample" style="height: 400px; width: 550px;"></div>