tags:

views:

704

answers:

5

Hi,

I have seen a facebook application in which clicking on a radio button renders a graph.

As you can see below:

alt text

alt text

I wanted to know whether there is any similar graph library through which i can generate the same graph in php.

Thanks,

Pankaj

A: 

PChart looks promising.

This blog entry details 6 candidates and their pros/cons. Most of them have quite stylised rendering. A quick look suggests that ezComponent's bar chart is quite straightforward (in terms of presentation) and perhaps what you're looking for.

I think the unusual aspect of your request is the horizontal orientation of the bar chart, but given that most of the above libraries look powerful, that shouldn't be a headache.

Brian Agnew
A: 

See this related question and answers for Charting libs in PHP.

Also, check this article for various additional approaches to your problem.

My favorite would be not to use a Charting lib at all and use just plain CSS and HTML.

Gordon
+1  A: 

actually, generating this kind of graph is pretty easy - just use two DIVs - one is a 100% width bar, another DIV is in that first DIV making background colored % you require. you can even include text in that. easy, just plain HTML/CSS.

UPDATE:

HTML:

<div class="bar">
    <div class="percentage" style="width:66%">This is 66% wide div</div>
</div>

CSS:

.bar { width: 99%; border: 1px solid #000; }
.percentage { background: #000; }
dusoft
Hi,Can you please give me example code to generate css dynamically?
Pankaj Khurana
+1  A: 

You can do it with just CSS by setting the background-position of an background image of a span.

Let's say that the maximum width of the bar is 200 pixels. The HTML and CSS for this bar might look something like this:

<label>49%</label><span class="bar">Sweaters with leggings</span>

And CSS:

span.bar {
    background: url(bar_bg.png) 0 0 repeat-y;
    display: block;
    width: 200px;
    line-height: 20px;
}

Notice the background image? For that, you need a 400px * 1px sized image, where the leftmost 200 pixels is colored with the color you want the bars to be, the rest being white or transparent. If you set this background to the span with background position of 0 0, the bar will be 100% colored, and if you put background position of -200px 0, the bar will be blank. So, you'll just have to determine where to set your background position among these extremes for each of your values.

First you have to find the maximum of the values you want to represent. The maximum value has an background position of 0 0, whereas value of 0 has background position of -200px 0.

The PHP code to calculate and apply the background position goes like this:

$values = array(
            49 => 'Sweaters with leggings',
            37 => 'Scarves with everything',
            14 => 'Cute knit hats and gloves');

// Find the maximum percentage
$max = max(array_keys($values));

foreach($values as $percentage => $label) {
    // Calculate the position, maximum value gets position 0, smaller values approach 200
    $pos = 200 - ($percentage / $max) * 200;
    // Output the label that shows the percentage
    echo '<label>'.$percentage.'%</label>';
    // Output the span, apply style rule for the background position
    echo '<span class="bar" style="background-position: -'.$pos.'px 0;">'.$label.'</span>';
}

If you want to use different coloured bar for the highest value as in your example, you can just do another background image and apply a class to the span with the highest value:

echo '<span class="bar '.($value == $max ? 'bar_max' : '').'" style="background-position: -'.$pos.'px 0;">'.$value.'</span>';

And in CSS:

span.bar.bar_max {
    background: url(bar_max_bg.png) 0 0 repeat-y;
}

And that's it, no need for extra DIV's or external libraries, just two simple background images. If you're handy, you can even combine the two images into one. Please let me know if this is helpful, and also if I didn't explain something clear enough.

Tatu Ulmanen
Hi,I don't want to give background image then how can i accomplish same effect.
Pankaj Khurana
Then it's harder to do and I'd suggest looking into what others have answered so far, the method can be found there. And why don't you want to use background image? It's the simplest, cleanest and easiest solution available.
Tatu Ulmanen
look my method below, no background image is required - what for, when you can have just `background-color` and `width` used.
dusoft
ok then from where i can get bar_bg.png and bar_max_bg.png?
Pankaj Khurana
@dusoft, your method requires extra markup, so that's not optimal either. Problems arise when the text inside the inner div is wider than the bar width etc (these can be fixed but requires still more markup). @Pankaj Khurana, you create those images yourself like I explained in my post.
Tatu Ulmanen
@Pankaj You should have enough information by now to get this working. If the given answers do not meet your requirements, please edit your question and be more specific about what you are looking for. Don't expect us to spoonfeed you code and/or graphics though.
Gordon
A: 

PChart is easy to implement and quite scalable as well. Try the link below.

http://www.clickoffline.com/2008/12/creating-a-simple-bar-chart-using-pchart-library-in-php/

Krishna Shasankar