サンプル・プログラムの実行例

女性で見ると、さらに顕著だ。年齢が高くなるに伴って、ますます長生きすることが見て取れる。
20~30代の死因の上位は、自殺、事故であるから、これらを減らせば、平均寿命は更に伸びるのではないだろうか。
サンプル・プログラム
viewLifeExpectancy.php | サンプル・プログラム本体 |
LifeExpectancy.csv | 簡易生命表データファイル |
バージョン | 更新日 | 内容 |
---|---|---|
1.2 | 2022/04/09 | PHP8対応,リファラ・チェック改良 |
1.11 | 2019/07/14 | グラフの色を指定できるように |
1.1 | 2019/07/14 | グラフの表示幅・高さを指定できるように |
1.01 | 2019/07/14 | リファラ・チェック追加 |
1.0 | 2017/07/30 |
解説:準備
公式サイト「jqPlot Charts and Graphs for jQuery」から圧縮ファイルをダウンロードし、適当な場所に解凍しておく。その場所を定数 JQPLOT に定義する。

また、厚生労働省「平成30年簡易生命表」に掲載されている図表データから、1947年(昭和22年)以降の毎年の平均余命の推移を元に、データファイル "LifeExpectancy.csv" を用意した。カラム区切りはタブである。
これを読み込んで、プログラム内で伸び率を計算、グラフ表示させる。
西暦年 | 平均余命 | |||||||||||
男性 | 女性 | |||||||||||
0歳 | 20歳 | 40歳 | 65歳 | 75歳 | 90歳 | 0歳 | 20歳 | 40歳 | 65歳 | 75歳 | 90歳 |
解説:jqPlotスクリプト
249: /**
250: * jqPlot用のスクリプト:下請け
251: * @param array $items データ配列
252: * @param string $category 種別
253: * @return string スクリプト
254: */
255: function plotSub($items, $category) {
256: //プロットするデータ
257: switch ($category) {
258: case 'zero':
259: $table = array('男性', '女性');
260: $colors = array('blue', 'red'); //グラフの色
261: break;
262: default:
263: $table = array('0歳', '20歳', '40歳', '65歳', '75歳', '90歳');
264: $colors = array('cyan', 'green', 'blue', 'red', 'purple', 'orange');
265: break; //グラフの色
266: }
267:
268: //系列の生成
269: $str2 = '';
270: foreach ($table as $key=>$val) {
271: $str2 .= "\t\t\t{ label: '{$val}', color: '{$colors[$key]}' },\n";
272: $series[] = '';
273: }
274:
275: $year_min = 9999;
276: $year_max = 0;
277: foreach ($items as $year=>$val) {
278: if ($year < $year_min) $year_min = $year; //X軸の最小値
279: if ($year > $year_max) $year_max = $year; //X軸の最大値
280: if ($category == 'male') {
281: for ($key = 0; $key < 6; $key++) {
282: $series[$key] .= sprintf("['%04d-01-01', %4.1f],", $year, $val[$key]);
283: }
284: } else if ($category == 'female') {
285: for ($key = 6; $key < 12; $key++) {
286: $series[$key - 6] .= sprintf("['%04d-01-01', %4.1f],", $year, $val[$key]);
287: }
288: } else {
289: $series[0] .= sprintf("['%04d-01-01', %4.1f],", $year, $val[0]);
290: $series[1] .= sprintf("['%04d-01-01', %4.1f],", $year, $val[6]);
291: }
292: }
293: $year_min = floor($year_min / 5) * 5;
294: $year_max = ceil($year_max / 5) * 5;
295:
296: $str1 = '';
297: foreach ($series as $val) {
298: $str1 .= "\t\t[ " . $val . "],\n";
299: }
300:
301: $js =<<< EOT
302: jQuery(function() {
303: jQuery.jqplot('jqPlot_polls',
304: [
305: {$str1}
306: ],
307: {
308: //系列
309: series: [
310: {$str2}
311: ],
312: legend: {
313: show: true,
314: placement: 'inside',
315: location: 'nw',
316: renderer: $.jqplot.EnhancedLegendRenderer,
317: rendererOptions: { numberRows: 1 }
318: },
319: seriesDefaults: {
320: showLine: true,
321: rendererOptions: { smooth: false },
322: markerOptions: { size: 0 },
323: },
324: //軸
325: axes: {
326: xaxis: {
327: renderer:$.jqplot.DateAxisRenderer,
328: tickOptions: { formatString: '%Y' },
329: label: '西暦年',
330: min: '{$year_min}-01-01',
331: max: '{$year_max}-12-31',
332: },
333: yaxis: {
334: label: '伸び率'
335: }
336: },
337: //ハイライター
338: highlighter: {
339: show: true,
340: showMarker: true,
341: tooltipLocation: 'sw',
342: fadeTooltip: false,
343: bringSeriesToFront: true,
344: tooltipAxes: 'xy',
345: formatString: '%s年<br />伸び率%s'
346: }
347: }
348: );
349: });
350:
351: EOT;
352:
353: return $js;
354: }
365: /**
366: * jqPlot用のスクリプト:女性
367: * @param array $items データ配列
368: * @return string スクリプト
369: */
370: function plotFemale($items) {
371: return plotSub($items, 'female');
372: }
jqPlotの使い方は、「jqPlot - jQuery プラグイン」(アルファシス)が詳しい。

男性、女性、0歳余命は、伸び率を格納しているデータ配列 $items から表示させる要素が違うだけなので、下請け関数 plotSub 内で場合分けしてスクリプトを発生させるように下。
参考サイト
- jqPlot Charts and Graphs for jQuery:公式
- 令和2年簡易生命表:厚生労働省
- PHPでNHK政治意識月例調査をグラフ表示:ぱふぅ家のホームページ
- jqPlot - jQuery プラグイン:アルファシス
一方、身の回りで振り返ると他界した同期も少なくなく、年齢階層別の平均余命の伸びはどうなっているのだろうか、と疑問を感じた。
そこで、厚労省のデータを使って、「PHPでNHK政治意識月例調査をグラフ表示」で作ったPHPプログラムをベースに、平均余命の伸び率を暗く表示させてみることにする。
(2023年2月13日)令和3年度簡易生命表に対応