Thursday, February 11, 2016

WPF Graph Control


Drawing graph is always complex because you need to work with two different coordinates at a time. The first is world coordinate system and the second is device coordinate system that can be measured by pixels on the screen. The most complex part is when you need to position something in world coordinates but draw in device coordinates. You use world coordinates to figure out the place where the tic mark should be placed and then you need to calculate the tic marks in pixels. And you use device coordinates to draw some text on the graph to level something. Another serious problem you may face is making the graph’s line having a consistent thickness. You can get a solution by using a layout transform and when the transform stretches the graph, the lines drawn for the graphs will also be stretched.
                                                                                        So, you need to work both in world and device coordinate system. In case of WPF charts you can’t draw directly on the drawing surface but if you need, then you can use line, ellipse, rectangle and other shape controls to draw. You can also use XAML code for WPF graph control. But, if you want to draw a non-trivial graph then you need to use code to do it.
After coding the program creates a  Geometry Group to represent the X axis and then The code creates a Line to represent the axis’s baseline and adds it to the group. Then, it create a bunch of Line objects to represent tic marks and adds them to the group by using a loop. After finishing these  the program creates a Path object and sets its StrokeThickness andStroke properties. The code adds the path object to the can graph object’s Children compilation.

The code repeats for the Y axis. Now, the code creates some graph data and for each data set, aPointCollection object is created. It creates a group of scattered points and adds them. After finishing the data making process, the program creates a Polyline and sets its drawing property and point’s property.  Finally the code adds the Polyline to the canGraph object’s Children compilation.
That’s all you need to do for the WPFgraph control.



 .

No comments:

Post a Comment