Solved

Adding Chart to Web Page

Posted on 2012-12-29
11
285 Views
Last Modified: 2013-01-22
We have a MYSQL/PHP database and want to add a chart to the web[age that shows the total amount of exercise per day over a period of time. Is there a nice chart plugin we could use for this?

The fields in the table are called exercise_id, exercise_time and exercise_timestamp.

Thanks for the help,

D
0
Comment
Question by:TLN_CANADA
11 Comments
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38730179
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38730708
I've used JPGraph with very good success. http://jpgraph.net/
0
 
LVL 9

Expert Comment

by:abolinhas
ID: 38731117
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 38756021
Google charts is very easy to work with and produces customizable charts that blend seamlessly with your existing pages.
I pull data from SQL server, populate into an array and send the array to a Google API to produce this chart:
Screen shot of chart
0
 

Author Comment

by:TLN_CANADA
ID: 38757795
Thanks everyone,

I'm looking to make a simple chart that shows a person's exercise practice over time. User's can track their exercise times in minutes in the database. I would like to setup a simple line chart that shows on the vertical axis the minutes they exercised and on the horizontal axis the month of the year. If the chart could automatically resize to the amount of user data in the database that would be ideal. (E.g. if they have only exercised for one month it would show each day in details whereas if they have a year's worth of data it would show the data in months on the horizontal access). Could anyone provide a simple example of this? I have tried to do this myself using 3 of the programs suggest above (Float Chart, Google Charts and PHPChart) but as I'm not a seasoned PHP developer I'm finding it difficult to set it up.

Thanks for your help,

D
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:TLN_CANADA
ID: 38757798
If it helps, our database table is called exercise_table and the fields are:

exercise_id (ai)
exercise_duration (int in minutes)
exercise_timestamp
user_id
0
 

Author Comment

by:TLN_CANADA
ID: 38757801
I can get all the necessary information from the database, I just can't figure out how to link it with the charts and the best chart system to use so that the view is automatically adjusted based on the amount of content for that user.
0
 
LVL 7

Accepted Solution

by:
dimmergeek earned 500 total points
ID: 38774362
Here is the classic ASP page I used to generate the line chart above with GOogle API.  ALl you need to do is get the data from your database, populate your chart fields into an array and call the API.

<body>
    <%
        dim fso, rsCount, rsTally, i, arrSize, arrDate(), arrCount(), arrTally(), arrPercent(), chartData
        
        ' Create the ADO connection object
        set fso = CreateObject("ADODB.Connection")
        
        ' Create the recordsets
        set rsCount = Server.CreateObject("ADODB.Recordset")
        set rsTally = Server.CreateObject("ADODB.Recordset")
        
        ' Open the database
        fso.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("../../../../BR/LEAN/personalizationMetrics.mdb") & ";Persist Security Info=False;"
        
        mSQL = "SELECT entryDate, SUM([qty]) AS [SUM OF KEYPADS] FROM dataTable WHERE DateDiff('d', entryDate, Now()) <=30 AND (reqType = 0 OR reqType = 1) GROUP BY entryDate ORDER BY entryDate ASC;"
        rsCount.Open mSQL, fso, 1, 1
        arrSize = rsCount.RecordCount
        rsCount.MoveFirst
        ReDim arrDate(arrSize)
        ReDim arrCount(arrSize)
        ReDim arrTally(arrSize)
        ReDim arrPercent(arrSize)
        ctr=0
        Do While Not rsCount.eof
            If Not rsCount.eof Then
                arrDate(ctr) = rsCount("entryDate")
                arrCount(ctr) = rsCount("SUM OF KEYPADS")
                ctr = ctr+1
            End If
            rsCount.MoveNext
        Loop
        
        mSQL = "SELECT entryDate, SUM([qty]) AS [SUM OF KEYPADS] FROM dataTable WHERE DateDiff('d', entryDate, Now()) <=30 AND (reqType = 0 OR reqType = 1) AND velocity = 0 GROUP BY entryDate ORDER BY entryDate ASC;"
        rsTally.Open mSQL, fso, 1, 1
        arrSize = rsTally.RecordCount
        rsTally.MoveFirst
        ctr=0
        Do While Not rsTally.eof
            If Not rsTally.eof Then
                arrTally(ctr) = rsTally("SUM OF KEYPADS")
                'Response.Write(ctr & ") " & arrDate(ctr) & ", " & arrCount(ctr) & ", " & arrTally(ctr) & "<br />")
                ctr= ctr+1
            End If
            rsTally.MoveNext
        Loop
        
        'Response.Write("<br /><br /><br />")
        
        ctr=0
        Do While ctr < arrSize
            arrPercent(ctr) = FormatNumber(eval((arrTally(ctr)/arrCount(ctr))*100),0)
            'Response.Write(ctr & ") " & arrDate(ctr) & ", " & arrPercent(ctr) & "<br />")
            chartData = chartData & ",['"&arrDate(ctr)&"', 100, "&arrPercent(ctr)&"]"
            ctr= ctr+1
        Loop
    %>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] }); google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = google.visualization.arrayToDataTable([
            ['Date', 'Goal', 'Actual']
            <%=chartData%>
            ]);
            var options = {colors: ['#A90000','#028837'], title: 'Percentage of Orders Entered within 24 Hours', curveType: 'function', titleTextStyle: {fontSize: 24}, backgroundColor: '#F0F0F0', legend: 'bottom', hAxis: {slantedTextAngle: 90}, vAxis: {viewWindowMode:'explicit', viewWindow:{ max:110, min:0}}};
            var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
            chart.draw(data,options);
        }
    </script>
    
    <!--Div that will hold the chart-->    
    <center>
        <div id="chart_div" style="width: 1000px; height: 600px;"></div>
    </center>
</body>

Open in new window

0
 

Author Comment

by:TLN_CANADA
ID: 38799698
Hi dimmergeek, we are using PHP/MYSQL. Will this work in the example you presented also?
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 38801174
Yes, the data source does not matter.  Once you have your data, you just build an array table with it and then call the API.
0
 
LVL 7

Expert Comment

by:dimmergeek
ID: 38805552
Thanks for the points and good luck!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now