Skip to main content
GameDev.net gamedev.net
🔒 Locked

Line Graph

Started by Shaun Patterson Aug 15, 2001 at 10:36 PM 3 replies 1.3k views
Original Post
Shaun Patterson
Shaun Patterson
A little off topic.. I apologize - but i know there''s some really great programmers here that can help me I''m "trying" to create a line graph for an app. My only problem is how to get nice even numbers for the Y axis for example if you had a set of y values: 1840 570 732 220 1242 Now, I can tell by looking at that I want a graph that looks kinda like: 2000------------- | | 1500------------- | | 1000------------- | | 0500------------- | | 0---------------- So what kind of algo could I use to create "pretty" numbers on the graph? Thanks ~Shaun
Colin Jeanne
Colin Jeanne
I once did something similar to this in VB. But, if you play around a little and read up at MSDN I''m sure you can do the same in C++.

Basically what I did was this:
Since the pixel coorinate system is upside down compared to what we commonly use, you have to reverse the Y coorinate.

So, if you were to plot a pixel at (0, 123) then you would do this:

  
HDC your_window_hdc = GetDC(your_window); //Gets DC of the window

int max_y = GetDeviceCaps(your_window_hdc, VERTRES); //Gets the number of pixels high

COLORREF some_colorref = RBG(0, 0, 0); //Creates a black color


SetPixel(your_window_hdc, 0, max_y - 123, some_colorref);
//Sets the pixel to black



If that doesn''t work (which it should, unless I''ve forgotten something important), read around MSDN about DC''s, COLORREFS, and SetPixel.

To get back to the specifics of the graphing engine - my graphing engine made points that were 3 pixels by 3 pixels for visibility reasons. It also plotted every 3rd pixel to save time and because each plot was 3x3.
Shaun Patterson
Shaun Patterson
no no. I can draw a pixel, I can draw lines, I can figure out where to draw them, etc

I can even MAKE a line graph. my problem is how to figure out a USER friendly Y axis increment.

again, from my example, the increment would be 500. thus the y axis marks would go from 0 - 2000 at increments of 500

now, how do find those 3 numbers is what I''m asking (increment val, lowest val, highest val)

Hope this clears things up
~Shaun










Colin Jeanne
Colin Jeanne
OH! I''m sorry, I misread.

Well, what I did was ask the user to specify and upper bound, but another way is to round the highest value to the heightest hundred or thousand (whatever you choose), then divide that into 4 or 8 or however many equal parts works for you.
BeerNutts
BeerNutts
This is from the top of my head, and will give you an idea how to do this.

1st, find the min and max numbers of you''re samples (obviously)

2nd, find the resolution of each values (ie, find how many digits it has (10, 100, 1000, etc.))

3rd, use the lower resolution for the min value, and use the higher resolution for the max value.

That is just a thought off the top of my head. No algorithm written in C, but you can do that yourself.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.