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.canvasAxisLabelRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.canvasTextRenderer.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.pyramidRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.pyramidAxisRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.pyramidGridRenderer.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.canvasAxisLabelRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.canvasTextRenderer.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.pyramidRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.pyramidAxisRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="plugins/jqplot.pyramidGridRenderer.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.canvasAxisLabelRenderer.min.js
- jqplot.canvasTextRenderer.min.js
- jqplot.categoryAxisRenderer.min.js
- jqplot.pyramidRenderer.min.js
- jqplot.pyramidAxisRenderer.min.js
- jqplot.pyramidGridRenderer.min.js
- jquery.jqplot.min.css
JavaScript
<script>
jQuery( function() {
var ticks = [ "0-9", "10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80-89", "90-99", "100+" ];
var male = [ 10, 16, 36, 79, 68, 49, 46, 33, 20, 4, 2 ];
var female = [ 11, 20, 45, 56, 55, 44, 31, 22, 18, 5, 7 ];
var male2 = [ 8, 18, 35, 71, 53, 54, 36, 21, 15, 8, 6 ];
var female2 = [ 12, 23, 42, 51, 45, 34, 16, 12, 10, 9, 5 ];
var colors = [ "skyblue", "pink", "blue", "red" ];
jQuery . jqplot(
'jqPlot-sample',
[
male,
female,
male2,
female2
],
{
title: '<div style="float: left; width: 50%; text-align: center;">男性</div><div style="float: right; width: 50%; text-align: center;">女性</div>',
seriesColors: colors,
seriesDefaults: {
renderer: jQuery.jqplot.PyramidRenderer,
rendererOptions: {
barPadding: 4
},
yaxis: "yaxis",
shadow: false
},
series: [
{
rendererOptions:{
side: 'left',
}
},
{
yaxis: "y2axis",
},
{
rendererOptions: {
fill: false,
side: "left"
}
},
{
yaxis: "y2axis",
rendererOptions: {
fill: false
}
}
],
grid: {
drawBorder: false,
shadow: false,
background: "#f0f0f0",
rendererOptions: {
plotBands: {
show: false,
interval: 2
}
},
},
axes: {
xaxis: {
rendererOptions: {
baselineWidth: 2
}
},
yaxis: {
label: "年齢",
labelRenderer: jQuery.jqplot.CanvasAxisLabelRenderer,
showMinorTicks: true,
ticks: ticks,
rendererOptions: {
category: true,
baselineWidth: 2
}
},
y2axis: {
label: "年齢",
labelRenderer: jQuery.jqplot.CanvasAxisLabelRenderer,
showMinorTicks: true,
ticks: ticks,
rendererOptions: {
category: true,
baselineWidth: 2
}
}
}
}
);
} );
</script>
jQuery( function() {
var ticks = [ "0-9", "10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80-89", "90-99", "100+" ];
var male = [ 10, 16, 36, 79, 68, 49, 46, 33, 20, 4, 2 ];
var female = [ 11, 20, 45, 56, 55, 44, 31, 22, 18, 5, 7 ];
var male2 = [ 8, 18, 35, 71, 53, 54, 36, 21, 15, 8, 6 ];
var female2 = [ 12, 23, 42, 51, 45, 34, 16, 12, 10, 9, 5 ];
var colors = [ "skyblue", "pink", "blue", "red" ];
jQuery . jqplot(
'jqPlot-sample',
[
male,
female,
male2,
female2
],
{
title: '<div style="float: left; width: 50%; text-align: center;">男性</div><div style="float: right; width: 50%; text-align: center;">女性</div>',
seriesColors: colors,
seriesDefaults: {
renderer: jQuery.jqplot.PyramidRenderer,
rendererOptions: {
barPadding: 4
},
yaxis: "yaxis",
shadow: false
},
series: [
{
rendererOptions:{
side: 'left',
}
},
{
yaxis: "y2axis",
},
{
rendererOptions: {
fill: false,
side: "left"
}
},
{
yaxis: "y2axis",
rendererOptions: {
fill: false
}
}
],
grid: {
drawBorder: false,
shadow: false,
background: "#f0f0f0",
rendererOptions: {
plotBands: {
show: false,
interval: 2
}
},
},
axes: {
xaxis: {
rendererOptions: {
baselineWidth: 2
}
},
yaxis: {
label: "年齢",
labelRenderer: jQuery.jqplot.CanvasAxisLabelRenderer,
showMinorTicks: true,
ticks: ticks,
rendererOptions: {
category: true,
baselineWidth: 2
}
},
y2axis: {
label: "年齢",
labelRenderer: jQuery.jqplot.CanvasAxisLabelRenderer,
showMinorTicks: true,
ticks: ticks,
rendererOptions: {
category: true,
baselineWidth: 2
}
}
}
}
);
} );
</script>
- seriesDefaults
グラフの種類や、系列の色、太さなど、系列に関する初期設定。
- renderer
系列の生成編集に使うプラグインを指定。
jQuery . jqplot . BarRenderer
: 棒グラフjQuery . jqplot . BezierCurveRenderer
: ベジェ曲線グラフjQuery . jqplot . BlockRenderer
: ブロックチャートjQuery . jqplot . BubbleRenderer
: バブルチャートjQuery . jqplot . DonutRenderer
: ドーナツグラフjQuery . jqplot . MeterGaugeRenderer
: メーターゲージjQuery . jqplot . MekkoRenderer
: マリメッコ・チャートjQuery . jqplot . OHLCRenderer
: いかり足、ローソク足jQuery . jqplot . PieRenderer
: 円グラフjQuery . jqplot . PyramidRenderer
: ピラミッドグラフ
HTML
<div id="jqPlot-sample" style="height: 350px; width: 600px;"></div>