0
0

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 5 years have passed since last update.

レーダーチャート

Posted at

デモ:http://codepen.io/mo4_9/pen/dOXoPx

スクリーンショット 2016-11-18 21.47.51.png

const canvas = document.getElementById("radarChart");
const ctx = canvas.getContext("2d");

const SIZE = 320;
const MAX_LEVEL = 3;
const CENTER = SIZE / 2;
const STRIDE = CENTER / MAX_LEVEL;

const LEVELS = [3, 2, 1, 3];

const LINE_COLOR = "rgba(0,255,0,.8)";
const CHART_COLOR = "rgba(0,255,0,.6)";

canvas.width = canvas.height = SIZE;

// base
ctx.strokeStyle = LINE_COLOR;

for(let i = 1; i < MAX_LEVEL + 1; i++) {
  ctx.beginPath();
  ctx.moveTo(CENTER, (MAX_LEVEL-i) * STRIDE);
  ctx.lineTo(CENTER + STRIDE * i, CENTER);
  ctx.lineTo(CENTER, CENTER + STRIDE * i);
  ctx.lineTo((MAX_LEVEL-i) * STRIDE, CENTER);
  ctx.closePath();
  ctx.stroke();
}

ctx.beginPath();
ctx.moveTo(CENTER, 0);
ctx.lineTo(CENTER, SIZE);
ctx.closePath();
ctx.stroke();

ctx.beginPath();
ctx.moveTo(0, CENTER);
ctx.lineTo(SIZE, CENTER);
ctx.closePath();
ctx.stroke();

// chart
ctx.fillStyle = CHART_COLOR;
ctx.beginPath();
ctx.moveTo(CENTER, (MAX_LEVEL-LEVELS[0]) * STRIDE);
ctx.lineTo(CENTER + STRIDE * LEVELS[1], CENTER);
ctx.lineTo(CENTER, CENTER + STRIDE * LEVELS[2]);
ctx.lineTo((MAX_LEVEL-LEVELS[3]) * STRIDE, CENTER);
ctx.closePath();
ctx.fill();

細かいデザインの指定がない場合はChart.jsを使う

参考
チャート、グラフを書くのに良さそうなJavascriptライブラリ6選
↓円周上頂点の取得の勉強になりそう
http://codepen.io/thebabydino/pen/MbjZjw

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?