0

I am creating a report in which I want to show my data using a bar chart I went through some blogs, tutorials and a way to print; but, now my need is I want to show my data in percentages and print it, can anyone help me out?

Here is my code:

<style>
.clear{
    clear:both;
}

.graphcont {
    padding-top:10px;
    color:#000;
    font-weight:700;
    float:left
}

.graph {
    float:left;
    margin-top:10px;
    background-color:#cecece;
    position:relative;
    width:280px;
    padding:0
}

.graph .bar {
    display:block;
    position:relative;
    background-image:url(images/bargraph.gif);
    background-position:right center;
    background-repeat:repeat-x;
    border-right:#538e02 1px solid;
    text-align:center;
    color:#fff;
    height:25px;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    line-height:1.9em
}

.graph .bar span {
    position:absolute;
    left:1em
}
</style>

</head>

<body>

<div id="container">

<?php
require("includes/dbconnect.php");

$getcen  = mysql_query("SELECT count(*) AS totcen, center FROM `makhtab` WHERE center != '' GROUP BY center ORDER BY center") or die(mysql_error());

while ($mcent = mysql_fetch_array($getcen)) {  
     echo '<div class="rating"><div class="graphcont"><div class="graph"><strong class="bar" style="width:'.$mcent["totcen"].'%;">'.$mcent["totcen"].'</strong></div></div></div>
      <div class="clear"></div>';
}
?>
</div>

The above code works fine but I want to set my div in which the bar is shown to a fixed width and I want to manipulate the bar width according to percentage created by the query. Thanks in-advance.

5
  • Do you mean manipulate the height of the bar ? Also, what is the problem you are having with the above code ? Commented Mar 14, 2013 at 8:21
  • der isnt any problem sir the code is working fine i jus want to fix the width of the div in which the bar is appearing and jus want to manipulate the bar according to the data Commented Mar 14, 2013 at 8:25
  • i jus want to convert my data into percentage so that i can use it in my bar width Commented Mar 14, 2013 at 8:30
  • So, you want the percentage displayed as a bar, based on the container div's width ? Commented Mar 14, 2013 at 8:42
  • well the percentage is 100% but my output which is 'totmakh' is 266 so its showing 266% Commented Mar 14, 2013 at 9:43

3 Answers 3

2

My advice to you would be is use an external light weight framework to take care of charting , it simplifies a lot of things.

RazorFlow is really good , try it out.

Sign up to request clarification or add additional context in comments.

Comments

1

In order to correctly display the bar, you would need to calculate the value of the percentage that the 'totmakh' value represents.

First, you would need to know the maximum allowed value, which represents 100%, let's assume it's called MAX. Once you have defined this value, calculating the percentage is easy:

$percentage = $mcent["totmakh"] * 100 / MAX;

You can then use that value to set the percentage width of the div.

Comments

0

If you want to build a dashboard containing interactive charts and tables in PHP, and the Data in MySQL, you can check out RazorFlow PHP.

You will be able to create reports from SQL with the kind of chart that you want.

(Disclosure: I am the developer of RazorFlow and I am posting only because I feel it's relavant)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.