p5.js 外部データの入力
CSVを読み込んで、イチローの打率の変遷をグラフにする
CSVファイル の所在
https://www.katsumotoy.com/tmp/ichiro.csv
wikipediaを元に作成 https://ja.wikipedia.org/wiki/イチロー
code: graph.js
let stats;
const graph = [];
function preload() {
//ファイルの読み込み
stats = loadTable('ichiro.csv');
}
function setup() {
createCanvas(400, 400);
const rowCount = stats.getRowCount();
for (let i = 0; i < rowCount; i++) {
graphi = stats.getNum(i, 20);
}
}
function draw() {
background(200);
//罫線
stroke(220);
line(20, 380, 380, 380);
for (let i = 0; i < graph.length; i++) {
const wx = map(i, 0, graph.length - 1, 20, 380);
line(wx, 20, wx, 380);
}
//グラフ
noFill();
stroke(100);
beginShape();
for (let j = 0; j < graph.length; j++) {
const x = map(j, 0, graph.length - 1, 20, 380);
const y = map(graphj, 0, 0.5, 380, 20);
vertex(x, y);
}
endShape();
}
自治体の緯度経度を点描して、アメリカの地図を書く
CSVデータの所在
https://github.com/processing/processing-docs/blob/master/content/gswp_2e/examples/12_Data/Ex_12_03/data/cities.csv
code: us_map.js
let cities;
function preload() {
cities = loadTable('cities.csv', 'header');
}
function setup() {
createCanvas(800, 800);
stroke(0, 100);
}
function draw() {
background(220);
for (let i = 0; i < cities.getRowCount(); i++) {
const latitude = cities.getNum(i, 'lat');
const longitude = cities.getNum(i, 'lng');
setXY(latitude, longitude);
}
}
function setXY(lat, lng){
const x = map(lng, -180, -60, 0, 800);
const y = map(lat, 130, 10, 0, 800);
point(x, y - 200);
}
参考文献
「Processingをはじめよう 第2版」
https://www.oreilly.co.jp/books/9784873117737/
ヒムカンパニー 「11_1:表データ p5.js JavaScript」
https://himco.jp/2019/02/28/11_1:表テーブル-p5-js-javascript/
p5.js Reference & Examples
https://p5js.org/reference
https://p5js.org/examples/
https://gyazo.com/73af86b6fc90d5b630a82027e32c20a5
#coding #design #p5.js