Integrate Flotr2 charts Dynamically from DB

2.4k Views Asked by At

I am new to this flotr and beginner of java. How can i create a flotr chart that retervie data from data base. i can create chart by static values but using database how can i create flotr charts. please give me some Example with database . so that i can work for all the othe charts.please help guide me the process to retervie data ffrom database into flotr charts using java or jsp.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> Line Chart</title>

  <style>
  body {
        margin: 20px;
        padding:10px;

      }
      #example {
    width: 500px;
    height: 300px;
}
  </style>
 </head>

 <body>
<div id="example"></div>
  <script type="text/javascript" src="./js/flotr2.min.js"></script>

    <script type="text/javascript">
(function basic(container) {

  var
    d1 = [[0, 3], [4, 8], [8, 5], [9, 13]], // First data series

      graph;
   // Draw Graph
        graph = Flotr.draw(container, [ d1],{
        lines: {show: true, fill: true,color: 'yellow'},
        points: {show: true,radius: 3,fillColor:'yellow'},
        title: "Line Chart",
        subtitle: "Employee"}

  );
})(document.getElementById("example"));
    </script>
 </body>
</html>
1

There are 1 best solutions below

0
Pompey On

Well I'm not sure what you mean by Java...do you mean Javascript? Because Flotr2 works on web-based platforms. But to integrate a database into making a chart with Flotr2 is actually really easy.

I use php, but you could use asp or jsp as well for this. Basically what you need to do is write some php code that will access the database and then just echo out the javascript code with the php variables. Like so:

<script type="text/javascript">
(function (){
<?php
$mysqli = new mysqli("host", "user", "password", "database");
if(!($stmt = $mysqli->prepare("SELECT attribute FROM table WHERE condition"))){
     echo "Prepare Failed: (" . $mysqliprivate->errno . ") " . $mysqliprivate->error;
}
else{
     $stmt->bind_param("s", $condition);
     $stmt->execute();
     $stmt->bind_result("$result");
     echo "var dataset = [";
     $i = 0;
     while($stmt11->fetch()){
     echo " [" . $i . ", " . $result. "]";
     echo ",";
     $i++;
}
echo "];";
$stmt->close();
?>

And then in your javascript code

graph = Flotr.draw(container, [ d1],{

instead of d1, replace that with dataset the variable you declared back at your php code