PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/HorizontalAxisTitle.cs

#
C# | 111 lines | 84 code | 13 blank | 14 comment | 11 complexity | f4600176c752e96829e6f5c86312a952 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Markup;
  5. using Microsoft.Research.DynamicDataDisplay.Charts;
  6. using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
  7. namespace Microsoft.Research.DynamicDataDisplay
  8. {
  9. /// <summary>
  10. /// Represents a title of horizontal axis. Can be placed from top or bottom of Plotter.
  11. /// </summary>
  12. public class HorizontalAxisTitle : ContentControl, IPlotterElement
  13. {
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="HorizontalAxisTitle"/> class.
  16. /// </summary>
  17. public HorizontalAxisTitle()
  18. {
  19. FontSize = 16;
  20. HorizontalAlignment = HorizontalAlignment.Center;
  21. }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="HorizontalAxisTitle"/> class.
  24. /// </summary>
  25. /// <param name="content">The content.</param>
  26. public HorizontalAxisTitle(object content)
  27. : this()
  28. {
  29. Content = content;
  30. }
  31. private Plotter plotter;
  32. public Plotter Plotter
  33. {
  34. get { return plotter; }
  35. }
  36. public void OnPlotterAttached(Plotter plotter)
  37. {
  38. this.plotter = plotter;
  39. AddToPlotter();
  40. }
  41. public void OnPlotterDetaching(Plotter plotter)
  42. {
  43. RemoveFromPlotter();
  44. this.plotter = null;
  45. }
  46. private Panel GetHostPanel(Plotter plotter)
  47. {
  48. if (placement == AxisPlacement.Bottom)
  49. return plotter.BottomPanel;
  50. else
  51. return plotter.TopPanel;
  52. }
  53. private int GetInsertPosition(Panel panel)
  54. {
  55. if (placement == AxisPlacement.Bottom)
  56. return panel.Children.Count;
  57. else
  58. return 0;
  59. }
  60. private AxisPlacement placement = AxisPlacement.Bottom;
  61. /// <summary>
  62. /// Gets or sets the placement of axis title.
  63. /// </summary>
  64. /// <value>The placement.</value>
  65. public AxisPlacement Placement
  66. {
  67. get { return placement; }
  68. set
  69. {
  70. if (!value.IsBottomOrTop())
  71. throw new ArgumentException(String.Format("HorizontalAxisTitle only supports Top and Bottom values of AxisPlacement, you passed '{0}'", value), "Placement");
  72. if (placement != value)
  73. {
  74. if (plotter != null)
  75. {
  76. RemoveFromPlotter();
  77. }
  78. placement = value;
  79. if (plotter != null)
  80. {
  81. AddToPlotter();
  82. }
  83. }
  84. }
  85. }
  86. private void RemoveFromPlotter()
  87. {
  88. var oldPanel = GetHostPanel(plotter);
  89. oldPanel.Children.Remove(this);
  90. }
  91. private void AddToPlotter()
  92. {
  93. var hostPanel = GetHostPanel(plotter);
  94. var index = GetInsertPosition(hostPanel);
  95. hostPanel.Children.Insert(index, this);
  96. }
  97. }
  98. }