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