How to create bar graph dynamically using gd libraries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mukeshrasm
    Contributor
    • Nov 2007
    • 254

    How to create bar graph dynamically using gd libraries

    Hi

    I have created bar graph using gd libraries and php but i want to create this graph dynamically. How can I do this? based on number of data width and height of the image will be changed.
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    Well I don't know what you've got so far, in detail that is, but you need to keep track of how many bars, obeservations etc you are going to work with.

    You should either calculate this or provide it in some other way.

    Are you reading values from a database or in some other way?

    Comment

    • pedalpete
      New Member
      • Oct 2007
      • 109

      #3
      not sure exactly what you are looking to do, or have done,
      but I've been building bar graphs with css for years, and find it always works well for me.

      Just set the div height or width to be dynamically output into your page and voila, instant bar chart.

      Hope it helps

      Comment

      • mukeshrasm
        Contributor
        • Nov 2007
        • 254

        #4
        Originally posted by pedalpete
        not sure exactly what you are looking to do, or have done,
        but I've been building bar graphs with css for years, and find it always works well for me.

        Just set the div height or width to be dynamically output into your page and voila, instant bar chart.

        Hope it helps

        I dont how you are doing it using css so. if you may please give your code so that I can try to do it using css.

        Comment

        • mukeshrasm
          Contributor
          • Nov 2007
          • 254

          #5
          Originally posted by MarkoKlacar
          Hi,

          Well I don't know what you've got so far, in detail that is, but you need to keep track of how many bars, obeservations etc you are going to work with.

          You should either calculate this or provide it in some other way.

          Are you reading values from a database or in some other way?
          presently I am not taking the data from database but later I will. I have track that how many bar I needed. thing is that when I try to give some negative values for graph it is not showing the bar. so If you need code definetly tell me.

          or if you are using some thing like that please help me.

          Comment

          • MarkoKlacar
            Recognized Expert Contributor
            • Aug 2007
            • 296

            #6
            I've made my own charts, I wasn't using php when I did mine but I stil think I can give you some pointers.

            Let me know when you get stuck or need advice.

            Comment

            • ifedi
              New Member
              • Jan 2008
              • 60

              #7
              Originally posted by pedalpete
              not sure exactly what you are looking to do, or have done,
              but I've been building bar graphs with css for years, and find it always works well for me.

              Just set the div height or width to be dynamically output into your page and voila, instant bar chart.

              Hope it helps
              Yeah, I've done a bar-graph with php and css myself. There were w major disadvantages, however, which I noted:
              1. The 'bar divs' had to be absolutely positioned to work well, and there was no easy way of placing the div wrapping the bars on the page, unless the larger container-divs are themselves absolutely positioned.
              2. The graphs will need a refresh each time you want the current status displayed.

              I reckon that something like ajax would solve the latter problem. Unfortunately, I'm completely green, and painfully slow in ajax.

              In conclusion: I still need help here!

              Ifedi

              Comment

              • pedalpete
                New Member
                • Oct 2007
                • 109

                #8
                I can't track down any of my old code right now, but here's the basics,

                step 1 - have your php page create the div which will be the bar and the size of the bar is the width field

                each bar gets it's own div, the output html looks like this
                Code:
                <div class="bar"  style=" width: [I]width-variable[/I] ">[I]your bar variable[/I]</div>
                then your css sets the rest of the style for the div

                Code:
                .bar { 
                        height: 14px;
                        border: solid black 1px;
                        background-color: blue;
                }
                that's it! You can line-up your bars by setting absolute positions if necessary, etc.
                Vertical bars are a bit more difficult, but can be done (I did it a LONG time ago and remember positioning it being a pain, but eventually got it working).

                If memory serves me correctly, the vertical bars where made by creating a div with a set width, and then having the background image begin partway the div setting the background position in the output.

                Hope it helps,
                Pete

                Comment

                • MarkoKlacar
                  Recognized Expert Contributor
                  • Aug 2007
                  • 296

                  #9
                  Aren't we talking php-charts, i.e creating an image useing the gd libraries?
                  Or am I way off base?

                  Comment

                  Working...