GDI+ & Calculation Problems.
-
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 -
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 dicould 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?
-
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?
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.
-
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?
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
-
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
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 oneint 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!