Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide the Circles into 24/7 equal parts using canvas?

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
    <title>Growing Circles</title>
  </head>
  <body>
    <canvas id="c" width="960" height="720"></canvas>
  </body>
</html>

JavaScript

var canvas = document.getElementById( "c" ),
    ctx = canvas.getContext( "2d" );

ctx.lineWidth = 3;

ctx.beginPath();
ctx.arc( 500, 350, 60, 0, 2 * Math.PI, false );
ctx.fillStyle = "#4DA54D";
ctx.fill();
ctx.strokeStyle = "DarkRed";
ctx.stroke();

ctx.beginPath();
ctx.arc( 500, 350, 120, 0, 2 * Math.PI, false );
ctx.strokeStyle = "OliveDrab";
ctx.stroke();

ctx.beginPath();
ctx.arc( 500, 350, 180, 0, 2 * Math.PI, false );
ctx.strokeStyle = "#530053";
ctx.stroke();

ctx.beginPath();
ctx.arc( 500, 350, 240, 0, 2 * Math.PI, false );
ctx.strokeStyle = "#208181";
ctx.stroke();

ctx.beginPath();
ctx.arc( 500, 350, 300, 0, 2 * Math.PI, false );
ctx.strokeStyle = "#CC7A00";
ctx.stroke();

ctx.beginPath();
ctx.arc( 500, 350, 360, 0, 2 * Math.PI, false );
ctx.strokeStyle = "#205450";
ctx.stroke();

I want to divide This demo into 24 hours timeline by using lines. I tried few but not up-to mark.It is going very big in code! Is there any other way to solve this in small piece of code like using slice as in Tried demo ?

Tried Demo

My Requirement is to do as This demo. I want to have This demo to be sliced 24/7 where 24 represent hours and 7 for days.

Further Requirement :: Even i must be able to access the particular arc which i want depending on the day and hour !

Finally i want to have a look like this Image I will pass the arguments(Day, hour,Color) and then that particular segment should change the color, as i shown in the Image.

This is how i tried to print the numbers .. function drawnumbers(){ for(var i=1; i< hours+1;i++){ context.font="18px sans-serif"; context.fillText(+i,20+i*30,20+i*30); } }but i want them on the outer circle as in png Image

like image 308
09Q71AO534 Avatar asked Aug 12 '13 13:08

09Q71AO534


2 Answers

Man, you are doing PROGRAMMING, so why are you afraid of using the tools? Use loops and variables.

Dial

var canvas = document.getElementById( "c" ),
ctx = canvas.getContext( "2d" ),
strokes = ["DarkRed", "OliveDrab", "#530053", "#208181", "#CC7A00", "#205450"];
ctx.lineWidth = 3;

for(var i=0; i<7; i++) {
  ctx.beginPath();
  ctx.arc( 500, 350, 420-60*i, 0, 2 * Math.PI, false );
  if(i==6) {
    ctx.fillStyle = "#4DA54D";
    ctx.fill();
  }
  ctx.strokeStyle = strokes[i];
  ctx.stroke();
}

// Now do the same for the 24 spokes (Use mathematical formulae)
// this is just from the top of my head, may need some adjustment
angle = 360/25; // you have 24 spokes, so 25 sectors (first and last one overlap or are the same)
for(var j=0; j<24; j++) {
  ctx.beginPath();
  ctx.moveTo(500, 350);
  ctx.lineTo(500+420*Math.sin(angle*j), 350-420*Math.cos(angle*j));
  ctx.strokeStyle = "blue";
  ctx.stroke();
}

Labels

For printing labels on the dial:

angle = 360/23;
for(var i=0; i<23; i++) {
  x = <centerX> + <radius> * Math.sin(angle*i*Math.PI/180) // convert from radian to angle
  y = <centerY> - <radius> * Math.cos(angle*i*Math.PI/180)
  context.fillText(i+1, x, y);
}
like image 151
kumarharsh Avatar answered Oct 26 '22 10:10

kumarharsh


Although Harsh has already provided a very useful answer, it relates to the wireframe drawing which you depicted.

I thought it would also be useful to show you how you could achieve the drawing of the individual segments.

I think you have asked for a little too much in your PNG as we would practically be creating your project for you, but with my answer and Harsh's answer I believe you can get what you want:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

// centre or center for US :) the drawing
var x = canvas.width / 2;
var y = canvas.height / 2;

// number of days
var days = 7;

// number of hours
var hours = 24;

// one segment represents an hour so divide degrees by hours
var segmentWidth = 360 / hours;

// begin at 0 and end at one segment width
var startAngle = 0;
var endAngle = segmentWidth;

// how thick you want a segment
var segmentDepth = 30;

function init() {
    for (var i = 1; i <= days; i++) {
        drawSegments(i * segmentDepth);
    }
}

function drawSegments(radius) {
    for (var i = 0; i < hours; i++) {
        context.beginPath();
        context.arc(x, y, radius, 
             (startAngle * Math.PI / 180), (endAngle * Math.PI / 180), false);
        context.lineWidth = segmentDepth;
        context.strokeStyle = '#' + 
             (Math.random() * 0xFFFFFF << 0).toString(16);
        context.stroke();

        // increase per segment        
        startAngle += segmentWidth;
        endAngle += segmentWidth;
    }
}

// start drawing our chart
init();

See my http://jsfiddle.net/U2tPJ/6/ demo.

like image 27
Neil Avatar answered Oct 26 '22 09:10

Neil