PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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