0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PCR検査の実施状況のグラフを作成

Posted at

厚生労働省のデータを使って次のようなグラフを作成します。
corona_mar14.png

フォルダー構造

$ tree
.
├── data_inspect.json
├── index.html -> inspect_organization.html
├── inspect_organization.html
└── inspect_organization.js
inspect_organization.html
<!DOCTYPE html>
<html lang="ja">
<head>
<title>PCR検査の実施状況</title>
<meta charset="UTF-8">
<script src="/js/jquery-3.4.1.min.js"></script>
<script src="/js/Chart.min.js"></script>
<script src="inspect_organization.js"></script>
</head>
<body>
<h1>国内における新型コロナウイルスに係るPCR検査の実施状況</h1>
<canvas id="chart" height="200" width="400"></canvas>
<p />
データソース<br />
<blockquote>
	<a href="https://www.mhlw.go.jp/stf/seisakunitsuite/bunya/0000121431_00086.html">厚生労働省の報道発表資料</a><p />
	<a href="https://www.mhlw.go.jp/content/10900000/000608163.pdf">
国内における新型コロナウイルスに係るPCR検査の実施状況(2020年3月13日掲載分)</a><p />
</blockquote>
<hr />
<div id="outarea_aa">outarea_aa</div>
<div id="outarea_bb">outarea_bb</div>
<div id="outarea_cc">outarea_cc</div>
<div id="outarea_dd">outarea_dd</div>
<div id="outarea_ee">outarea_ee</div>
<div id="outarea_ff">outarea_ff</div>
<div id="outarea_gg">outarea_gg</div>
<div id="outarea_hh">outarea_hh</div>
<hr />
Mar/14/2020<p />
</body>
</html>
inspect_organization.js
// ----------------------------------------------------------------------
//	inspect_organization.js
//
//					Mar/14/2020
// ----------------------------------------------------------------------
const file_json = "data_inspect.json"

jQuery.getJSON(file_json,function (data)
	{
	jQuery("#outarea_aa").text ("*** start ***")
	const graph_data = convert01(data)
	prog01 (graph_data)
	jQuery("#outarea_hh").text ("*** end ***")
	})


// ----------------------------------------------------------------------
function convert01(data)
{
	var count = 0
	var labels = []
	var data_aa = []
	var data_bb = []
	var data_cc = []
	var data_dd = []
	var data_ee = []

	const llx = data.array_date.length - 1
	jQuery("#outarea_bb").text ("llx = " + llx)

	for (var it = 0; it < llx; it += 1)
		{
		labels.push(data.array_date[it])
		data_aa.push(data.array_aa[it])
		data_bb.push(data.array_bb[it])
		data_cc.push(data.array_cc[it])
		data_dd.push(data.array_dd[it])
		data_ee.push(data.array_ee[it])
		}

	graph_data = {}

	graph_data['labels'] = labels
	graph_data['datasets'] = []

	const unit_aa =	{ label: "国立感染症研究所",
		lineTension: 0, data : data_aa , borderColor: 'green'}

	const unit_bb =	{ label: "検疫所",
		lineTension: 0, data : data_bb, borderColor: 'blue' }

	const unit_cc =	{ label: "地方衛生研究所・保健所",
		lineTension: 0, data : data_cc ,borderColor: 'cyan'}

	const unit_dd =	{ label: "民間検査会社",
		lineTension: 0, data : data_dd, borderColor: 'yellow' }

	const unit_ee =	{ label: "大学",
		lineTension: 0, data : data_ee, borderColor: 'magenta' }

	graph_data['datasets'].push(unit_aa)
	graph_data['datasets'].push(unit_bb)
	graph_data['datasets'].push(unit_cc)
	graph_data['datasets'].push(unit_dd)
	graph_data['datasets'].push(unit_ee)

	return graph_data
}

// ----------------------------------------------------------------------
function prog01 (graph_data)
{
	var options = {}

	const config = {
		type: 'line',
		data: graph_data,
		options: options
		}

	const context = jQuery("#chart")
	const chart = new Chart(context,config)
}

// ----------------------------------------------------------------------

data_inspect.json の作り方

PDF データのダウンロード

wget https://www.mhlw.go.jp/content/10900000/000608163.pdf

corona_table_mar14.png

PDF を JSON に変換

pdf_inspect.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	pdf_inspect.py
#
#						Mar/14/2020
#
import	sys
import	json
import	pdftotext
#
# ------------------------------------------------------------------
sys.path.append('/var/www/data_base/common/python_common')
from file_io import file_write_proc
# ------------------------------------------------------------------
def append_proc(array_in,cols):
#	sys.stderr.write("*** check append_proc\n")
	icount = 0
	value = ""
	for col in cols:
		if 0 < icount:
			if col == "" or col == "" :
				value = col
			elif col.find("(") < 0:
				value = int(col.replace(",",""))
			else:
				value = col
			array_in.append(value)
		icount += 1
#
# ------------------------------------------------------------------
# [6-2]:
def pdf_to_array_s2_proc(res,line):
	cols= line.split ()
	if (5 < len (cols)):
		if 0 < cols[0].find("("):
			sys.stderr.write(cols[0] + "\n")
			res['array_date'].append(cols[0])
			sys.stderr.write("len = %d\n" % len(res['array_date']))
			append_proc(res['array_date'],cols)
		elif 0 < cols[0].find("国立感染症"):
			append_proc(res['array_aa'],cols)
		elif 0 < cols[0].find("検疫所"):
			append_proc(res['array_bb'],cols)
		elif 0 < cols[0].find("地方衛生研究所"):
			append_proc(res['array_cc'],cols)
		elif 0 < cols[0].find("民間検査会社"):
			append_proc(res['array_dd'],cols)
		elif 0 < cols[0].find("大学"):
			append_proc(res['array_ee'],cols)
		elif 0 < cols[0].find("医療機関"):
			append_proc(res['array_ff'],cols)
# ------------------------------------------------------------------
# [6]:
def pdf_to_array_proc (file_pdf):
#
	fp_in = open(file_pdf, "rb")
	pdf = pdftotext.PDF(fp_in)
	fp_in.close()
#
	res = {}
	for key in ['array_date','array_aa','array_bb','array_cc','array_dd', \
			'array_ee','array_ff']:
		res[key] = []
#
	lines = pdf[0].split("\n")
	for str in lines:
		line = str.rstrip()
		if (5 < len (line)):
			pdf_to_array_s2_proc(res,line)
#
	return	res
# ------------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
file_pdf = sys.argv[1]
res = pdf_to_array_proc (file_pdf)
for key in ['array_date','array_aa','array_bb','array_cc','array_dd', \
		'array_ee','array_ff']:
		print(res[key])
#
file_out = "data_inspect.json"
out_str = json.dumps(res)
file_write_proc(file_out,out_str)
#
sys.stderr.write ("*** 終了 ***\n")
# ------------------------------------------------------------------

実行コマンド

./pdf_inspect.py 000608163.pdf
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?