Figure 1 - Updated XY-Plot User Control Introduction This update of the xy-plot user control adds some significant improvements and aesthetic enhancements to the graphing code. All points are moved into a generic list instead of an object array making the code more readable. Properties are added for a title, coloring of data and axis, the number of ticks per axis, maximum and minimum values for the x and y axis and more. The graph is now double-buffered and anti-aliased to produce a smoother curve. The y-axis text is now rotated the correct way. There is an algorithm for making sure the tick values on the axis have reasonable values. Since the graph is more or less open source, feel free to e-mail suggestions at [email protected] to improve the control. Storing Points in Generics: The power of templates allow us to create a collection of points for our control. We could have used the existing value type PointF in our List collection, but we chose to create our own reference type called PointFloat giving us more flexibility in using our coordinate information. The collection of points declaration is shown below: private List<PointFloat> m_points = new List<PointFloat>();Our previous XY-Plot control used an ArrayList which stores objects rather than the type in which we are interested. private ArrayList m_points = new ArrayList();The difference between using the generic collection List<PointFloat> and the object collection ArrayList is that every time we want a PointFloat object out of an ArrayList we have to unbox it. This is an extra step that the program needs to take. In order to understand, let's look at an example: Using an ArrayList: PointFloat nextPoint = (PointFloat)m_points[i]; // (1) get the point at index i and (2) unbox itUsing a generic List<PointFloat> PointFloat nextPoint = m_points[i]; // get the point at index iAs you can see, the generic list allows us to avoid the object class layer and go directly to manipulating the PointFloat object. Algorithm for Creating Tics on the Axis The axis are marked with values indicating a measure of where the point is located. In a way, an axis is like a ruler, so it should have value increments that "make sense" like a ruler. That means that the values should have precision values of 1, 2, or 5 in the last decimal spot because these values will eventually increment to a uniform unit. The algorithm below is an attempt to make this happen. First it calculates a raw increment based on the maximum and minimum axis values. Then it separates out the value to the right of the decimal. It then finds the most logical precision number that comes closest to the raw precision value and substitutes this value into the increment. Listing 1 - Calculating the Closest logical increment for the Graph
Table 1 - Smoothing Mode Enumeration Values
What is antialiasing? Anti-aliasing is a way to fool the eye into thinking a line is smooth instead of having jagged edges. The way anti-aliasing fools the eye is by putting shading around the jagged edges. The shading effect makes the jags look slightly fuzzy giving the illusion that the lines and curves are more "smooth". Smoothing is accomplished in the Paint Event handler as shown in listing 2: Listing 2 - Activating Smoothing in the Graphics Object
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: