I have hit a snag when I got to the point where I draw a bar graph.
I want to have a bar graph based on the database records, but I have no idea on how to do it.
I tried searching online, but to no avail. I only found a code that is static; the one below;

    # ------- The graph values in the form of associative array
    $sql="SELECT * FROM marks WHERE school_id='14'";
    $row = mysql_fetch_array($result);

    $subject = $row['subject_name'];
    $mark = $row['subject_score'];

    $values=array($subject => $mark);


    # ---- Find the size of graph by substracting the size of borders
    $graph_width=$img_width - $margins * 2;
    $graph_height=$img_height - $margins * 2; 

    $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);

    # -------  Define Colors ----------------

    # ------ Create the border around the graph ------


    # ------- Max value is required to adjust the scale -------
    $ratio= $graph_height/$max_value;

    # -------- Create scale and draw horizontal lines  --------

        $y=$img_height - $margins - $horizontal_gap * $i ;
        $v=intval($horizontal_gap * $i /$ratio);


    # ----------- Draw the bars here ------
    for($i=0;$i< $total_bars; $i++){ 
        # ------ Extract key and value pair from the current pointer position
        $x1= $margins + $gap + $i * ($gap+$bar_width) ;
        $x2= $x1 + $bar_width; 
        $y1=$margins +$graph_height- intval($value * $ratio) ;


Only problem is that this only returns one subject on the graph.

Recommended Answers

All 6 Replies

Member Avatar

There are loads of javascript/jQuery ones out there. You could pass the DB data to them as json

you could try highcharts, it is very easy to set it up.

any links to the "javascripts"?? or "highcharts"??

Member Avatar - Google not working? :)

Lemme check it out. Thanx.

Another one is jQplot, open source and free for commercial use. I quite liked it.

Simple and effective bar chart can also be accomplished just by using tables or divs and a bit of css. I have done it once in a small project and it looked quite cute.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.