PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/ChartControls/ChartLines.cs

#
C# | 182 lines | 125 code | 31 blank | 26 comment | 18 complexity | 921ffa3638a741419570bd305dd03f37 MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation
  4//===============================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===============================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===============================================================================
 17
 18using System.Windows;
 19using System.Collections.ObjectModel;
 20using System.Windows.Media;
 21using System.Collections.Specialized;
 22namespace StockTraderRI.ChartControls
 23{
 24    public class ChartLines : FrameworkElement
 25    {
 26        protected override void OnRender(DrawingContext drawingContext)
 27        {
 28            base.OnRender(drawingContext);
 29            if (VerticalAxisTickPositions != null)
 30            {
 31                if (DrawVerticalAxisReferenceLines)
 32                {
 33                    for (int i = 0; i < VerticalAxisTickPositions.Count; i++)
 34                        drawingContext.DrawLine(ReferenceLinePen, new Point(0, VerticalAxisTickPositions[i]), new Point(RenderSize.Width, VerticalAxisTickPositions[i]));
 35                }
 36                else if(DrawVerticalAxisTickMarks)
 37                {
 38                    for (int i = 0; i < VerticalAxisTickPositions.Count; i++)
 39                        drawingContext.DrawLine(ReferenceLinePen, new Point(VerticalAxis - TickMarksLength/2, VerticalAxisTickPositions[i]), new Point(VerticalAxis + TickMarksLength/2, VerticalAxisTickPositions[i]));
 40                }
 41            }
 42            drawingContext.DrawLine(ReferenceLinePen, new Point(VerticalAxis, 0), new Point(VerticalAxis, RenderSize.Height));
 43            if (HorizontalAxisTickPositions != null)
 44            {
 45                if (DrawHorizontalAxisReferenceLines)
 46                {
 47                    for (int i = 0; i < HorizontalAxisTickPositions.Count; i++)
 48                        drawingContext.DrawLine(ReferenceLinePen, new Point(HorizontalAxisTickPositions[i], 0), new Point(HorizontalAxisTickPositions[i], RenderSize.Height));
 49                }
 50                else if (DrawHorizontalAxisTickMarks)
 51                {
 52                    for (int i = 0; i < HorizontalAxisTickPositions.Count; i++)
 53                        drawingContext.DrawLine(ReferenceLinePen, new Point(HorizontalAxisTickPositions[i], HorizontalAxis - TickMarksLength / 2), new Point(HorizontalAxisTickPositions[i], HorizontalAxis + TickMarksLength / 2));
 54                }
 55            }
 56            drawingContext.DrawLine(ReferenceLinePen, new Point(0, HorizontalAxis), new Point(RenderSize.Width, HorizontalAxis));
 57        }
 58
 59        public static void OnVerticalAxisTickValuesChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
 60        {
 61            ChartLines obj = sender as ChartLines;
 62            if (obj != null && obj.VerticalAxisTickPositions != null)
 63            {
 64                obj.InvalidateVisual();
 65                obj.VerticalAxisTickPositions.CollectionChanged += new NotifyCollectionChangedEventHandler(obj.VerticalAxisTickPositions_CollectionChanged);
 66            }
 67        }
 68
 69        public void VerticalAxisTickPositions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
 70        {
 71            InvalidateVisual();                        
 72        }
 73
 74        public Pen ReferenceLinePen
 75        {
 76            get { return (Pen)GetValue(ReferenceLinePenProperty); }
 77            set { SetValue(ReferenceLinePenProperty, value); }
 78        }
 79
 80        // Using a DependencyProperty as the backing store for ReferenceLinePen.  This enables animation, styling, binding, etc...
 81        public static readonly DependencyProperty ReferenceLinePenProperty =
 82            DependencyProperty.Register("ReferenceLinePen", typeof(Pen), typeof(ChartLines), new UIPropertyMetadata(new Pen(Brushes.Black,1.0)));
 83
 84
 85        public ObservableCollection<double> VerticalAxisTickPositions
 86        {
 87            get { return (ObservableCollection<double>)GetValue(VerticalAxisTickPositionsProperty); }
 88            set { SetValue(VerticalAxisTickPositionsProperty, value); }
 89        }
 90
 91        // Using a DependencyProperty as the backing store for VerticalAxisTickPositions.  This enables animation, styling, binding, etc...
 92        public static readonly DependencyProperty VerticalAxisTickPositionsProperty =
 93            DependencyProperty.Register("VerticalAxisTickPositions", typeof(ObservableCollection<double>), typeof(ChartLines), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnVerticalAxisTickValuesChanged)));
 94
 95        public ObservableCollection<double> HorizontalAxisTickPositions
 96        {
 97            get { return (ObservableCollection<double>)GetValue(HorizontalAxisTickPositionsProperty); }
 98            set { SetValue(HorizontalAxisTickPositionsProperty, value); }
 99        }
100
101        // Using a DependencyProperty as the backing store for HorizontalAxisTickPositions.  This enables animation, styling, binding, etc...
102        public static readonly DependencyProperty HorizontalAxisTickPositionsProperty =
103            DependencyProperty.Register("HorizontalAxisTickPositions", typeof(ObservableCollection<double>), typeof(ChartLines), new UIPropertyMetadata(null));
104
105        public double TickMarksLength
106        {
107            get { return (double)GetValue(TickMarksLengthProperty); }
108            set { SetValue(TickMarksLengthProperty, value); }
109        }
110
111        // Using a DependencyProperty as the backing store for TickMarksLength.  This enables animation, styling, binding, etc...
112        public static readonly DependencyProperty TickMarksLengthProperty =
113            DependencyProperty.Register("TickMarksLength", typeof(double), typeof(ChartLines), new UIPropertyMetadata(8.0));
114
115
116        public bool DrawVerticalAxisTickMarks
117        {
118            get { return (bool)GetValue(DrawVerticalAxisTickMarksProperty); }
119            set { SetValue(DrawVerticalAxisTickMarksProperty, value); }
120        }
121
122        // Using a DependencyProperty as the backing store for DrawVerticalAxisTickMarks.  This enables animation, styling, binding, etc...
123        public static readonly DependencyProperty DrawVerticalAxisTickMarksProperty =
124            DependencyProperty.Register("DrawVerticalAxisTickMarks", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
125
126
127        public bool DrawVerticalAxisReferenceLines
128        {
129            get { return (bool)GetValue(DrawVerticalAxisReferenceLinesProperty); }
130            set { SetValue(DrawVerticalAxisReferenceLinesProperty, value); }
131        }
132
133        // Using a DependencyProperty as the backing store for DrawVerticalAxisReferenceLines.  This enables animation, styling, binding, etc...
134        public static readonly DependencyProperty DrawVerticalAxisReferenceLinesProperty =
135            DependencyProperty.Register("DrawVerticalAxisReferenceLines", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
136
137
138        public bool DrawHorizontalAxisTickMarks
139        {
140            get { return (bool)GetValue(DrawHorizontalAxisTickMarksProperty); }
141            set { SetValue(DrawHorizontalAxisTickMarksProperty, value); }
142        }
143
144        // Using a DependencyProperty as the backing store for DrawHorizontalAxisTickMarks.  This enables animation, styling, binding, etc...
145        public static readonly DependencyProperty DrawHorizontalAxisTickMarksProperty =
146            DependencyProperty.Register("DrawHorizontalAxisTickMarks", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
147
148
149        public bool DrawHorizontalAxisReferenceLines
150        {
151            get { return (bool)GetValue(DrawHorizontalAxisReferenceLinesProperty); }
152            set { SetValue(DrawHorizontalAxisReferenceLinesProperty, value); }
153        }
154
155        // Using a DependencyProperty as the backing store for DrawHorizontalAxisReferenceLines.  This enables animation, styling, binding, etc...
156        public static readonly DependencyProperty DrawHorizontalAxisReferenceLinesProperty =
157            DependencyProperty.Register("DrawHorizontalAxisReferenceLines", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
158
159
160        public double HorizontalAxis
161        {
162            get { return (double)GetValue(HorizontalAxisProperty); }
163            set { SetValue(HorizontalAxisProperty, value); }
164        }
165
166        // Using a DependencyProperty as the backing store for HorizontalAxis.  This enables animation, styling, binding, etc...
167        public static readonly DependencyProperty HorizontalAxisProperty =
168            DependencyProperty.Register("HorizontalAxis", typeof(double), typeof(ChartLines), new UIPropertyMetadata(0.0));
169
170
171        public double VerticalAxis
172        {
173            get { return (double)GetValue(VerticalAxisProperty); }
174            set { SetValue(VerticalAxisProperty, value); }
175        }
176
177        // Using a DependencyProperty as the backing store for VerticalAxis.  This enables animation, styling, binding, etc...
178        public static readonly DependencyProperty VerticalAxisProperty =
179            DependencyProperty.Register("VerticalAxis", typeof(double), typeof(ChartLines), new UIPropertyMetadata(0.0));
180
181    }
182}