views:

633

answers:

2

I have downloaded the Flot library and have been having a play around.

I understand how the Flot library works and how to draw simple graphs. The bit I am struggling with is pulling data from a DB and passing that into a Flot Graph so that the graph then becomes dynamic depending on the data retrieved.

I believe you can pass data from a DB into a Flot Graph using a JSON function, but I don't know where to start, could anyone help me please? Any sample code or advice would be much appreciated

I am using classic ASP to pull the data from the DB.

+2  A: 

There are several things you have to look at. First JSON. You need to know the structure of your JSON string. For instance you can create a JSON string like this:

 {"name":"Tom", "sales":200}

Or if it is many names and sales then use an array like this:

{"names":["Tom", "Harry"], "sales":[200, 100]}

Then to access these values in JavScript...suppose you had the JSON object like this:

var myJSON = {"names":["Tom", "Harry"], "sales":[200, 100]};
document.write(myJSON.names[0]); //print out Tom
document.write(myJSON.sales[0]); //200

Since you want to use AJAX to get this JSON object then you can create an ASP page that simply returns a JSON String in the right format. For example:

 Set rs = Conn.Execute("SELECT name, sales FROM employees")
 If Not rs.EOF Then
  Response.Write "{'names':["   
  Do Until rs.EOF
    Response.Write rs("name")
    Response.Write ","
    rs.MoveNext
  Loop
  rs.Close : Set rs = Nothing : Conn.Close : Set Conn = Nothing 
  Response.Write "]}"
%>

Disclaimer: This is not tested code, I have not written classic ASP in almost 10 years! For instance, make sure that a final , is not printed etc.

Now, this link has some examples of loading data with AJAX and updating the graph. So you have to modify the URL to point to your asp page that generates the JSON string and modify the onDataRecieved function to add the data according to your own structure.

I hope this helps a little.

Vincent Ramdhanie
+3  A: 

I am not sure if by dynamic you mean "updated in real-time" (AJAX). If this is not the case, then the following example should help you getting started with flot and classic ASP:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
    <title>Example</title>
    <!--[if IE]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
    <script language="javascript" type="text/javascript" src="jquery.js"></script>
    <script language="javascript" type="text/javascript" src="jquery.flot.js"></script>
</head>

<body>

    <div id="placeholder" style="width:600px;height:300px;"></div>

    <script id="source" language="javascript" type="text/javascript">
    $(function () {
        var d1 = [];

        <%
            SET ac = Server.CreateObject("ADODB.Connection") 

            ac.Open "driver={sql server}; server=localhost; database=db_name; uid=user; pwd=secret" 

            SET rs = Server.CreateObject("ADODB.Recordset") 
            SET rs = ac.Execute("SELECT data_x, data_y FROM data_log") 

            FOR EACH field IN rs.Fields    
               Response.Write "d1.push([" & field.data_x & ", " & field.data_y & "]);"    
            NEXT
        %>

        $.plot($("#placeholder"), [d1]);
    });
    </script>

</body>
</html>
Daniel Vassallo