Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GDI+ & Calculation Problems.

GDI+ & Calculation Problems.

Scheduled Pinned Locked Moved C#
graphicswinformstutorial
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gavin Roberts
    wrote on last edited by
    #1

    Hi all, First of all, please look at the following image: http://img171.imageshack.us/img171/6038/graphne9.jpg[^] I'm currently working on a reporting system for our web stats system. Code: Pen Black = new Pen(Brushes.Black, 1); Pen Gray = new Pen(Brushes.LightGray, 1); Bitmap b = new Bitmap(parentWidth,parentHeight); Graphics g = Graphics.FromImage(b); g = GenerateBG(g, parentWidth, parentHeight); // Generates linear background int width = parentWidth - 50; // generates inner width int height = parentHeight - 50; // generates inner height int spacer_width = width/7; // amount of space between dates (y axis) int spacer_height = 0; int maxCount = 0; ArrayList Data = new ArrayList(); // loops from today, backwards 7 days and writes the date // and then gets any data against that date and stores it into // an arraylist for future usage. // At the same time, it also finds out the maximum number over // the 7 days. for(int i = -6; i < 1; i++) { DateTime date = DateTime.Now.AddDays(i); string toWrite = date.Day.ToString() + "/" + date.Month.ToString(); g.DrawString(toWrite, new Font("Verdana", 7), Brushes.Black, ((i+7)*spacer_width), parentHeight -20); object[] dateData = dal.getStatsForDate(date.Date); if(dateData.Length > maxCount) maxCount = dateData.Length; Data.Add(dateData); } spacer_height = (height/maxCount); // amount of space between numbers (x axis) int tempHeight = parentHeight; // temp storage int modCheck = Isqrt(maxCount); // gets square root of the maxcount for(int i = 0; i < maxCount; i++) { if((i%modCheck)==0) // Space Saver { g.DrawLine(Gray, 50, tempHeight-25, width+25, tempHeight-25); // draws guide line g.DrawString(i.ToString(),new Font("Verdana", 7), Brushes.Black, 5, tempHeight-32); // draws int value } tempHeight = (tempHeight-spacer_height); // calculate next location for guide line and int value } g.DrawLine(Black, 50, 25, 50, parentHeight-25); // draws x axis g.DrawLine(Black, 50, parentHeight - 25, parentWidth-25, parentHeight-25); // draws y axis Now if you look at the image and the code, i've got something wrong and I can't figure where. Basically, I have calculated the di

    M 1 Reply Last reply
    0
    • G Gavin Roberts

      Hi all, First of all, please look at the following image: http://img171.imageshack.us/img171/6038/graphne9.jpg[^] I'm currently working on a reporting system for our web stats system. Code: Pen Black = new Pen(Brushes.Black, 1); Pen Gray = new Pen(Brushes.LightGray, 1); Bitmap b = new Bitmap(parentWidth,parentHeight); Graphics g = Graphics.FromImage(b); g = GenerateBG(g, parentWidth, parentHeight); // Generates linear background int width = parentWidth - 50; // generates inner width int height = parentHeight - 50; // generates inner height int spacer_width = width/7; // amount of space between dates (y axis) int spacer_height = 0; int maxCount = 0; ArrayList Data = new ArrayList(); // loops from today, backwards 7 days and writes the date // and then gets any data against that date and stores it into // an arraylist for future usage. // At the same time, it also finds out the maximum number over // the 7 days. for(int i = -6; i < 1; i++) { DateTime date = DateTime.Now.AddDays(i); string toWrite = date.Day.ToString() + "/" + date.Month.ToString(); g.DrawString(toWrite, new Font("Verdana", 7), Brushes.Black, ((i+7)*spacer_width), parentHeight -20); object[] dateData = dal.getStatsForDate(date.Date); if(dateData.Length > maxCount) maxCount = dateData.Length; Data.Add(dateData); } spacer_height = (height/maxCount); // amount of space between numbers (x axis) int tempHeight = parentHeight; // temp storage int modCheck = Isqrt(maxCount); // gets square root of the maxcount for(int i = 0; i < maxCount; i++) { if((i%modCheck)==0) // Space Saver { g.DrawLine(Gray, 50, tempHeight-25, width+25, tempHeight-25); // draws guide line g.DrawString(i.ToString(),new Font("Verdana", 7), Brushes.Black, 5, tempHeight-32); // draws int value } tempHeight = (tempHeight-spacer_height); // calculate next location for guide line and int value } g.DrawLine(Black, 50, 25, 50, parentHeight-25); // draws x axis g.DrawLine(Black, 50, parentHeight - 25, parentWidth-25, parentHeight-25); // draws y axis Now if you look at the image and the code, i've got something wrong and I can't figure where. Basically, I have calculated the di

      M Offline
      M Offline
      mikone
      wrote on last edited by
      #2

      could you check the maxCount variable for its value? it seems like its value is too big.. like 2 times bigger than it should be... On the other hand i don't know the value of ParentHeight so it could be possible that you're calculating with a wrong value all the time... what exactly is that "dal" object you're using? What exactly does the getStatsForDate method and are you sure its outpu is correct?

      G 2 Replies Last reply
      0
      • M mikone

        could you check the maxCount variable for its value? it seems like its value is too big.. like 2 times bigger than it should be... On the other hand i don't know the value of ParentHeight so it could be possible that you're calculating with a wrong value all the time... what exactly is that "dal" object you're using? What exactly does the getStatsForDate method and are you sure its outpu is correct?

        G Offline
        G Offline
        Gavin Roberts
        wrote on last edited by
        #3

        maxCount will currently read 90. parentHeight = 200 parentWidth = 700 dal is my data access layer, and getStatsForDate will take a date, and get all records in the database against this date. maxCount will start off life as zero, and each time I loop through the dates, and retrieve the data via getStatsForDate, I check the length to see how many rows were returned. If the amount of rows returned is more then the current value of maxCount, maxCount will change to the amount of rows. Thanks for replying.

        1 Reply Last reply
        0
        • M mikone

          could you check the maxCount variable for its value? it seems like its value is too big.. like 2 times bigger than it should be... On the other hand i don't know the value of ParentHeight so it could be possible that you're calculating with a wrong value all the time... what exactly is that "dal" object you're using? What exactly does the getStatsForDate method and are you sure its outpu is correct?

          G Offline
          G Offline
          Gavin Roberts
          wrote on last edited by
          #4

          I think I may have sorted it. rather then using the maxCount to calculate the space between lines etc, I have got the square root of the maxCount and devided that by the height. Regardless of the maxCount value, it seems to be sticking in the correct place. Thanks

          M 1 Reply Last reply
          0
          • G Gavin Roberts

            I think I may have sorted it. rather then using the maxCount to calculate the space between lines etc, I have got the square root of the maxCount and devided that by the height. Regardless of the maxCount value, it seems to be sticking in the correct place. Thanks

            M Offline
            M Offline
            mikone
            wrote on last edited by
            #5

            Okay, i think i know the error now. You want the value 81 to be on the top of the graphic (almost y = 0??). if so, the problem is the height calculation. add 2 variables int dlHeight = int dsHeight = just replace this piece of code spacer_height = (height/maxCount); // amount of space between numbers (x axis) int tempHeight = parentHeight; // temp storage int modCheck = Isqrt(maxCount); // gets square root of the maxcount for(int i = 0; i < maxCount; i++) { if((i%modCheck)==0) // Space Saver { g.DrawLine(Gray, 50, tempHeight-25, width+25, tempHeight-25); // draws guide line g.DrawString(i.ToString(),new Font("Verdana", 7), Brushes.Black, 5, tempHeight-32); // draws int value } tempHeight = (tempHeight-spacer_height); // calculate next location for guide line and int value } with this one int tempHeight = parentHeight; // temp storage // These variables are new int sqrtMaxCount = Isqrt(maxCount); int rowHeight = height / sqrtMaxCount; for(int i = 0; i < sqrtMaxCount; i++) { // Try to check the calculation... its simpler than before and therefore should be understandable... g.DrawLine(Gray, 50, tempHeight-rowHeight, width+25, tempHeight-rowHeight); g.DrawString(i.ToString(),new Font("Verdana", 7), Brushes.Black, 5, tempHeight-rowHeight-7); // -7 because it was the difference between the relative Line and String Y value. tempHeight -= rowHeight; // This has changed a little bit. } in case you haven't found it out yourself yet :) I hope i didnt miss anything - currently i'm too lazy to write it myself but if it doesn't work i'll give it a try :) *edit* i forgot to change the condition for the for-loop... that would have turned out bad!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups