Dynamic Chart Using PHP-Xampp Mysqli database and visualizing line chart.

Objective of this blog:
  • To build a simple line graph using the values fetched from MYSQL-Xampp database table.

Why I created ?
I have created a database named as chart with table name as analysis with 6 columns as name (varchar 30),a1(int ),a2(int ),a3(int ),a4(int ),a4(int ),a5 (int )
Database
Caption:Database



1) Develop a php program as linep.php


<?php
$con=mysqli_connect("localhost","root","","chart");
//connection
if($con)
{
 $query = "SELECT * from analysis";
//select the records from table
 $result = mysqli_query($con,$query);
//initializing variables
 $a1=0;
 $a2=0;
 $a3=0;
 $a4=0;
 $a5=0;
 $count1=0;
//counting the number of students
 while($row=mysqli_fetch_assoc($result))
    {
$count1+=1;
//adding total marks obtained
$a1+= $row['a1'];
   
    $a2+= $row['a2'];
$a3+= $row['a3'];
$a4+= $row['a4'];
$a5+= $row['a5'];
}
$count=$count1*2;
//Here my question is 2 mark for correct answer so multiplied by 2
//percentage= total marks obtained by each student/total number of full marks obtained by every student
 $a1=($a1/$count)*100;
 $a2=($a2/$count)*100;
 $a3=($a3/$count)*100;
 $a4=($a4/$count)*100;
 $a5=($a5/$count)*100;
 ?>
<html>
  <head>
//code taken from google chart
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
var a=<?php echo $a1?>;
var b=<?php echo $a2?>;
var c=<?php echo $a3?>;
var d=<?php echo $a4?>;
var e=<?php echo $a5?>;
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
         
['Percentage', 'Percentage of Marks Obtained for the questions'],
          ['Q1',  a  ],
          ['Q2',  b   ],
          ['Q3',  c  ],
          ['Q4',  d  ],
          ['Q5',  e  ]

        ]);
        var options = {
          title: 'Percentage of Marks Obtained for the questions',
          curveType: 'function',
          legend: { position: 'bottom' }
        };
        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="curve_chart" style="width: 900px; height: 500px"></div>
  </body>
</html>

<?php
}
else
{
die("could not connect");
	//echo("not connect")
}
?> 
Chart
Fig:Line chart

Comments