PageRenderTime 38ms CodeModel.GetById 7ms RepoModel.GetById 0ms 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. using System.Windows;
  18. using System.Collections.ObjectModel;
  19. using System.Windows.Media;
  20. using System.Collections.Specialized;
  21. namespace StockTraderRI.ChartControls
  22. {
  23. public class ChartLines : FrameworkElement
  24. {
  25. protected override void OnRender(DrawingContext drawingContext)
  26. {
  27. base.OnRender(drawingContext);
  28. if (VerticalAxisTickPositions != null)
  29. {
  30. if (DrawVerticalAxisReferenceLines)
  31. {
  32. for (int i = 0; i < VerticalAxisTickPositions.Count; i++)
  33. drawingContext.DrawLine(ReferenceLinePen, new Point(0, VerticalAxisTickPositions[i]), new Point(RenderSize.Width, VerticalAxisTickPositions[i]));
  34. }
  35. else if(DrawVerticalAxisTickMarks)
  36. {
  37. for (int i = 0; i < VerticalAxisTickPositions.Count; i++)
  38. drawingContext.DrawLine(ReferenceLinePen, new Point(VerticalAxis - TickMarksLength/2, VerticalAxisTickPositions[i]), new Point(VerticalAxis + TickMarksLength/2, VerticalAxisTickPositions[i]));
  39. }
  40. }
  41. drawingContext.DrawLine(ReferenceLinePen, new Point(VerticalAxis, 0), new Point(VerticalAxis, RenderSize.Height));
  42. if (HorizontalAxisTickPositions != null)
  43. {
  44. if (DrawHorizontalAxisReferenceLines)
  45. {
  46. for (int i = 0; i < HorizontalAxisTickPositions.Count; i++)
  47. drawingContext.DrawLine(ReferenceLinePen, new Point(HorizontalAxisTickPositions[i], 0), new Point(HorizontalAxisTickPositions[i], RenderSize.Height));
  48. }
  49. else if (DrawHorizontalAxisTickMarks)
  50. {
  51. for (int i = 0; i < HorizontalAxisTickPositions.Count; i++)
  52. drawingContext.DrawLine(ReferenceLinePen, new Point(HorizontalAxisTickPositions[i], HorizontalAxis - TickMarksLength / 2), new Point(HorizontalAxisTickPositions[i], HorizontalAxis + TickMarksLength / 2));
  53. }
  54. }
  55. drawingContext.DrawLine(ReferenceLinePen, new Point(0, HorizontalAxis), new Point(RenderSize.Width, HorizontalAxis));
  56. }
  57. public static void OnVerticalAxisTickValuesChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
  58. {
  59. ChartLines obj = sender as ChartLines;
  60. if (obj != null && obj.VerticalAxisTickPositions != null)
  61. {
  62. obj.InvalidateVisual();
  63. obj.VerticalAxisTickPositions.CollectionChanged += new NotifyCollectionChangedEventHandler(obj.VerticalAxisTickPositions_CollectionChanged);
  64. }
  65. }
  66. public void VerticalAxisTickPositions_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  67. {
  68. InvalidateVisual();
  69. }
  70. public Pen ReferenceLinePen
  71. {
  72. get { return (Pen)GetValue(ReferenceLinePenProperty); }
  73. set { SetValue(ReferenceLinePenProperty, value); }
  74. }
  75. // Using a DependencyProperty as the backing store for ReferenceLinePen. This enables animation, styling, binding, etc...
  76. public static readonly DependencyProperty ReferenceLinePenProperty =
  77. DependencyProperty.Register("ReferenceLinePen", typeof(Pen), typeof(ChartLines), new UIPropertyMetadata(new Pen(Brushes.Black,1.0)));
  78. public ObservableCollection<double> VerticalAxisTickPositions
  79. {
  80. get { return (ObservableCollection<double>)GetValue(VerticalAxisTickPositionsProperty); }
  81. set { SetValue(VerticalAxisTickPositionsProperty, value); }
  82. }
  83. // Using a DependencyProperty as the backing store for VerticalAxisTickPositions. This enables animation, styling, binding, etc...
  84. public static readonly DependencyProperty VerticalAxisTickPositionsProperty =
  85. DependencyProperty.Register("VerticalAxisTickPositions", typeof(ObservableCollection<double>), typeof(ChartLines), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnVerticalAxisTickValuesChanged)));
  86. public ObservableCollection<double> HorizontalAxisTickPositions
  87. {
  88. get { return (ObservableCollection<double>)GetValue(HorizontalAxisTickPositionsProperty); }
  89. set { SetValue(HorizontalAxisTickPositionsProperty, value); }
  90. }
  91. // Using a DependencyProperty as the backing store for HorizontalAxisTickPositions. This enables animation, styling, binding, etc...
  92. public static readonly DependencyProperty HorizontalAxisTickPositionsProperty =
  93. DependencyProperty.Register("HorizontalAxisTickPositions", typeof(ObservableCollection<double>), typeof(ChartLines), new UIPropertyMetadata(null));
  94. public double TickMarksLength
  95. {
  96. get { return (double)GetValue(TickMarksLengthProperty); }
  97. set { SetValue(TickMarksLengthProperty, value); }
  98. }
  99. // Using a DependencyProperty as the backing store for TickMarksLength. This enables animation, styling, binding, etc...
  100. public static readonly DependencyProperty TickMarksLengthProperty =
  101. DependencyProperty.Register("TickMarksLength", typeof(double), typeof(ChartLines), new UIPropertyMetadata(8.0));
  102. public bool DrawVerticalAxisTickMarks
  103. {
  104. get { return (bool)GetValue(DrawVerticalAxisTickMarksProperty); }
  105. set { SetValue(DrawVerticalAxisTickMarksProperty, value); }
  106. }
  107. // Using a DependencyProperty as the backing store for DrawVerticalAxisTickMarks. This enables animation, styling, binding, etc...
  108. public static readonly DependencyProperty DrawVerticalAxisTickMarksProperty =
  109. DependencyProperty.Register("DrawVerticalAxisTickMarks", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
  110. public bool DrawVerticalAxisReferenceLines
  111. {
  112. get { return (bool)GetValue(DrawVerticalAxisReferenceLinesProperty); }
  113. set { SetValue(DrawVerticalAxisReferenceLinesProperty, value); }
  114. }
  115. // Using a DependencyProperty as the backing store for DrawVerticalAxisReferenceLines. This enables animation, styling, binding, etc...
  116. public static readonly DependencyProperty DrawVerticalAxisReferenceLinesProperty =
  117. DependencyProperty.Register("DrawVerticalAxisReferenceLines", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
  118. public bool DrawHorizontalAxisTickMarks
  119. {
  120. get { return (bool)GetValue(DrawHorizontalAxisTickMarksProperty); }
  121. set { SetValue(DrawHorizontalAxisTickMarksProperty, value); }
  122. }
  123. // Using a DependencyProperty as the backing store for DrawHorizontalAxisTickMarks. This enables animation, styling, binding, etc...
  124. public static readonly DependencyProperty DrawHorizontalAxisTickMarksProperty =
  125. DependencyProperty.Register("DrawHorizontalAxisTickMarks", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
  126. public bool DrawHorizontalAxisReferenceLines
  127. {
  128. get { return (bool)GetValue(DrawHorizontalAxisReferenceLinesProperty); }
  129. set { SetValue(DrawHorizontalAxisReferenceLinesProperty, value); }
  130. }
  131. // Using a DependencyProperty as the backing store for DrawHorizontalAxisReferenceLines. This enables animation, styling, binding, etc...
  132. public static readonly DependencyProperty DrawHorizontalAxisReferenceLinesProperty =
  133. DependencyProperty.Register("DrawHorizontalAxisReferenceLines", typeof(bool), typeof(ChartLines), new UIPropertyMetadata(true));
  134. public double HorizontalAxis
  135. {
  136. get { return (double)GetValue(HorizontalAxisProperty); }
  137. set { SetValue(HorizontalAxisProperty, value); }
  138. }
  139. // Using a DependencyProperty as the backing store for HorizontalAxis. This enables animation, styling, binding, etc...
  140. public static readonly DependencyProperty HorizontalAxisProperty =
  141. DependencyProperty.Register("HorizontalAxis", typeof(double), typeof(ChartLines), new UIPropertyMetadata(0.0));
  142. public double VerticalAxis
  143. {
  144. get { return (double)GetValue(VerticalAxisProperty); }
  145. set { SetValue(VerticalAxisProperty, value); }
  146. }
  147. // Using a DependencyProperty as the backing store for VerticalAxis. This enables animation, styling, binding, etc...
  148. public static readonly DependencyProperty VerticalAxisProperty =
  149. DependencyProperty.Register("VerticalAxis", typeof(double), typeof(ChartLines), new UIPropertyMetadata(0.0));
  150. }
  151. }