using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.Generic; using Visifire.Charts; using System.Reflection; using System.Collections; using Visifire.Commons; namespace Impulse.AttachedProperties { public class DataSeriesBinding { public static readonly DependencyProperty ChartDataProperty = DependencyProperty.RegisterAttached( "ChartData", typeof(IEnumerable), typeof(DataSeriesBinding), new PropertyMetadata(null, new PropertyChangedCallback(DataSeriesPropertyChanged) ) ); public static readonly DependencyProperty XValuePropertyNameProperty = DependencyProperty.RegisterAttached( "XValuePropertyName", typeof(string), typeof(DataSeriesBinding), new PropertyMetadata(null, new PropertyChangedCallback(DataSeriesPropertyChanged) ) ); public static readonly DependencyProperty YValuePropertyNameProperty = DependencyProperty.RegisterAttached( "YValuePropertyName", typeof(string), typeof(DataSeriesBinding), new PropertyMetadata(null, new PropertyChangedCallback(DataSeriesPropertyChanged) ) ); public static readonly DependencyProperty ChartTypeProperty = DependencyProperty.RegisterAttached( "ChartType", typeof(string), typeof(DataSeriesBinding), new PropertyMetadata(null, new PropertyChangedCallback(DataSeriesPropertyChanged) ) ); #region Get/Set public static void SetChartType(DependencyObject d, string value) { d.SetValue(ChartTypeProperty, value); } public static string GetChartType(DependencyObject d) { return d.GetValue(ChartTypeProperty) as string; } public static void SetChartData(DependencyObject d, ICollection value) { d.SetValue(ChartDataProperty, value); } public static IEnumerable GetChartData(DependencyObject d) { return d.GetValue(ChartDataProperty) as IEnumerable; } public static void SetXValuePropertyName(DependencyObject d, string value) { d.SetValue(XValuePropertyNameProperty, value); } public static string GetXValuePropertyName(DependencyObject d) { return d.GetValue(XValuePropertyNameProperty) as string; } public static void SetYValuePropertyName(DependencyObject d, string value) { d.SetValue(YValuePropertyNameProperty, value); } public static string GetYValuePropertyName(DependencyObject d) { return d.GetValue(YValuePropertyNameProperty) as string; } #endregion private static void DataSeriesPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Chart chart = d as Chart; if (CheckAllPropertiesSet(chart)) { DisplayChartData(chart); } } private static bool CheckAllPropertiesSet(Chart chart) { return chart != null && chart.GetValue(ChartDataProperty) != null && chart.GetValue(XValuePropertyNameProperty) != null && chart.GetValue(YValuePropertyNameProperty) != null && chart.GetValue(ChartTypeProperty) != null; } private static void DisplayChartData(Chart chart) { DataSeries dataSeries; string xValuePropertyName = GetXValuePropertyName(chart); string yValuePropertyName = GetYValuePropertyName(chart); IEnumerable dataPointsCollection = GetChartData(chart); RenderAs chartType = (RenderAs) Enum.Parse(typeof(RenderAs), GetChartType(chart), true); chart.Series.Clear(); dataSeries = BuildDataSeries(dataPointsCollection, chartType, xValuePropertyName, yValuePropertyName, false, MarkerTypes.Circle); dataSeries.Color = new SolidColorBrush(Colors.Red); dataSeries.LineThickness = 5; chart.Series.Add(dataSeries); } private static DataSeries BuildDataSeries(IEnumerable dataSource, RenderAs renderAs, string xValuePropertyName, string yValuePropertyName, bool showInLegend, MarkerTypes markerType) { object xValue, yValue; DataPoint tempDataPoint; IEnumerator dataEnumerator = dataSource.GetEnumerator(); DataSeries dataSeries = new DataSeries(); PropertyInfo xValuePropertyAccessor = null, yValuePropertyAccessor = null; dataSeries.RenderAs = renderAs; dataSeries.ShowInLegend = showInLegend; dataSeries.MarkerType = markerType; dataSeries.MarkerColor = new SolidColorBrush(Colors.Red); dataSeries.MarkerSize = 5; while (dataEnumerator.MoveNext()) { if (xValuePropertyAccessor == null) { xValuePropertyAccessor = dataEnumerator.Current.GetType().GetProperty(xValuePropertyName); } if (yValuePropertyAccessor == null) { yValuePropertyAccessor = dataEnumerator.Current.GetType().GetProperty(yValuePropertyName); } xValue = xValuePropertyAccessor.GetValue(dataEnumerator.Current, null); yValue = yValuePropertyAccessor.GetValue(dataEnumerator.Current, null); tempDataPoint = new DataPoint { AxisXLabel = xValue.ToString() }; if (yValue != null) { tempDataPoint.YValue = double.Parse(yValue.ToString()); } dataSeries.DataPoints.Add(tempDataPoint); } return dataSeries; } } }