Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

高齢者等のワクチン1回目接種率

$
0
0
次のような表を作成する方法です。 入力データ   KOREI-kenbetsu-vaccination_data.json 高齢者等のワクチン総接種回数の表を作成 pref.json 都道府県別65歳以上の高齢者の数 2つの JSON を 都道府県をキーとしてマージします。 ./merge.js pref.json KOREI-kenbetsu-vaccination_data.json vaccine_merged.json merge.js #! /usr/bin/node // --------------------------------------------------------------- // merge.js // // Jun/01/2021 // --------------------------------------------------------------- 'use strict' var fs = require("fs") // --------------------------------------------------------------- function text_read_proc(file_in) { var dict_aa = new Object () const data = fs.readFileSync (file_in,'utf8') const lines_in = ("" + data).split ("\n") lines_in.forEach (function(line) { const separator_string = /\s+/ const rr = line.split (separator_string) const pref = rr[1] // if (1< pref.length) if (pref) { const number = rr[3] const removed = number.replace(/,/g, '') const pp = parseInt(removed,10) * 1000 dict_aa[pref] = {"over_65": pp} console.log(pref) } }) return dict_aa } // --------------------------------------------------------------- function json_write_proc(file_json,dict_aa) { const json_str = JSON.stringify(dict_aa) fs.writeFile (file_json,json_str,function (err) { if (err) { console.error ("Error on write: " + err) } else { console.log("File written.") console.error ("*** 終了 ***") } }) } // --------------------------------------------------------------- function merge_proc(dict_pref,dict_vaccine) { var dict_merged = new Object () for (var pref in dict_pref) { dict_merged[pref] = new Object () dict_merged[pref]["over_65"] = dict_pref[pref].over_65 dict_merged[pref]["code"] = dict_vaccine[pref].code dict_merged[pref]["1"] = dict_vaccine[pref]["1"] dict_merged[pref]["2"] = dict_vaccine[pref]["2"] dict_merged[pref]["3"] = dict_vaccine[pref]["3"] const code = dict_vaccine[pref]["code"] console.log(pref,code) } return dict_merged } // --------------------------------------------------------------- function array_to_dict_proc(array_vaccine) { var dict_vaccine = new Object () for (var it in array_vaccine) { const unit_aa = array_vaccine[it] if (unit_aa["0"]) { const code = unit_aa["0"].substring(0,2) if (isFinite(code)) { // console.log(unit_aa["0"],code) const pref = unit_aa["0"].substring(3,) if (1 < pref.length) { // console.log(unit_aa["0"],code,pref) dict_vaccine[pref] = new Object () dict_vaccine[pref]["code"] = code dict_vaccine[pref]["1"] = unit_aa["1"] dict_vaccine[pref]["2"] = unit_aa["2"] dict_vaccine[pref]["3"] = unit_aa["3"] } } } } return dict_vaccine } // --------------------------------------------------------------- console.error ("*** 開始 ***") // const file_pref=process.argv[2] const file_vaccine=process.argv[3] const file_out=process.argv[4] console.log (file_pref) if (fs.existsSync(file_pref)) { const str_pref = fs.readFileSync (file_pref,'utf8') const dict_pref = JSON.parse (str_pref) const str_vaccine = fs.readFileSync (file_vaccine,'utf8') const array_vaccine = JSON.parse (str_vaccine) const dict_vaccine = array_to_dict_proc(array_vaccine) const dict_merged = merge_proc(dict_pref,dict_vaccine) json_write_proc(file_out,dict_merged) } else { console.error ("*** error *** " + file_pref+ " doesn't exist. ***") } // --------------------------------------------------------------- Web ページ フォルダー構造 . ├── index.html ├── vaccine.css ├── vaccine.js └── vaccine_merged.json index.html <!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="vaccine.css"> <title>高齢者等のワクチン総接種回数</title> </head> <body> <blockquote> <h2>高齢者等のワクチン総接種回数</h2><p /> <blockquote> (2021年5月30日時点)<p /> </blockquote> </blockquote> <blockquote> <div class="contents"></div> </blockquote> </blockquote> <hr /> データソース<br /> <blockquote> <p><a href="https://www.kantei.go.jp/jp/headline/kansensho/vaccine.html"> 新型コロナワクチンについて</a></p> <p><a href="https://www8.cao.go.jp/kourei/whitepaper/w-2020/html/zenbun/s1_1_4.html"> 4 地域別にみた高齢化</a></p> </blockquote> <a href="../">Return</a><p /> Jun/01/2020 AM 08:00<p /> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="vaccine.js"></script> </body> </html> vaccine.js // ----------------------------------------------------------------------- // vaccine.js // // Jun/01/2021 // // ----------------------------------------------------------------------- jQuery (function () { jQuery("#outarea_aa").text ("*** vaccine *** start ***") // const file_in = "./KOREI-kenbetsu-vaccination_data.json" const file_in = "./vaccine_merged.json" jQuery.getJSON (file_in,function (dict_in) { const data_aa = sort_proc(dict_in) var str_out = "" str_out += "<table>" str_out += "<tr>" str_out += "<th>No</th>" str_out += "<th>都道府県名</th>" str_out += "<th>接種回数</th>" str_out += "<th>内1回目</th>" str_out += "<th>内2回目</th>" str_out += "<th>65歳以上高齢者</th>" str_out += "<th>1回目接種率</th>" str_out += "</tr>" var count = 1 for (var it in data_aa) { const unit_aa = data_aa[it] const pref = unit_aa.pref str_out += "<tr>" str_out += "<td>" + count + "</td>" str_out += "<td>" + pref + "</td>" str_out += "<td>" + unit_aa["1"] + "</td>" str_out += "<td>" + unit_aa["2"] + "</td>" str_out += "<td>" + unit_aa["3"] + "</td>" str_out += "<td>" + unit_aa.over_65 + "</td>" str_out += "<td>" + unit_aa.percent.toFixed(1) + "</td>" str_out += "</tr>" count += 1 } str_out += "</table>" jQuery(".contents").html (str_out) }) jQuery("#outarea_hh").text ("*** vaccine *** end ***") }) // ----------------------------------------------------------------------- function sort_proc(dict_in) { var data_out = [] for (var pref in dict_in) { var unit_aa = dict_in[pref] unit_aa["pref"] = pref const percent = unit_aa["2"] * 100.0 / unit_aa.over_65 unit_aa["percent"] = percent data_out.push(unit_aa) } data_out.sort (compare_by_key_proc) return data_out } // ----------------------------------------------------------------------- function compare_by_key_proc (left,right) { var aa = left["percent"] var bb = right["percent"] var rvalue = 0 if (aa < bb) { rvalue = 1 } else if (aa > bb) { rvalue = -1 } return rvalue } // ----------------------------------------------------------------------- vaccine.css /* -------------------------------------------------------------- */ /* vaccine.css May/07/2021 */ /* -------------------------------------------------------------- */ table.main,td,th { table-layout:fixed; border:1.5px #7e7e7e solid; border-collapse: collapse; height: 16px; } th { background: #c6c6c6; } /* -------------------------------------------------------------- */

Viewing all articles
Browse latest Browse all 8691

Trending Articles