Hi all, I'm trying to add a little bit of extra functionality to my programme by using the Graphics class to draw lines in accordance to values held within text boxes...

I've converted the values of two textboxes to Int32's like so:

gUK = Convert.ToInt32(tbUK.Text);
gNotUK = Convert.ToInt32(tbNotUK.Text);

(I have code to prevent the conversion occuring if the text boxes contain null values in another part of the programme)

And now I'm drawing the lines:

int total = gUK + gNotUK+400;

            Graphics clear = CreateGraphics();
Graphics graphicsUK = CreateGraphics();
            Graphics graphicsNotUK = CreateGraphics();
            Pen pUK = new Pen(Color.Crimson);
            Pen pNotUK = new Pen(Color.Crimson);
            
            try
            {
                clear.FillRectangle(Brushes.LightGray, ClientRectangle);                
                graphicsUK.DrawRectangle(pUK, 0, 300, 0 + (gNotUK/total), 5);
            }
            catch
            {
                MessageBox.Show("No NotUK number");
            }              
            
            try
            {                
                graphicsNotUK.DrawRectangle(pNotUK, 0, 310, 0 + (gUK/total), 5);
            }
            catch
            {
                MessageBox.Show("No UK number");
            }

As you can see from the code above, I'm comparing the two values gUK and gNotUK. I would like to use an algorithm to do the following:

If for example, the first value is gUK = 1 (assuming that the text box carrying the gNotUK is still null)... gUK is 100% of the total as this is the only value... So I would like a line representing the gUK value as 100%. As soon as the gNotUK has a value (e.g. 1 again) gUK would only be 50% of the total and so the line would be halved and the two lines representing the values would be in line. I've tried numerous algorithms but failed each time!

I'm creating this programme on the assumption that there will be more than 100,000 entries, so this percentage algorithm is particularly important as the lines have to stay within the form...

Thanks for reading!

Rob

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

So basically you want a bar chart. That's pretty simple. In fact I bet it has already been done before if you search google.

You're right in thinking it's a bar chart, but the only difference is, there's a 100% total that has to be shared between the two values:

Scenario 1:
gUK = 0;
gNotUK = 1;

gUK:
gNotUK: --------------------------------------


Scenario 2:
gUK = 1;
gNotUK = 1;

gUK: -------------------
gNotUK: -------------------


Scenario 3:
gUK = 2;
gNotUK = 1;

gUK: ----------------------------
gNotUK: ----------


Etc... See what I mean? I've made plenty of bar charts using C# but never one that used an algorithm like this...

Thanks

Hi again, I've modified the drawing process, so it's now:

try
            {
                clear.FillRectangle(Brushes.LightGray, ClientRectangle);                
                graphicsUK.DrawRectangle(pUK, 0, 300, 0 + (gUK/total*500), 5);
            }
            catch
            {
                MessageBox.Show("No NotUK number");
            }              
            
            try
            {                
                graphicsNotUK.DrawRectangle(pNotUK, 0, 310, 0 + (gNotUK/total*500), 5);
            }
            catch
            {
                MessageBox.Show("No UK number");
            }

This on paper would be like doing the following:

Say we've got 4 countries listed in total, 3 of which are the UK and 1 is outside of the UK. So UK is 3/4 (4 being the total) which equals 0.75 (75%), I multiply 0.75 by 500 (could be anything) and it works fine for when there's just one value... As soon as the other value is added, none of the lines are drawn... The algorithm is forming... It's just getting it to work!

Thanks!

Be a part of the DaniWeb community

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