C# Класс GraphSample.CustomControls.Graph

NOTE FROM THE DEVELOPER: This is a custom control designed for Windows Phone 7 to provide a chart that accepts custom user data and allows the user of the graph to scale and translate the graph along it's X-Axis. Various design decisions were made along the way that focused on performance, as such I implemented, and later abandoned scaling along the Y-Axis. Partly because I was seeking smoother touch feedback and partly because it was difficult to control and did not add a lot of value for my application (a stock price chart). This was my very first attempt at Silverlight, and one of my first trips to the UI as I am primarily a backend developer. Because I am unfortunately not a great designer, I likely did a few hackish things in code that should have been done in the XAML. There may also be internal properties that you would like exposed to the XAML which I kept as public member variables as I am more comfortable in C# than XAML. Finally, I did not intend to release this publicly and various functions could use refactoring - some are very long like CreateGraph() and should be broken up. Along the same lines, some functions perform several tasks and you will probably also hate me for various variable names :). I sprinkled comments throughout the code to help you along, and performed a bit of refactoring before making this publicly available. Now, I want to move on to other projects, but also share this component with the community. If I come across bugs or improve functionality in this control for my own applications, I will update the source online and pass these on to you. Hopefully you can get some value from it in its current state, or modified for your own applications. Happy coding! You can see this control in use in two of my applications already on the WP7 marketplace: "Stock Portfolio" and "Car Log." Please contact me at [email protected] for bugs, comments or inquiries. You can find additional details at www.bryanalger.com/projects -Bryan Alger 4/24/2011
Наследование: System.Windows.Controls.UserControl
Показать файл Открыть проект

Открытые свойства

Свойство Тип Описание
AccentColorProperty System.Windows.DependencyProperty
ChartTitleProperty System.Windows.DependencyProperty
DarkAccentColorProperty System.Windows.DependencyProperty
GraphDataProperty System.Windows.DependencyProperty

Открытые методы

Метод Описание
Graph ( ) : System
ZoomOut ( ) : void

Completely zoom out to all of the graph data

ZoomToPoints ( double minX, double maxX ) : void

Zoom the graph onto a subset of its points

Защищенные методы

Метод Описание
MeasureOverride ( Size availableSize ) : Size
OnManipulationCompleted ( System.Windows.Input.ManipulationCompletedEventArgs e ) : void
OnManipulationDelta ( System.Windows.Input.ManipulationDeltaEventArgs e ) : void
OnManipulationStarted ( System.Windows.Input.ManipulationStartedEventArgs e ) : void

Приватные методы

Метод Описание
ChangeBounds ( double newWidth, double newHeight ) : void

Resize the graph.

ClearGraph ( ) : void

Draw an empty graph with "No data to display" text

CombineScaleTransform ( double CenterX, double ScaleX ) : void

Recompute the graph's scale transformation given a user manipulation.

ComputeGraphBounds ( ) : void

Iterates over all of the points and determines the range of values (min, max for x and y coordinates)

CreateGraph ( ) : void

Meat and potatoes, draw the graph

DrawHighlightedPoint ( ) : void

Draws the highlighted point (if any) that the user selected when he touched the chart.

FindNearestPoint ( double targetX, int graphIndex ) : int

Uses binary search to find the point with the given targetX or the point immediately preceding that x coordinate.

FullRedraw ( ) : void
GenerateLeftBorder ( ) : void

Generate the Y-Axis border of the graph. This can be done once for the graph since the Y-Axis does not scale or transform in this version.

GenerateSubGraphs ( ) : void

Generates several sub graphs with progressively fewer number of data points This allows us to redraw the graph quickly during manipulation

Graph_Loaded ( object sender, RoutedEventArgs e ) : void
Graph_Unloaded ( object sender, RoutedEventArgs e ) : void
OnGraphDataChanged ( DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e ) : void

Called when the GraphData property was set by the user

ScreenToWorld ( double x, double y ) : Point

Transforms screen coordinates to world coordinates.

ScreenToWorldX ( double x ) : double

Transforms only a screen x coordinate to world x coordinates

ScreenToWorldY ( double y ) : double

Transforms only a screen y coordinate to world y coordinates

TriggerGraphRefresh ( ) : void

Trigger a full refresh of the graph starting with a lower resolution and working up

UpdateGraphFillGradient ( ) : void

Small helper function that generates the Brush that comprises the gradient for the fill graph

WorldToScreenOptimized ( double x, double y ) : Point

Transforms world coordinates to screen coordinates, noting that the Graph actually computed the y coordinate in advance (when GraphData was set) since we only scale on the X-Axis

WorldToScreenYCoord ( double y ) : double

Transform a world y-coordinate to a screen-y coordinate.

ZoomToPoints ( int minPoint, int maxPoint ) : void

Zooms the graph onto a subset of points

drawTimer_Tick ( object sender, EventArgs e ) : void

This is the graph drawing loop. It will draw low resolution graphs while the graph is being manipulated, and progressively higher resolution when content is static.

Описание методов

Graph() публичный Метод

public Graph ( ) : System
Результат System

MeasureOverride() защищенный Метод

protected MeasureOverride ( Size availableSize ) : Size
availableSize System.Windows.Size
Результат System.Windows.Size

OnManipulationCompleted() защищенный Метод

protected OnManipulationCompleted ( System.Windows.Input.ManipulationCompletedEventArgs e ) : void
e System.Windows.Input.ManipulationCompletedEventArgs
Результат void

OnManipulationDelta() защищенный Метод

protected OnManipulationDelta ( System.Windows.Input.ManipulationDeltaEventArgs e ) : void
e System.Windows.Input.ManipulationDeltaEventArgs
Результат void

OnManipulationStarted() защищенный Метод

protected OnManipulationStarted ( System.Windows.Input.ManipulationStartedEventArgs e ) : void
e System.Windows.Input.ManipulationStartedEventArgs
Результат void

ZoomOut() публичный Метод

Completely zoom out to all of the graph data
public ZoomOut ( ) : void
Результат void

ZoomToPoints() публичный Метод

Zoom the graph onto a subset of its points
public ZoomToPoints ( double minX, double maxX ) : void
minX double The desired minimum x parameter of the drawn graph
maxX double The desired maximum x parameter of the drawn graph
Результат void

Описание свойств

AccentColorProperty публичное статическое свойство

public static DependencyProperty,System.Windows AccentColorProperty
Результат System.Windows.DependencyProperty

ChartTitleProperty публичное статическое свойство

public static DependencyProperty,System.Windows ChartTitleProperty
Результат System.Windows.DependencyProperty

DarkAccentColorProperty публичное статическое свойство

public static DependencyProperty,System.Windows DarkAccentColorProperty
Результат System.Windows.DependencyProperty

GraphDataProperty публичное статическое свойство

public static DependencyProperty,System.Windows GraphDataProperty
Результат System.Windows.DependencyProperty