HTML5 charting Gauge by Canvas and JavaScript

View: 3919    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: References   Fields: Other

9 point/7 review File has been tested

In today's article, I'll show you how to create charts using HTML5 Gauge Canvas conjunction with JavaScript. Graph Gauge will automatically refresh every 2 seconds and the number increases / decreases will be illustrated in the chart.

Introduction

In today's article, I'll show you how to create charts using HTML5 Gauge Canvas conjunction with JavaScript. Graph Gauge will automatically refresh every 2 seconds and the number increases / decreases will be illustrated in the chart.

HTML

To create the chart, we just declare a canvas element as follows:

1
<canvas id="canvas" width="300" height="300">

CSS

Then we just simple chart format as follows:

1
2
3
4
5
6
/*Centering the gauge*/
#canvas {
    display: block;
    width: 300px;
    margin: 100px auto;
}

JavaScript

This is the important part, and is where you should spend the most interest. Read carefully all annotations on each line to better understand how to create and run the chart:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<script language="JavaScript">
window.onload = function(){
    //canvas initialization
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    //dimensions
    var W = canvas.width;
    var H = canvas.height;
    //Variables
    var degrees = 0;
    var new_degrees = 0;
    var difference = 0;
    var color = "lightgreen"; //green looks better to me
    var bgcolor = "#222";
    var text;
    var animation_loop, redraw_loop;
     
    function init()
    {
        //Clear the canvas everytime a chart is drawn
        ctx.clearRect(0, 0, W, H);
         
        //Background 360 degree arc
        ctx.beginPath();
        ctx.strokeStyle = bgcolor;
        ctx.lineWidth = 30;
        ctx.arc(W/2, H/2, 100, 0, Math.PI*2, false); //you can see the arc now
        ctx.stroke();
         
        //gauge will be a simple arc
        //Angle in radians = angle in degrees * PI / 180
        var radians = degrees * Math.PI / 180;
        ctx.beginPath();
        ctx.strokeStyle = color;
        ctx.lineWidth = 30;
        //The arc starts from the rightmost end. If we deduct 90 degrees from the angles
        //the arc will start from the topmost end
        ctx.arc(W/2, H/2, 100, 0 - 90*Math.PI/180, radians - 90*Math.PI/180, false);
        //you can see the arc now
        ctx.stroke();
         
        //Lets add the text
        ctx.fillStyle = color;
        ctx.font = "50px bebas";
        text = Math.floor(degrees/360*100) + "%";
        //Lets center the text
        //deducting half of text width from position x
        text_width = ctx.measureText(text).width;
        //adding manual value to position y since the height of the text cannot
        //be measured easily. There are hacks but we will keep it manual for now.
        ctx.fillText(text, W/2 - text_width/2, H/2 + 15);
    }
     
    function draw()
    {
        //Cancel any movement animation if a new chart is requested
        if(typeof animation_loop != undefined) clearInterval(animation_loop);
         
        //random degree from 0 to 360
        new_degrees = Math.round(Math.random()*360);
        difference = new_degrees - degrees;
        //This will animate the gauge to new positions
        //The animation will take 1 second
        //time for each frame is 1sec / difference in degrees
        animation_loop = setInterval(animate_to, 1000/difference);
    }
     
    //function to make the chart move to new degrees
    function animate_to()
    {
        //clear animation loop if degrees reaches to new_degrees
        if(degrees == new_degrees)
            clearInterval(animation_loop);
         
        if(degrees < new_degrees)
            degrees++;
        else
            degrees--;
             
        init();
    }
     
    //Lets add some animation for fun
    draw();
    redraw_loop = setInterval(draw, 2000); //Draw a new chart every 2 seconds  
}
</script>

It's done, depending on the purpose and application that you build, may revise at the javascript above. I hope that through this article, you will better understand how to use Javascript to create the chart.

Good luck!

 

HTML5 charting Gauge by Canvas and JavaScript

HTML5 charting Gauge by Canvas and JavaScript Posted on 31-07-2015  In today's article, I'll show you how to create charts using HTML5 Gauge Canvas conjunction with JavaScript. Graph Gauge will automatically refresh every 2 seconds and the number increases / decreases will be illustrated in the chart. 1.2857142857143/10 3919

Comment:

To comment you must be logged in members.

Files with category

  • Instructions for installing and configuring Windows Live Mail on Windows 10 simple

    View: 2899    Download: 0   Comment: 0   Author: none  

    Instructions for installing and configuring Windows Live Mail on Windows 10 simple

    Category: References
    Fields: Other

    0/4 review
    Windows 10 pre-installed Mail application for managing personal email on the device. The latest version of this application but improved a lot and support other email accounts such as Gmail or Yahoo Mail but users almost no "salty" really with this...

  • Step by step instructions to create social networking buttons with CSS3

    View: 2006    Download: 0   Comment: 0   Author: none  

    Step by step instructions to create social networking buttons with CSS3

    Category: References
    Fields: Other

    0.64285714285714/7 review
    CSS3 help web designers have the ability to create buttons (buttons) very nice, today I will share with you two tips decoration for button in your web becomes radiant beauty that no need to use photoshop.

  • You should understand the CSS Box Shadow

    View: 2139    Download: 0   Comment: 0   Author: none  

    You should understand the CSS Box Shadow

    Category: References
    Fields: Other

    1.2083333333333/12 review
    Effects his shadow was introduced in the previous article, however, in this article, I will guide more specific about shadow attributes, you try it with anything. First we try to learn css following paragraph:

  • How to do button movement with beautiful CSS3

    View: 1828    Download: 0   Comment: 0   Author: none  

    How to do button movement with beautiful CSS3

    Category: References
    Fields: Other

    0/5 review
    Previously to create motion effects button, you must draw the button in photoshop, then switched to flash to make it possible to move. But now you need not spend too much time doing it, all because of the emergence of CSS3.

  • Create a dropdown menu with CSS3 effects with beautiful transtions

    View: 2108    Download: 0   Comment: 0   Author: none  

    Create a dropdown menu with CSS3 effects with beautiful transtions

    Category: References
    Fields: Other

    0.40909090909091/11 review
    More of the dropdown menu you want to introduce to you, this will have the effect menu transitions (converted background of menu background from pale to dark). This menu works well on browsers like FF, Chrome, Safary, but unfortunately did not work...

  • How to hack css for all browsers you should know

    View: 1733    Download: 0   Comment: 0   Author: none  

    How to hack css for all browsers you should know

    Category: References
    Fields: Other

    1.5/9 review
    In the process of web design, which makes it the most headaches is how to design web pages that you can run on the browser. Especially when clients use the old as IE6 browser such. However, with this article, you will easily edit interface runs well...

  • Create Accordion with extremely beautiful CSS3

    View: 2163    Download: 0   Comment: 0   Author: none  

    Create Accordion with extremely beautiful CSS3

    Category: References
    Fields: Other

    0.75/6 review
    Accordion is a technique that helps you shortened the content that you want displayed on the website. It is often used for the display of submenus. In addition you can also use it for content, images. Before applying this procedure, you have to use...

  • Create the effect Hovercho images with Webkit and CSS3 is great

    View: 1850    Download: 0   Comment: 0   Author: none  

    Create the effect Hovercho images with Webkit and CSS3 is great

    Category: References
    Fields: Other

    0.9/5 review
    WebKit is one open source application framework provides the necessary components built one for your web browser. Along with CSS3, cả have contributed to very nice effect những previously only using JavaScript or JQuery thực beige.​

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd