Jump to content

Making a Speedometer!?


strberg

Recommended Posts

Hello!

 

Allright so i want taken upon a task were i should make a gauge. But i don't know how. I've been googleing alot about this and i found this:

 

http://jpgraph.net/download/manuals/chunkhtml/ch20s02.html

 

But for my it only says how to make it. Which is a great start but i want the gauge to look into a database to see what precent the meter should point on.

Example:

 

The question is, Do you watch the olympics?

Two answers, Yes and No.

 

The database saves these and when you press View results, the speedometers arrow should point at, lets say 60%. The you know that 60% watches the olympics and 40% doesn't. (it is a half circle)

If you have any better guides than the one i linked please enlighten me.

 

If you haven't notice i'm new to PHP so use your easy words.

Link to comment
Share on other sites

Rather than have your gauge look into the database so that it performs only a single function it is much better to have a gauge that just lets you pass a percentage figure and draw itself accordingly.

 

That way it becomes reusable on many other occasions. Query the database to get the percentage.

 

Another approach would be a basic gauge class which you can then subclass for your olympics question.

Link to comment
Share on other sites

Create a php script to draw your gauge using the GD library, say "gauge.php". This would expect the percentage to be passed to it so it can draw the pointer in the right position.

 

guage.php

$percent = $_GET['percent'];         // get the percentage to be displayed

// draw gauge and output

 

Query the database and calc the percentage to be displayed, say $pcent.

 

Display the gauge using an img tag

 

<img src='gauge.php?percent=$pcent' />

 

You can repeat this for as many questions as you like.

 

 

Link to comment
Share on other sites

There are many tutorials on how to use GD with PHP to create custom images. Chances are no one here is going to write another one just for your issue.

 

If you can be more specific as to what you're having problems with, we might be able to help. As of now, the questions you're asking are too generic.

 

Are you having problems getting the percentage from the database? Once you have that number, the class you listed should do the rest for you quite easily.

 

$odo->needle->Set($percent);

Link to comment
Share on other sites

Here's an example using one of my image php files to draw a bar graph (you would substitute your gauge image).

 

I am attaching the code for img_bar.php to get you started.

 

<style type='text/css'>
th {background-color: #369; color:#FFF; font-family: arial; font-size: 10pt;}
td {background-color: #ccc; color:#000; font-family: arial; font-size: 9pt;}
</style>
<?php
/***
* Suppose you had these questions and reults
*/
$questions = array (
     array (
        'q' => 'Do you watch the Olympic Games?',
        'pcent' => 70
        ),
        
     array (
        'q' => 'Do you normally watch sport?',
        'pcent' => 55
        ),
        
     array (
        'q'  => 'Do you watch soaps?',
        'pcent' => 80
        )
);

/***
* and you want to display them
*/

echo "<table border='0' cellpadding='4' cellspacing='2'><tr><th>Question</th><th colspan='2'>Percent</th>";
foreach ($questions as $qarr) {
    echo "<tr><td>{$qarr['q']}</td>
        <td>{$qarr['pcent']}</td>
        <td><img src='img_bar.php?val={$qarr['pcent']}&max=100' /></td></tr>";
}
echo "</table>";
?>

post-3113-13482403686038_thumb.png

18829_.php

Link to comment
Share on other sites

That would depend on your database.

 

Assuming you have a table of questions and a table of responses

 

question -> qid, question_text

response-> rid, userid, qid, answer

 

SELECT q.qid, q.question_text, COUNT(*) as total, SUM(IF(r.answer='yes', 1, 0) as yes
FROM response r
INNER JOIN question q USING (qid)
GROUP BY qid

Link to comment
Share on other sites

The value is decided here right?

 

$questions = array (
     array (
        'q' => 'Do you watch the Olympic Games?',
        'pcent' => 70
        ),
        
     array (
        'q' => 'Do you normally watch sport?',
        'pcent' => 90
        ),
        
     array (
        'q'  => 'Do you watch soaps?',
        'pcent' => 80
        )

 

How do i get so the "pcent" is decided by the code you replied with for getting the database value?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.