PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/DevSamples/Log10Axis/Window1.xaml.cs

#
C# | 169 lines | 134 code | 29 blank | 6 comment | 10 complexity | 8f1ec8ab4813267e956b82cf400e36e2 MD5 | raw file
  1using System.Windows;
  2using System.Collections.Generic;
  3using System.Windows.Controls;
  4using System.Windows.Media;
  5using Microsoft.Research.DynamicDataDisplay.DataSources;
  6using Microsoft.Research.DynamicDataDisplay;
  7using Microsoft.Research.DynamicDataDisplay.Charts;
  8using Microsoft.Research.DynamicDataDisplay.Charts.Axes.Numeric;
  9
 10namespace Log10Axis
 11{
 12    /// <summary>
 13    /// Interaction logic for D3LineChart.xaml
 14    /// </summary>
 15	public partial class Window1 : Window
 16	{
 17		private AxisInfo _xAxisInfo;
 18		private IList<AxisInfo> _yAxisInfoList;
 19		private IList<AxisInfo> _yAxisInfoSecList;
 20
 21		private string _chartTitle = string.Empty;
 22		public string ChartTitle
 23		{
 24			get { return _chartTitle; }
 25			set { _chartTitle = value; }
 26		}
 27
 28		public Window1()
 29		{
 30			InitializeComponent();
 31		}
 32
 33		public Window1(AxisInfo xAxisInfo, IList<AxisInfo> yAxisInfoList)
 34			: this()
 35		{
 36			_xAxisInfo = xAxisInfo;
 37			_yAxisInfoList = yAxisInfoList;
 38
 39			if (_xAxisInfo != null && _yAxisInfoList != null)
 40			{
 41				DrawPlots();
 42			}
 43		}
 44
 45		public Window1(AxisInfo xAxisInfo, IList<AxisInfo> yAxisInfoList, IList<AxisInfo> yAxisInfoSecList)
 46		{
 47			InitializeComponent();
 48			_xAxisInfo = xAxisInfo;
 49			_yAxisInfoList = yAxisInfoList;
 50			_yAxisInfoSecList = yAxisInfoSecList;
 51
 52			if (_xAxisInfo != null && _yAxisInfoList != null)
 53			{
 54				DrawPlots();
 55			}
 56		}
 57
 58		private void DrawPlots()
 59		{
 60			HorizontalAxis xAxis = (HorizontalAxis)plotter.MainHorizontalAxis;
 61			xAxis.TicksProvider = new LogarithmNumericTicksProvider(10);
 62			xAxis.LabelProvider = new UnroundingLabelProvider();
 63			xAxis.ShowMajorLabels = true;
 64			xAxis.ShowMinorTicks = true;
 65			xAxis.SnapsToDevicePixels = true;
 66
 67			xAxis.LabelProvider.SetCustomFormatter(info => info.Tick.ToString("#.######e0"));
 68
 69			HorizontalAxisTitle HAT = new HorizontalAxisTitle()
 70			{
 71				Content = _xAxisInfo.AxisTitle
 72			};
 73
 74			plotter.MainHorizontalAxis = xAxis;
 75			plotter.Children.Add(HAT);
 76			((NumericAxis)plotter.MainHorizontalAxis).AxisControl.TicksPath.Stroke = Brushes.Blue;
 77
 78			VerticalAxis yAxis = (VerticalAxis)plotter.MainVerticalAxis;
 79			yAxis.TicksProvider = new NumericTicksProvider();
 80			yAxis.LabelProvider = new UnroundingLabelProvider();
 81			yAxis.ShowMajorLabels = true;
 82			yAxis.ShowMinorTicks = true;
 83			yAxis.SnapsToDevicePixels = true;
 84
 85
 86			VerticalAxisTitle VAT = new VerticalAxisTitle()
 87			{
 88				Content = _yAxisInfoList[0].AxisTitle
 89			};
 90
 91			plotter.MainVerticalAxis = yAxis;
 92			plotter.Children.Add(VAT);
 93			plotter.AxisGrid.DrawVerticalMinorTicks = true;
 94			plotter.AxisGrid.DrawHorizontalMinorTicks = true;
 95			plotter.MainVerticalAxis.Background = new LinearGradientBrush(Colors.White, Colors.LightGray, 90);
 96
 97			var xPoints = _xAxisInfo.AxisDataPoints.AsXDataSource();
 98
 99			// Create the main plot
100			foreach (AxisInfo yAxInfo in _yAxisInfoList)
101			{
102				var yPoints = yAxInfo.AxisDataPoints.AsYDataSource();
103
104				CompositeDataSource plot = xPoints.Join(yPoints);
105				plotter.AddLineGraph(plot, yAxInfo.PlotColor, yAxInfo.PlotLineThickness, yAxInfo.AxisLegend);
106			}
107
108
109			// add secondary y-axis plots if any exist
110			if (_yAxisInfoSecList != null)
111			{
112				InjectedPlotter innerPlotter = new InjectedPlotter();
113				innerPlotter.SetViewportBinding = false;
114				plotter.Children.Add(innerPlotter);
115
116				HorizontalAxis ax = new HorizontalAxis();
117				ax.Placement = AxisPlacement.Top;
118				ax.TicksProvider = new LogarithmNumericTicksProvider(10);
119				ax.LabelProvider = new UnroundingLabelProvider();
120				ax.LabelProvider.SetCustomFormatter(info => info.Tick.ToString("#.######e0"));
121				ax.ShowMajorLabels = true;
122				ax.ShowMinorTicks = true;
123				ax.SnapsToDevicePixels = true;
124				ax.AxisControl.TicksPath.Stroke = Brushes.Red;
125				plotter.Children.Add(ax);
126
127
128				VerticalAxis yAxisSec = new VerticalAxis()
129				{
130					TicksProvider = new NumericTicksProvider(),
131					LabelProvider = new UnroundingLabelProvider(),
132					ShowMinorTicks = true,
133					ShowMajorLabels = true,
134					SnapsToDevicePixels = true,
135					Placement = AxisPlacement.Right
136				};
137
138				VerticalAxisTitle VATsecondary = new VerticalAxisTitle()
139				{
140					Content = _yAxisInfoSecList[0].AxisTitle,
141					Placement = AxisPlacement.Right
142				};
143
144				innerPlotter.MainVerticalAxis = yAxisSec;
145				innerPlotter.Children.Add(VATsecondary);
146				innerPlotter.MainVerticalAxis.Background = new LinearGradientBrush(Colors.White, Colors.Red, 90);
147
148				foreach (AxisInfo yAxInfoSec in _yAxisInfoSecList)
149				{
150					var ySecPoints = yAxInfoSec.AxisDataPoints.AsYDataSource();
151
152					CompositeDataSource plotSec = xPoints.Join(ySecPoints);
153					/*innerP*/
154					plotter.AddLineGraph(plotSec, yAxInfoSec.PlotColor, yAxInfoSec.PlotLineThickness, yAxInfoSec.AxisLegend);
155				}
156
157			}
158		}
159	}
160
161    public class AxisInfo
162    {
163        public string AxisTitle;
164        public string AxisLegend;
165        public double[] AxisDataPoints;
166        public Color PlotColor = Colors.Black;
167        public int PlotLineThickness = 1;
168    }
169}