/Main/src/DynamicDataDisplay/GenericChartPlotter.cs
C# | 121 lines | 102 code | 19 blank | 0 comment | 4 complexity | 34dfc646fe5b04c09a85fd09b61aa5f3 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using Microsoft.Research.DynamicDataDisplay.Charts; 6using System.Windows; 7using Microsoft.Research.DynamicDataDisplay.Common; 8 9namespace Microsoft.Research.DynamicDataDisplay 10{ 11 public sealed class GenericChartPlotter<THorizontal, TVertical> 12 { 13 private readonly AxisBase<THorizontal> horizontalAxis; 14 public AxisBase<THorizontal> HorizontalAxis 15 { 16 get { return horizontalAxis; } 17 } 18 19 private readonly AxisBase<TVertical> verticalAxis; 20 public AxisBase<TVertical> VerticalAxis 21 { 22 get { return verticalAxis; } 23 } 24 25 private readonly ChartPlotter plotter; 26 public ChartPlotter Plotter 27 { 28 get { return plotter; } 29 } 30 31 public Func<THorizontal, double> HorizontalToDoubleConverter 32 { 33 get { return horizontalAxis.ConvertToDouble; } 34 } 35 36 public Func<double, THorizontal> DoubleToHorizontalConverter 37 { 38 get { return horizontalAxis.ConvertFromDouble; } 39 } 40 41 public Func<TVertical, double> VerticalToDoubleConverter 42 { 43 get { return verticalAxis.ConvertToDouble; } 44 } 45 46 public Func<double, TVertical> DoubleToVerticalConverter 47 { 48 get { return verticalAxis.ConvertFromDouble; } 49 } 50 51 internal GenericChartPlotter(ChartPlotter plotter) : this(plotter, plotter.MainHorizontalAxis as AxisBase<THorizontal>, plotter.MainVerticalAxis as AxisBase<TVertical>) { } 52 53 internal GenericChartPlotter(ChartPlotter plotter, AxisBase<THorizontal> horizontalAxis, AxisBase<TVertical> verticalAxis) 54 { 55 if (horizontalAxis == null) 56 throw new ArgumentNullException(Strings.Exceptions.PlotterMainHorizontalAxisShouldNotBeNull); 57 if (verticalAxis == null) 58 throw new ArgumentNullException(Strings.Exceptions.PlotterMainVerticalAxisShouldNotBeNull); 59 60 this.horizontalAxis = horizontalAxis; 61 this.verticalAxis = verticalAxis; 62 63 this.plotter = plotter; 64 } 65 66 public GenericRect<THorizontal, TVertical> ViewportRect 67 { 68 get 69 { 70 DataRect viewportRect = plotter.Viewport.Visible; 71 return CreateGenericRect(viewportRect); 72 } 73 set 74 { 75 DataRect viewportRect = CreateRect(value); 76 plotter.Viewport.Visible = viewportRect; 77 } 78 } 79 80 public GenericRect<THorizontal, TVertical> DataRect 81 { 82 get 83 { 84 DataRect dataRect = plotter.Viewport.Visible.ViewportToData(plotter.Viewport.Transform); 85 return CreateGenericRect(dataRect); 86 } 87 set 88 { 89 DataRect dataRect = CreateRect(value); 90 plotter.Viewport.Visible = dataRect.DataToViewport(plotter.Viewport.Transform); 91 } 92 } 93 94 private DataRect CreateRect(GenericRect<THorizontal, TVertical> value) 95 { 96 double xMin = HorizontalToDoubleConverter(value.XMin); 97 double xMax = HorizontalToDoubleConverter(value.XMax); 98 double yMin = VerticalToDoubleConverter(value.YMin); 99 double yMax = VerticalToDoubleConverter(value.YMax); 100 101 return new DataRect(new Point(xMin, yMin), new Point(xMax, yMax)); 102 } 103 104 private GenericRect<THorizontal, TVertical> CreateGenericRect(DataRect rect) 105 { 106 double xMin = rect.XMin; 107 double xMax = rect.XMax; 108 double yMin = rect.YMin; 109 double yMax = rect.YMax; 110 111 GenericRect<THorizontal, TVertical> res = new GenericRect<THorizontal, TVertical>( 112 DoubleToHorizontalConverter(xMin), 113 DoubleToVerticalConverter(yMin), 114 DoubleToHorizontalConverter(xMax), 115 DoubleToVerticalConverter(yMax)); 116 117 return res; 118 } 119 120 } 121}