Using PHP and CSS to make a simple graph

Using PHP and CSS to make a simple graph

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 28, 2009

phpgraph

Here’s an easy way to display a simple percentage graph using PHP to work out the percentages and do the maths and CSS to display the data on our page. Here’s the demo.

First you need to copy the CSS below into the head of your page, you can style this to suit your needs once we have the PHP in place to render our graph.

.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
}

I used this graph on a project that required users to rate a video. They could give the video a plus or a minus rating, the values are then converted into percentages, the plus percentage is then put into the CSS width of the div, creating the bar graph, in my previous project I had two graphs to represent how many plus votes and how many minus votes were cast, this is just a simple example of how to create a dynamic graph with PHP.


$plus = 138;
$minus = 59;

$totalnumber = $plus + $minus;

$pluspercent = round(($plus / $totalnumber) * 100);
$minuspercent = round(($minus / $totalnumber) * 100);

$total = round($plus + $minus);
$totalVotes += $total;

     echo '<div class="rating"><div class="graphcont">The average rating of '.$pluspercent.'% is based on '.$totalVotes.' votes<div class="graph"><strong class="bar" style="width:'.$pluspercent.'%;">'.$pluspercent.'%</strong></div></div>

  </div>

<div class="clear"></div>
';

demodemo


Recent shares

More tutorials from Papermashup
Comments
19 discussions around Using PHP and CSS to make a simple graph
  1. boris says:

    thanks dude, i know the post is old but this piece of code is exactly what i was looking for! such simplicity and such a perfect result!

  2. Pingback: 16 Usable CSS Graph and Bar Chart Tutorials and Techniques « RPL Class

  3. Pingback: easypoll a side project | Papermashup.com

  4. Pingback: 185+ Very Useful and Categorized CSS Tools, Tutorials, Cheat Sheets | tripwire magazine

  5. Brandon Goodin says:

    I’ve been looking for something like this for the past couple of weeks. Here at work, we’re installing a 42″ tv to display current production reports. This code was exactly what I was looking for. Thank you for sharing!

  6. Pingback: 155+ Mega CSS Roundup of Tools, Tutorials, Cheat Sheets etc. | tripwire magazine

  7. Pingback: 75+ Tools for Visualizing your Data, CSS, Flash, jQuery, PHP | Afif Fattouh - Web Specialist

  8. Pingback: 70 + Tools for Visualizing your Data, CSS, Flash, jQuery, PHP | tripwire magazine

  9. Pingback: Are you want to create CSS Bar Graphs and Star Rater?

  10. Paul Bremer says:

    I used this method in my school project, you can see it @ http://bit.ly/11Aa8s

  11. Pingback: tripwire magazine | tripwire magazine

  12. Pingback: 50+ Essential Techniques and Tools for Visualizing your Data | tripwire magazine

  13. Pingback: 16 Usable CSS Graph and Bar Chart Tutorials and Techniques - Code Index

  14. AzeriFire says:

    Bookmarked! Thank you!

  15. Pingback: 16 Usable CSS Graph and Bar Chart Tutorials and Techniques « All things beautiful

  16. Pingback: 16 Usable CSS Graph and Bar Chart Tutorials and Techniques | guidesigner.com

  17. Pingback: CSSでさくっと作れるグラフいろいろ - IDEA*IDEA ~ 百式管理人のライフハックブログ ~

  18. Pingback: 126 Usable CSS Graph and Bar Chart Tutorials and Techniques : Speckyboy - Web Design, Web Development and Graphic Design Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.