Guest User

Untitled

a guest
Jan 15th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. namespace MyNS
  2. {
  3.     public partial class ChartDoubleTimeControl : UserControl
  4.     {
  5.         private List<List<KeyValuePair<DateTime, double>>> m_DataCollection;
  6.         private List<SolidColorBrush> m_Palette;
  7.         private string m_LegendVisibility;
  8.  
  9.         public ChartDoubleTimeControl()
  10.         {
  11.             InitializeComponent();
  12.             DataContext = this;
  13.             m_DataCollection = null;
  14.             m_Palette = new List<SolidColorBrush>();
  15.             m_Palette.Add(Brushes.Green);
  16.             m_Palette.Add(Brushes.Red);
  17.             m_Palette.Add(Brushes.Blue);
  18.             m_Palette.Add(Brushes.Yellow);
  19.             m_Palette.Add(Brushes.Gray);
  20.             m_Palette.Add(Brushes.Orange);
  21.         }
  22.  
  23.         public string LegendVisibility
  24.         {
  25.             get { return m_LegendVisibility; }
  26.             set { m_LegendVisibility = value; }
  27.         }
  28.  
  29.         public void RefreshChart()
  30.         {
  31.             this.Dispatcher.Invoke((Action)(() =>
  32.             {
  33.                 LoadChartData();
  34.             }));
  35.         }
  36.  
  37.         public void setDataCollection(List<List<KeyValuePair<DateTime, double>>> nDataCollection, List<string> nLegend, bool isSeries)
  38.         {
  39.             m_DataCollection = nDataCollection;
  40.  
  41.             if (isSeries)
  42.             {
  43.                 for (int i = (LineChart.Series.Count - 1); i > -1; i--)
  44.                 {
  45.                     LineChart.Series.Remove(LineChart.Series[i]);
  46.                 }
  47.             }
  48.  
  49.             LineSeries mySeries = null;
  50.  
  51.             if (!isSeries)
  52.             {
  53.                 LegendVisibility = "0";
  54.             }
  55.             else
  56.             {
  57.                 LegendVisibility = "Auto";
  58.             }
  59.  
  60.             if (isSeries)
  61.             {
  62.                 for (int i = 0; i < m_DataCollection.Count; i++)
  63.                 {
  64.                     if (nLegend.Count > 0)
  65.                     {
  66.                         mySeries = new LineSeries();
  67.                         mySeries.DependentValuePath = "Value";
  68.                         mySeries.IndependentValuePath = "Key";
  69.                         mySeries.ItemsSource = m_DataCollection[i];
  70.                         mySeries.Title = nLegend[i];
  71.                         Style style = new Style { TargetType = typeof(LineDataPoint) };
  72.                         style.Setters.Add(new Setter(ColumnDataPoint.BackgroundProperty, m_Palette[i]));
  73.                         mySeries.DataPointStyle = style;
  74.  
  75.                         LineChart.Series.Add(mySeries);
  76.                     }
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 SeriesColumn.ItemsSource = m_DataCollection[0];
  82.             }
  83.         }
  84.  
  85.         public void LoadChartData()
  86.         {
  87.             MyHelper resultHelper = new MyHelper();
  88.             LineChart.Title = "Title";
  89.            
  90.            
  91.             List<List<KeyValuePair<DateTime, double>>> DataCollection = new List<List<KeyValuePair<DateTime, double>>>();
  92.             DataCollection.Add(new List<KeyValuePair<DateTime, double>>());
  93.             List<string> Legend = new List<string>();
  94.             bool isSeries = false;
  95.            
  96.             Dictionary<DateTime, double> result = resultHelper.GetPoints();
  97.             for (int i = 0; i < result.Count; i++)  
  98.                 DataCollection[0].Add(new KeyValuePair<DateTime, double>(result.ElementAt(i).Key, result.ElementAt(i).Value));
  99.             isSeries = false;
  100.             setDataCollection(DataCollection, Legend, isSeries);
  101.         }
  102.  
  103.         private void Button_Click(object sender, RoutedEventArgs e)
  104.         {
  105.             RefreshChart();
  106.         }
  107.     }
  108. }
Add Comment
Please, Sign In to add comment