Monday, May 28, 2012

How to create Graphs using HTML5

To develop a small web application, I would like to use the data of students' grades and translate it into a graph bar.  But how can I do that?

With HTML5, using tag <canvas>, it is possible create a graph bar without third part software. The first thing to do is write some functions in JavaScript. Before you copy code, you have to understand the coordinates on Canvas. The next figure show how coordinates are designed. It is not the same of what we learned on Math classes.


JavaScript code:

<script type="text/javascript">


        function drawLine(context,startX,startY,endX,endY,color){
            context.beginPath();
            context.moveTo(startX,startY);
            context.lineTo(endX,endY);
            context.closePath();
            context.strokeStyle=color;
            context.stroke();
        }


        function drawRectangle(context,startX,startY,width,height,fill){
            context.beginPath();
            context.rect(startX,startY,width,height);
            context.closePath();
            context.stroke();
            if(fill) context.fill();
        }


        function clearCanvas(context){
            // Store the current transformation matrix
            context.save();
            // Use the identity matrix while clearing the canvas
            context.setTransform(1, 0, 0, 1, 0, 0);
            context.clearRect(0, 0, myCanvas.width, myCanvas.height);
            // Restore the transform
            context.restore();
        }


        function drawBarChart(context,data,startX,startY,endX,
endY,chartHeight,barWidth,markDataIncrementsIn,scale){
            //draw x-axis
            drawLine(context,startX,startY,endX,startY,"#000");
            //draw y-axis
            drawLine(context,startX,startY,startX,endY,"#000");


            var maxValue=0;
            for(var i=0;i<data.length;i++){
                //extract data
                var values = data[i].split(",");
                //column name
                var name=values[0];
                //column value
                var height=parseFloat(values[1])*scale;
                //find maximum value
                if(parseInt(height)>parseInt(maxValue)) maxValue=height;
                //write data to chart
                //fill rectangle with yellow color
                context.fillStyle="#ff0";
                drawRectangle(context,startX+(i*barWidth*2),(startY-height),barWidth,height,true);
                //add column title to x-axis
                context.textAlign="left";
                //text color is black
                context.fillStyle="#000";
                context.fillText(name,startX+(i*barWidth*2),(chartHeight-25),barWidth*2);
            }


            //add some data markers to y-axis
            var numMarkers = Math.ceil(maxValue/parseInt(markDataIncrementsIn));
            context.textAlign="right";
            context.fillStyle="#000";
            var markerValue=0;
            for(var i=0;i<=numMarkers;i++){
                context.fillText((markerValue/parseInt(scale)),(startX-5),(startY-markerValue),50);
                if(i==0){
                    drawLine(context,startX,(startY-markerValue),endX,(startY-markerValue),"#000");
                }else{
                    drawLine(context,startX,(startY-markerValue),endX,(startY-markerValue),"#00f");
                }
                markerValue+=parseInt(markDataIncrementsIn);
            }
        }


        function graph(scale){
            var s=0;
            var changed=false;
            document.getElementById("range").innerHTML=scale;
            if(scale==undefined){
                s=1;
                changed=false;
            }else{
                s=scale;
                changed=true;
            }
             
            var graphCanvas = document.getElementById("myCanvas");
            //ensure that element is available within DOM
            if(graphCanvas && graphCanvas.getContext){
                //open a 2d context within canvas
                var context = graphCanvas.getContext("2d");
                //bar chart data
                var data = new Array(5);
                data[0]="Bad,2";
                data[1]="Mediocre,0.45";
                data[2]="Sufficient,5.5";
                data[3]="Good,7.2";
                data[4]="Excelent,3.9";
                //draw bar chart
                if(!changed){
                    drawBarChart(context,data,35,560,750,50,myCanvas.height,30,s,s);
                }else{
                    //clear canvas to redrawing
                    clearCanvas(context);
                    //draw bar chart
                    drawBarChart(context,data,35,560,750,50,myCanvas.height,30,s,s);
                    
                }
            }
            return true;
        }
    </script>

The second thing to do is write some HTML tags.

<body onload="graph(1);">
        <div id="title">Grades</div>
        <table>
            <tr>
                <td><canvas id="myCanvas" width="800" height="600"></canvas><td>
            <td>
                <table>
                    <tr>
                        <td>Scale : <span id="range">1</span></td>
                    </tr>
                    <tr>
                        <td><input type="range" min="1" max="501" value="1" onchange="return graph(this.value)"/></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>


The third thing to do is write CSS.

<style type="text/css">
            #title{
                text-align: center;
            }
</style>

Now put together all three parts on a single page or you can create a file to JavaScript and another to CSS and then link them on HTML page. How you can do that? Copy the next code and you can change name files.

<head>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <script type="text/javascript" src="functions.js"></script>
</head>



Attention
All this code is working on Google Chrome. Also works on Safari and Opera. If you will use Internet Explorer, you will have problems. For example, you can not use slide bar and you have to type a value instead.

Tuesday, May 1, 2012

How to install Oracle Database Express Edition


I heard talk about technology Oracle PL/SQL but I never tried it. To study PL/SQL it is necessary install software Oracle Database 11g Express Edition. Hereafter you have 3 minutes video showing all necessary steps to installation correct.

Monday, April 9, 2012

What is Data Warehouse?

Definition #1
A data warehouse is a system that retrieves and consolidates data periodically from the source systems into a dimensional or normalized data store. 


Definition #2
A Data Warehouse is a database that is designed for facilitating querying and analysis. These database contain read-only data that can be queried and analysed.


Conclusion:
So what is difference between database and data warehouse?
Database is designed to record data and Data Warehouse is designed to analysis data.

Monday, April 2, 2012

The best way to planning software


I believe Use Case diagram, Entity-Relationship model and flowchart to design a software. It is extremely useful because I loose less time to program and correct the mistakes. I would rather planning during two months than programming during two months. Why? Because a software with good planning is cheap, fast and flexible unlike a software with bad planning that is expensive, slow and inflexible to possible changes.


All my academic life, I got used to program more hours than planning. My works were not awesome but also were not bad. It was enough to pass in evaluation. Finishing my course, I did an internship at the software company. Bringing old habits, I programmed more than planned. I had a project to finish in beginning of December but I finished in end of January. Why? Bad planning! My boss was angry with me for I did not meet deadline. I confess that is not good to see my boss angered!


I learned a lot with that mistake. From that, to develop any software, I always make Use Case diagram, Entity-Relationship model and flowchart. Use Case diagram is very abstract and easy to understand what the actor's goal. Entity-Relationship is used to define tables on database. Without this model, I would be lost. The flowchart is used to define software's logic.


With those three tools of planning, I spend less time to program, more time to planning, I am able to meet deadline and my boss will be happy with my work.

Wednesday, March 21, 2012

Communicating between PC and Arduino (Correction)


On March 19th, 2012, a youtube user called Sophie333C found a problem on video "Communicating between PC and Arduino".

Sophie333C's comment:
Hi, There seems to be a problem when you press send the second time around,
it only seems to update every second press of the button!

After two days, she sent me a solution. I tested her solution and it works fine. Now I can receive a answer
when I press the button for first time. Here is code of solution:

  answer = port_arduino.ReadByte()
        TextBox2.Text = Chr(answer)

I would like thank to Sophie333C to found out this problem.