2016年10月23日日曜日

D3.js : Bubbleチャートの利用

Bubbleチャート表示をやってみたくてやってみました。
http://bl.ocks.org/mbostock/4063269
を見てやってみようと思ったのですが、データ階層が面倒だったので簡単な方法で
http://www.openspc2.org/reibun/D3.js/code/graph/bubble-chart/0003/index.html
がシンプルだったのでこれを使わせてもらいました。

 1. jsファイルの作成
app]$ vi webroot/js/bubble.js
上記で使っているファイルに色をランダムにする部分を追加してあります。
var svgWidth = 2080; // SVG領域の横幅
var svgHeight =1080; // SVG領域の縦幅
var diameter = 1080; // 一番大きい円のサイズ
var color = new Array(100);
color[0] = "none";
for(var index = 1; index <= 100; index++){
color[index] = rgbTo16("rgb("+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")");
}
//var color = ["none", "#ff0000", "#", "orange", "#ffe090", "#a0ff90", "cyan", "#ccc", "#ff8080", "#c0ffc0", "#4090ff"];
var svg = d3.select("#myGraph").append("svg")
.attr("width", svgWidth).attr("height", svgHeight)
var bubble = d3.layout.pack()
.size([diameter, diameter]) // 表示サイズを指定
var grp = svg.selectAll("g") // グループを対象にする
.data(bubble.nodes(classes(list))) // データを読み込む
.enter()
.append("g")
.attr("transform", function(d) { // 円のX,Y座標を設定
return "translate(" + d.x + "," + d.y + ")";
})
grp.append("circle") // 円を生成する
.attr("r", function(d){ // 円を指定した半径にする
return d.r;
})
.attr("fill", function(d,i){ // 塗りの色を指定
return color[i];
//return rgbTo16("rgb(Math.floor(Math.random()*256), 0, 255)");
})
grp.append("text") // 文字を生成する
.attr("font-size", "9pt") // 文字のサイズを指定する
.attr("fill", "black") // 文字の塗りの色を指定する
.style("text-anchor", "middle") // 円の座標の中央から表示するようにする
.text(function(d) { return d.className; } ) // データの中のclassNameが地区名なので、それを出力
function classes(root) {
var classes = [];
function recurse(name, node) {
if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
else classes.push({packageName: name, className: node.name, value: node.households});
}
recurse(null, root);
return {children: classes};
}
function rgbTo16(col){ //color配列を作成
return "#" + col.match(/\d+/g).map(function(a){return ("0" + parseInt(a).toString(16)).slice(-2)}).join("");
}
view raw bubble.js hosted with ❤ by GitHub


2.viewへの取り込み
app]$ vi View/Keywords/graph.ctp
<div id="myGraph"></div>
<script>
var list = {
"name": "Keyphrase",
"children": [
{
"name": "twitter",
"children": [
<?php for($index = 0; $index < $count; $index++){ ?>
{ "name": "<?php echo $weight[$index][0];?>", "households": <?php echo $weight[$index][1];?> },
<?php }?>
]
}
]
};
</script>
<?php echo $this->Html->script('bubble.js');?>
view raw bubble-view hosted with ❤ by GitHub


ここで使っている$countはコントローラで生成してあります。
if(count($weight) < 100 ) {
    $count = count($weight);
}else{
    $count = 100;
}
$this->set('count',$count);
100件未満だと表示されないようなので、その場合は配列数にしてあります。
これで表示するとこんな感じです。
16進数への変換は
http://d.hatena.ne.jp/DECKS/20100907/1283843862
を利用しています。

2016年10月20日木曜日

サーバ証明書の作成・設定

サーバ証明書の作成と設定をします。

1.設定したいサーバでCSR(証明書発行要求:Certificate Signing Request)を作成します。OpenSSLを用いて作成をします。
[OpenSSLのインストール]
# yum install openssl

[鍵ペアの生成]
# cd /etc/httpd/conf
# mkdir ssl.key   作業ディレクトリの作成
# vi random1.txt    鍵生成用ランダムファイル
# vi random2.txt
# vi random3.txt
# openssl genrsa -des3 -rand random1.txt:random2.txt:random3.txt 2048 >test.sample.ac.jp.key
[CSRの生成]
ssl.key]# openssl req -new -key test.sample.ac.jp.key -sha256 -out test.sample.ac.jp.csr
Enter pass phrase for test.sample.ac.jp.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:.
Locality Name (eg, city) [Default City]:Fukuroi
注意(2019/4/1変更) Academe固定は廃止されました。
2018年7月9日以降、L=Academeを指定するとエラーとなります
Organization Name (eg, company) [Default Company Ltd]:Shizuoka Institute of Science and Technology
Organizational Unit Name (eg, section) []:sist.ac.jp
Common Name (eg, your name or your server's hostname) []:test.sample.ac.jp
Email Address []:.

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.
 内容確認
# openssl req -noout -text -in cloud02.sist.ac.jp.csr
Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=JP, L=Academe, O=Shizuoka Institute of Science and Technology, OU=sist.ac.jp, CN=test.sample.ac.jp
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:bd:fe:3a:13:dc:fb:b3:8d:bd:67:00:12:bf:b4:

このcsrファイルをダウンロードします。

2.申請用TSVファイルの作成
https://certs.nii.ac.jp/tsv-tool/
にアクセスしてtsvファイルを作成します。


補足(2019/4/1)
しているFQDNのホスト名以外でも利用する場合、(例:office365.sist.ac.jpで登録しておいて、fsecure.sist.ac.jpでも使う場合、dNSNameの部分に例のように書き込む:記入例:dNSName=xxx.example.ac.jp,dNSName=yyy.example.ac.jp)


ここでtsvファイルを作成し、NIIのサイトへアップロードし、証明書を発行します。
NIIからメールで送られてくる。

リンクからダウンロードしファイル名.crtで保存する。

3.証明書のインストール
[mod_sslインストール]
# yum install mod_ssl

# vi /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/httpd/conf/ssl.crt/cloud02.sist.ac.jp.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/cloud02.sist.ac.jp.key
SSLCertificateChainFile  /etc/httpd/conf/ssl.crt/nii-odca3sha2.cer

 
[中間証明書のインストール]
https://repo1.secomtrust.net/sppca/nii/odca3/index.html
より中間証明書を取得します。
/etc/httpd/conf/ssl.crt/nii-odca3sha2.cer  に設置します。
# mkdir  /etc/httpd/conf/ssl.crt
# cp nii-odca3sha2.cer /etc/httpd/conf/ssl.crt/

[サーバ証明書の設置]
# cp cloud02.sist.ac.jp.crt /etc/httpd/conf/ssl.crt/

サーバを再起動
# service httpd restart

[確認]
設定したサーバにhttpsでアクセスします。
https://サーバ名/
サーバ証明書の確認ができました。

参考
https://certs.nii.ac.jp/?action=pages_view_main&active_action=repository_view_main_item_detail&item_id=1&item_no=1&page_id=85&block_id=439

2016年10月6日木曜日

WordCloudの利用

形態素解析した結果を表示するのにWordCloudを使ってみました。

1.ライブラリの設置
今回使ったファイルは
d3.layout.cloud.js
word-cloud.js
d3.v3.min.js
です。app/webroot/jsに配置します。またはURL指定です。
ダウンロードは
https://github.com/shprink/d3js-wordcloud
https://github.com/jasondavies/d3-cloud/tree/master/build
からしました。

2.形態素解析した結果を取り込みます。
今回は View/graph.ctpに書きました。
<script src="https://d3js.org/d3.v3.min.js"></script>
<?php echo $this->Html->script('d3.layout.cloud.js');?>
<script>var tags = [
<?php for($index = 0; $index < count($weight); $index++){ ?>
{"key": "<?php echo $weight[$index][0];?>", "value": <?php echo $weight[$index][1];?>},
<?php }?>
]
</script>
<div id="vis"></div>
<?php echo $this->Html->script('word-cloud.js');?>
view raw graph.ctp hosted with ❤ by GitHub


オリンピックのtweet結果を形態素分析して、WordCloudで表示するとそれなりでした。