PageRenderTime 65ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/Isolines/IsolineGraphBase.cs

#
C# | 295 lines | 202 code | 63 blank | 30 comment | 5 complexity | fd7864e03b7d869bea109e426b332d51 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Research.DynamicDataDisplay.Common.Palettes;
  6. using Microsoft.Research.DynamicDataDisplay.DataSources;
  7. using DataSource = Microsoft.Research.DynamicDataDisplay.DataSources.IDataSource2D<double>;
  8. using System.Windows;
  9. using Microsoft.Research.DynamicDataDisplay.Charts.Shapes;
  10. namespace Microsoft.Research.DynamicDataDisplay.Charts.Isolines
  11. {
  12. public abstract class IsolineGraphBase : ContentGraph
  13. {
  14. protected IsolineGraphBase() { }
  15. private IsolineCollection collection = new IsolineCollection();
  16. protected IsolineCollection Collection
  17. {
  18. get { return collection; }
  19. set { collection = value; }
  20. }
  21. private readonly IsolineBuilder isolineBuilder = new IsolineBuilder();
  22. protected IsolineBuilder IsolineBuilder
  23. {
  24. get { return isolineBuilder; }
  25. }
  26. private readonly IsolineTextAnnotater annotater = new IsolineTextAnnotater();
  27. protected IsolineTextAnnotater Annotater
  28. {
  29. get { return annotater; }
  30. }
  31. #region Properties
  32. #region IsolineCollection property
  33. public IsolineCollection IsolineCollection
  34. {
  35. get { return (IsolineCollection)GetValue(IsolineCollectionProperty); }
  36. set { SetValue(IsolineCollectionProperty, value); }
  37. }
  38. public static readonly DependencyProperty IsolineCollectionProperty = DependencyProperty.Register(
  39. "IsolineCollection",
  40. typeof(IsolineCollection),
  41. typeof(IsolineGraphBase),
  42. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
  43. #endregion // end of IsolineCollection property
  44. #region WayBeforeTextMultiplier
  45. public double WayBeforeTextMultiplier
  46. {
  47. get { return (double)GetValue(WayBeforeTextMultiplierProperty); }
  48. set { SetValue(WayBeforeTextMultiplierProperty, value); }
  49. }
  50. public static readonly DependencyProperty WayBeforeTextMultiplierProperty = DependencyProperty.Register(
  51. "WayBeforeTextCoeff",
  52. typeof(double),
  53. typeof(IsolineGraphBase),
  54. new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.Inherits, OnIsolinePropertyChanged));
  55. #endregion // end of WayBeforeTextCoeff
  56. private static void OnIsolinePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  57. {
  58. // todo do smth here
  59. }
  60. #region Palette property
  61. public IPalette Palette
  62. {
  63. get { return (IPalette)GetValue(PaletteProperty); }
  64. set { SetValue(PaletteProperty, value); }
  65. }
  66. public static readonly DependencyProperty PaletteProperty = DependencyProperty.Register(
  67. "Palette",
  68. typeof(IPalette),
  69. typeof(IsolineGraphBase),
  70. new FrameworkPropertyMetadata(new HsbPalette(), FrameworkPropertyMetadataOptions.Inherits, OnIsolinePropertyChanged), ValidatePalette);
  71. private static bool ValidatePalette(object value)
  72. {
  73. return value != null;
  74. }
  75. #endregion // end of Palette property
  76. #region DataSource property
  77. public DataSource DataSource
  78. {
  79. get { return (DataSource)GetValue(DataSourceProperty); }
  80. set { SetValue(DataSourceProperty, value); }
  81. }
  82. public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register(
  83. "DataSource",
  84. typeof(DataSource),
  85. typeof(IsolineGraphBase),
  86. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, OnDataSourceChanged));
  87. private static void OnDataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  88. {
  89. IsolineGraphBase owner = (IsolineGraphBase)d;
  90. owner.OnDataSourceChanged((DataSource)e.OldValue, (DataSource)e.NewValue);
  91. }
  92. protected virtual void OnDataSourceChanged(IDataSource2D<double> prevDataSource, IDataSource2D<double> currDataSource)
  93. {
  94. if (prevDataSource != null)
  95. prevDataSource.Changed -= OnDataSourceChanged;
  96. if (currDataSource != null)
  97. currDataSource.Changed += OnDataSourceChanged;
  98. UpdateDataSource();
  99. CreateUIRepresentation();
  100. RaiseEvent(new RoutedEventArgs(BackgroundRenderer.UpdateRequested));
  101. }
  102. #endregion // end of DataSource property
  103. #region DrawLabels property
  104. public bool DrawLabels
  105. {
  106. get { return (bool)GetValue(DrawLabelsProperty); }
  107. set { SetValue(DrawLabelsProperty, value); }
  108. }
  109. public static readonly DependencyProperty DrawLabelsProperty = DependencyProperty.Register(
  110. "DrawLabels",
  111. typeof(bool),
  112. typeof(IsolineGraphBase),
  113. new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.Inherits, OnIsolinePropertyChanged));
  114. #endregion // end of DrawLabels property
  115. #region LabelStringFormat
  116. public string LabelStringFormat
  117. {
  118. get { return (string)GetValue(LabelStringFormatProperty); }
  119. set { SetValue(LabelStringFormatProperty, value); }
  120. }
  121. public static readonly DependencyProperty LabelStringFormatProperty = DependencyProperty.Register(
  122. "LabelStringFormat",
  123. typeof(string),
  124. typeof(IsolineGraphBase),
  125. new FrameworkPropertyMetadata("F", FrameworkPropertyMetadataOptions.Inherits, OnIsolinePropertyChanged));
  126. #endregion // end of LabelStringFormat
  127. #region UseBezierCurves
  128. public bool UseBezierCurves
  129. {
  130. get { return (bool)GetValue(UseBezierCurvesProperty); }
  131. set { SetValue(UseBezierCurvesProperty, value); }
  132. }
  133. public static readonly DependencyProperty UseBezierCurvesProperty = DependencyProperty.Register(
  134. "UseBezierCurves",
  135. typeof(bool),
  136. typeof(IsolineGraphBase),
  137. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
  138. #endregion // end of UseBezierCurves
  139. public double LabelsScaling
  140. {
  141. get { return (double)GetValue(LabelsScalingProperty); }
  142. set { SetValue(LabelsScalingProperty, value); }
  143. }
  144. public static readonly DependencyProperty LabelsScalingProperty = DependencyProperty.Register(
  145. "LabelsScaling",
  146. typeof(double),
  147. typeof(IsolineGraphBase),
  148. new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.Inherits));
  149. #endregion // end of Properties
  150. #region DataSource
  151. //private DataSource dataSource = null;
  152. ///// <summary>
  153. ///// Gets or sets the data source.
  154. ///// </summary>
  155. ///// <value>The data source.</value>
  156. //public DataSource DataSource
  157. //{
  158. // get { return dataSource; }
  159. // set
  160. // {
  161. // if (dataSource != value)
  162. // {
  163. // DetachDataSource(dataSource);
  164. // dataSource = value;
  165. // AttachDataSource(dataSource);
  166. // UpdateDataSource();
  167. // }
  168. // }
  169. //}
  170. #region MissingValue property
  171. public double MissingValue
  172. {
  173. get { return (double)GetValue(MissingValueProperty); }
  174. set { SetValue(MissingValueProperty, value); }
  175. }
  176. public static readonly DependencyProperty MissingValueProperty = DependencyProperty.Register(
  177. "MissingValue",
  178. typeof(double),
  179. typeof(IsolineGraphBase),
  180. new FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.Inherits, OnMissingValueChanged));
  181. private static void OnMissingValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  182. {
  183. IsolineGraphBase owner = (IsolineGraphBase)d;
  184. owner.UpdateDataSource();
  185. }
  186. #endregion // end of MissineValue property
  187. public void SetDataSource(DataSource dataSource, double missingValue)
  188. {
  189. DataSource = dataSource;
  190. MissingValue = missingValue;
  191. UpdateDataSource();
  192. }
  193. /// <summary>
  194. /// This method is called when data source changes.
  195. /// </summary>
  196. protected virtual void UpdateDataSource()
  197. {
  198. }
  199. protected virtual void CreateUIRepresentation() { }
  200. protected virtual void OnDataSourceChanged(object sender, EventArgs e)
  201. {
  202. UpdateDataSource();
  203. }
  204. #endregion
  205. #region StrokeThickness
  206. /// <summary>
  207. /// Gets or sets thickness of isoline lines.
  208. /// </summary>
  209. /// <value>The stroke thickness.</value>
  210. public double StrokeThickness
  211. {
  212. get { return (double)GetValue(StrokeThicknessProperty); }
  213. set { SetValue(StrokeThicknessProperty, value); }
  214. }
  215. /// <summary>
  216. /// Identifies the StrokeThickness dependency property.
  217. /// </summary>
  218. public static readonly DependencyProperty StrokeThicknessProperty =
  219. DependencyProperty.Register(
  220. "StrokeThickness",
  221. typeof(double),
  222. typeof(IsolineGraphBase),
  223. new FrameworkPropertyMetadata(2.0, OnLineThicknessChanged));
  224. private static void OnLineThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  225. {
  226. IsolineGraphBase graph = (IsolineGraphBase)d;
  227. graph.OnLineThicknessChanged();
  228. }
  229. protected virtual void OnLineThicknessChanged() { }
  230. #endregion
  231. }
  232. }