ãè¨èªå¦ç 100 æ¬ããã¯ãã« PHP ã§ææ¦ãã¦ãã¾ããä»åã¯ç¬¬ 4 ç« ã®æ®ãã®åé¡ã解ãã¾ãã
www.cl.ecei.tohoku.ac.jp
37. é »åº¦ä¸ä½10èª
åºç¾é »åº¦ãé«ã10èªã¨ãã®åºç¾é »åº¦ãã°ã©ãï¼ä¾ãã°æ£ã°ã©ããªã©ï¼ã§è¡¨ç¤ºããï¼
<?php require_once __DIR__ . '/read_mecab_data.php'; require_once __DIR__ . '/term_frequency.php'; $morphs = read_mecab_data('neko.txt.mecab'); $frequencies = term_frequency($morphs); arsort($frequencies); $top10 = array_slice($frequencies, 0, 10, true); $labels = json_encode(array_keys($top10)); $data = json_encode(array_values($top10)); ?> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script> </head> <body> <div id="container" style="width: 800px; padding: 4px; border: solid 1px;"> <canvas id="canvas"></canvas> </div> <script> window.onload = function() { var context = document.getElementById("canvas").getContext("2d"); window.myChart = new Chart(context, { type: "bar", data: { labels: <?= $labels ?>, datasets: [{ data: <?= $data ?>, backgroundColor: "hsl(240, 100%, 75%)" }] }, options: { title: { display: true, text: "é »åº¦ä¸ä½ 10 èª" }, scales: { yAxes: [{ ticks: { beginAtZero: true } }] }, legend: { display: false } } }); }; </script> </body> </html>
Chart.js ãå©ç¨ãã¦ãã¦ã§ããã©ã¦ã¶ã§è¡¨ç¤ºã§ããããã«å®è£
ãã¾ããã
Chart.js | Open source HTML5 Charts for your website
ããã°ã©ã 㯠HTML ã®ä¸ã« PHP ã¹ã¯ãªãããåãè¾¼ã¾ããå½¢å¼ã«ãªã£ã¦ãã¾ããå é é¨åã§ã°ã©ãã«è¡¨ç¤ºãããã¼ã¿ãçæãã¦ãscript è¦ç´ å ã® JavaScript ã«æ¸ãåºãã¾ããä»ã§ã¯ããã®ããã« HTML 㨠PHP ãæ··å¨ãããæ¸ãæ¹ã¯æ¨å¥¨ãããªãã¨æãã¾ãããä»åã¯æ°ã«ããªããã¨ã«ãã¾ãã
PHP ã¹ã¯ãªããã®å®è£ å 容ã¯ãããã¾ã§ã®åé¡ã¨å¤§ããªéãã¯ããã¾ãããåèªã®åºç¾é »åº¦ãè¨ç®ãã¦éé ã«ä¸¦ã¹æ¿ããarray_slice ã§å é ã® 10 èªãåå¾ãã¾ããarray_slice ã®æå¾ã®å¼æ°ã¯ãé åã®ãã¼ããã®ã¾ã¾ç¶æãããªãã·ã§ã³ã§ãã$top10 ã¯ãã¼ãåèªã®è¡¨å±¤å½¢ãå¤ãåºç¾é »åº¦ã«ãªã£ã¦ããã®ã§ãããããã JSON ã«å¤æã㦠$labels 㨠$data ã«æ ¼ç´ãã¾ãã
HTML å´ã®å®è£
ã¯ãChart.js ã®ãµã³ãã«ã¹ã¯ãªãããåèã«ãã¾ããã GitHub ãªãã¸ããªã® samples ãã£ã¬ã¯ããªã«ãChart.js ã§è¡¨ç¤ºã§ãããã¾ãã¾ãªã°ã©ãã®ãµã³ãã«ãããã¾ãã
GitHub - chartjs/Chart.js: Simple HTML5 Charts using the <canvas> tag
ããã°ã©ã ãå®è¡ããã«ã¯ã以ä¸ã®ã³ãã³ã㧠PHP ã®ãã«ãã¤ã³ãµã¼ããèµ·åãã¾ã*1ãç¹ã«ãªãã·ã§ã³ãæå®ããªããã°ãã«ã¬ã³ããã£ã¬ã¯ããªãããã¥ã¡ã³ãã«ã¼ãã«ãªãã¾ã*2ã
$ php -S localhost:8000
ãã©ã¦ã¶ããã¢ã¯ã»ã¹ããã¨ã次ã®ã°ã©ãã表示ããã¾ãã
38. ãã¹ãã°ã©ã
åèªã®åºç¾é »åº¦ã®ãã¹ãã°ã©ã ï¼æ¨ªè»¸ã«åºç¾é »åº¦ï¼ç¸¦è»¸ã«åºç¾é »åº¦ãã¨ãåèªã®ç¨®é¡æ°ãæ£ã°ã©ãã§è¡¨ãããã®ï¼ãæãï¼
<?php require_once __DIR__ . '/read_mecab_data.php'; require_once __DIR__ . '/term_frequency.php'; $morphs = read_mecab_data('neko.txt.mecab'); $frequencies = term_frequency($morphs); $bins = array_fill(0, 10, 0); foreach ($frequencies as $frequency) { ++$bins[intval($frequency / 1000)]; } $labels = json_encode(range(500, 9500, 1000)); $data = json_encode($bins); ?> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script> </head> <body> <div id="container" style="width: 800px; padding: 4px; border: solid 1px;"> <canvas id="canvas"></canvas> </div> <script> window.onload = function() { var context = document.getElementById("canvas").getContext("2d"); window.myChart = new Chart(context, { type: "bar", data: { labels: <?= $labels ?>, datasets: [{ data: <?= $data ?>, backgroundColor: "hsl(240, 100%, 75%)" }] }, options: { title: { display: true, text: "ãã¹ãã°ã©ã " }, legend: { display: false } } }); }; </script> </body> </html>
åé¡ 37 ã¨åæ§ã«å®è£
ãã¾ããåºç¾é »åº¦ãæã大ããªåèªã¯ãã®ã㧠9,194 åã ã¨ãããã¨ãåãã£ã¦ããã®ã§ã1,000 å»ã¿ã§ 10 éç´ã«åå²ãã¾ããã左端ã 1 åãã 1,000 åã§ããå®è¡çµæã¯ä»¥ä¸ã®ã¨ããã§ãã»ã¼ãã¹ã¦ã®åèªãåºç¾é »åº¦ã®ä½ãã¨ããã«éä¸ãã¦ãããã¨ãåããã¾ãã
念ã®ãããããã°ã©ã ãå¤æ´ãã¦åºç¾é »åº¦ã 1 ãã 20 ã®ç¯å²ã表示ããã¦ã¿ãã¨ã次ã®ããã«ãªãã¾ããã
39. Zipfã®æ³å
åèªã®åºç¾é »åº¦é ä½ã横軸ï¼ãã®åºç¾é »åº¦ã縦軸ã¨ãã¦ï¼ä¸¡å¯¾æ°ã°ã©ããããããããï¼
<?php require_once __DIR__ . '/read_mecab_data.php'; require_once __DIR__ . '/term_frequency.php'; $morphs = read_mecab_data('neko.txt.mecab'); $frequencies = term_frequency($morphs); arsort($frequencies); $data = json_encode(array_map( function ($n, $count) { return [ 'x' => $n, 'y' => $count ]; }, range(1, count($frequencies)), $frequencies )); ?> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script> </head> <body> <div id="container" style="width: 800px; padding: 4px; border: solid 1px;"> <canvas id="canvas"></canvas> </div> <script> window.onload = function() { var context = document.getElementById("canvas").getContext("2d"); window.myChart = new Chart(context, { type: "line", data: { datasets: [{ data: <?= $data ?>, fill: false, borderColor: "hsl(240, 100%, 75%)", pointRadius: 0 }] }, options: { title: { display: true, text: "Zipf ã®æ³å" }, legend: { display: false }, scales: { xAxes: [{ type: "logarithmic", position: "bottom", }], yAxes: [{ type: "logarithmic", }] } } }); }; </script> </body> </html>
Chart.js ã§æ£å¸å³ã表示ããã«ã¯ãdata ã®åè¦ç´ ããªãã¸ã§ã¯ãã¨ã㦠(x, y) ãããããããããã£ã¨ãã¦è¨å®ããããã§ããå
é ã® PHP ã¹ã¯ãªããé¨åã§ããã®ãããªãã¼ã¿æ§é ãä½æãã¦ãã¾ããã°ã©ãã®è¨å®ã¨ãã¦ã¯ãpointRadius: 0 ãæå®ãã¦ãã¼ã«ã¼ãé表示ã«ãã軸ã«ã¯ type: "logarithmic" ãæå®ãã¦ä¸¡å¯¾æ°ã°ã©ãã«ãã¦ãã¾ãã
対æ°ã°ã©ãã«ããã¨è»¸ã®ã©ãã«ãææ°å½¢å¼ã«ãªãããã§ããã¾ããX 軸ãè¦ãã¨ãã©ãã«ãéãªããã«è¡¨ç¤ºã§ããééã«é©å½ã«éå¼ããã¦ãã¾ãããã®ãããªæåããªãã·ã§ã³ã®è¨å®ã§å¤æ´ã§ãã¾ãããã¨ãã° scales ã®è¨å®ã次ã®ããã«å¤æ´ããã¨ãããæ«å°¾ã®ã°ã©ããå¾ããã¾ããã
scales: { xAxes: [{ type: "logarithmic", position: "bottom", ticks: { maxRotation: 90, minRotation: 90, callback: function($value) { return "125".indexOf(($value + "").substr(0, 1)) == -1 ? "" : $value; } } }], yAxes: [{ type: "logarithmic", ticks: { callback: function($value) { return "125".indexOf(($value + "").substr(0, 1)) == -1 ? "" : $value; } } }] }
*1:ãã¡ããããã«ãã¤ã³ãµã¼ã以å¤ã®æ¹æ³ã§ã確èªã§ãã¾ãããã¨ãã°ã¦ã§ããµã¼ããå©ç¨ããªãæ¹æ³ã¨ãã¦ã¯ãä½æããã¹ã¯ãªããã php ã³ãã³ãã§å®è¡ããã° HTML ãã¡ã¤ã«ãåºåãããã®ã§ããããéãã°å®è¡çµæã確èªã§ãã¾ãã
*2:-t ãªãã·ã§ã³ã§ããã¥ã¡ã³ãã«ã¼ããå¤æ´ã§ãã¾ãã