www.webdeveloper.com
Results 1 to 4 of 4

Thread: Bar Graph Creation Help

  1. #1
    Join Date
    Jun 2007
    Posts
    191

    Exclamation Bar Graph Creation Help

    I used a script from another website to build a bar graph for my website. Here is the code:

    PHP Code:
    <?php
        
    echo "<h2>Margaret Lackey State Offering</h2>";
        
    # ------- The graph values in the form of associative array
        
    $values=array(
            
    "Goal" => 500,
            
    "Current" => 405
        
    );

     
        
    $img_width=150;
        
    $img_height=200
        
    $margins=20;

     
        
    # ---- Find the size of graph by substracting the size of borders
        
    $graph_width=$img_width $margins 2;
        
    $graph_height=$img_height $margins 2
        
    $img=imagecreate($img_width,$img_height);

     
        
    $bar_width=20;
        
    $total_bars=count($values);
        
    $gap= ($graph_width$total_bars $bar_width ) / ($total_bars +1);

     
        
    # -------  Define Colors ----------------
        
    $bar_color=imagecolorallocate($img,0,64,128);
        
    $background_color=imagecolorallocate($img,240,240,255);
        
    $border_color=imagecolorallocate($img,200,200,200);
        
    $line_color=imagecolorallocate($img,220,220,220);
     
        
    # ------ Create the border around the graph ------

        
    imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
        
    imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);

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

     
        
    # -------- Create scale and draw horizontal lines  --------
        
    $horizontal_lines=20;
        
    $horizontal_gap=$graph_height/$horizontal_lines;

        for(
    $i=1;$i<=$horizontal_lines;$i++){
            
    $y=$img_height $margins $horizontal_gap $i ;
            
    imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
            
    $v=intval($horizontal_gap $i /$ratio);
            
    imagestring($img,0,5,$y-5,$v,$bar_color);

        }
     
     
        
    # ----------- Draw the bars here ------
        
    for($i=0;$i$total_bars$i++){ 
            
    # ------ Extract key and value pair from the current pointer position
            
    list($key,$value)=each($values); 
            
    $x1$margins $gap $i * ($gap+$bar_width) ;
            
    $x2$x1 $bar_width
            
    $y1=$margins +$graph_heightintval($value $ratio) ;
            
    $y2=$img_height-$margins;
            
    imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);        
            
    imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
        }
        
    header("Content-type:image/png");
        
    imagepng($img);
        
    $_REQUEST['asdfad']=234234;

    ?>
    This is the output that I get:

    Code:
    <h2>Margaret Lackey State Offering</h2>�PNG
    
    ���
    IHDR�����������w������PLTE�@����������OS�
    ��mIDATX��AN�0��R��l`_ye���=#�N��|
    ˫_�Y8@0NJ@�գJ"���U?�����o��+�D5��ٰX�D`I�gfs����!�q.쿈;�� ����q�Z
    {m�3��^ް�.�ڪ;l7��~�\�a7�3�y��囕�kI�n"�,:�ns�b~R��B�L�7�%�&��["���[_��Ǻ���7ŵ�=>?���r�?W�	A���vח�k��!
    ��a�-��vt>ҭ��S;�5�
    l̆9� ��\�7�S�o�ڀ�Т���-�|�A�2��L�oH7�oP7�o�P~� �N��7��oe���o �N��O$�ڸ�P��t[����-'9�ާ@�Ʈ>�tK�(��7����M�o�m��|#������o�{v����;e�F�
    ��{a�|C�S�8�߿��ΧP�%���:�߿M�%�<ܡ�������m�|�/#j6C>�Y��r��nI�E'��Q��v�pX~�R�ZJ��̯[�{�-p~�}��~�}��}w�+�n����/�@�˙�q?|�P��(��0��VM�%LL�B�&��l�t���1�Q��[�*�ߣxuф����'a\��a�OGg����IEND�B`�
    Any thoughts?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,329
    You cannot combine HTML text and image output in the same HTTP response. You will need an <img> tag in the main HTML page output that will point to a separate PHP script for generating the graph image.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Jun 2007
    Posts
    191
    I am not exactly sure what you are saying. Can you give an example.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,329
    Main HTML output:
    Code:
    <h2>Example:</h2>
    <p>Here comes a bar graph....<br />
    <img src="bargraph.php" width="300" height="50" alt="bar graph" />
    </p>
    bargraph.php:
    PHP Code:
    <?php
    // do image stuff to create graph in $img, then...
    header('Content-Type: image/png');
    imagepng($img);
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.11084 seconds
  • Memory Usage 2,911KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates