Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. using Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Windows.Forms.DataVisualization.Charting;
  12. using TrendingGUI.CoreReference;
  13.  
  14. namespace TrendingGUI
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private CoreReference.ITrending proxy;
  19. public static Dictionary<string, double[]> chartSeries;
  20.  
  21. public Form1(CoreReference.ITrending proxy)
  22. {
  23.  
  24. chartSeries = new Dictionary<string, double[]>();
  25.  
  26. InitializeComponent();
  27. this.proxy = proxy;
  28. trendingChart.ChartAreas[0].AxisX.Title = "Seconds";
  29. trendingChart.ChartAreas[0].AxisX.TitleFont = new Font(new FontFamily("Arial"), 12);
  30. trendingChart.ChartAreas[0].AxisY.Title = "Value";
  31. trendingChart.ChartAreas[0].AxisY.TitleFont = new Font(new FontFamily("Arial"), 12);
  32.  
  33. var tags = proxy.getTagsForDisplay();
  34. foreach (Tag t in tags.Keys)
  35. {
  36. comboBox1.Items.Add(t.Name);
  37. }
  38.  
  39. StartDoing();
  40. }
  41.  
  42. private void History(Value[] values, string name)
  43. {
  44. int i = 0;
  45. historyChart.Series[0].Points.Clear();
  46. historyChart.Series[0].Name = name;
  47. historyChart.ChartAreas[0].AxisX.Title = "Seconds";
  48. historyChart.ChartAreas[0].AxisX.TitleFont = new Font(new FontFamily("Arial"), 12);
  49. historyChart.ChartAreas[0].AxisY.Title = "Value";
  50. historyChart.ChartAreas[0].AxisY.TitleFont = new Font(new FontFamily("Arial"), 12);
  51. historyChart.ChartAreas[0].AxisX.Interval = 1;
  52. historyChart.Series[0].Enabled = true;
  53.  
  54. foreach (var value in values.Reverse())
  55. {
  56. historyChart.Series[0].Points.AddXY(i++, value.value1);
  57. }
  58. }
  59.  
  60. public void Update(Dictionary<Tag, double> tagValues)
  61. {
  62. List<string> strings = new List<string>();
  63.  
  64. int pos = comboBox1.Text.Length;
  65. comboBox1.Items.Clear();
  66. comboBox1.Select(pos, 0);
  67.  
  68. foreach (Tag t in tagValues.Keys)
  69. {
  70. comboBox1.Items.Add(t.Name);
  71. int index = getCBIndex(t.Name);
  72.  
  73. if (index != -1)
  74. {
  75. if (!checkedListBox1.GetItemChecked(index))
  76. trendingChart.Series[t.Name].Enabled = false;
  77. else trendingChart.Series[t.Name].Enabled = true;
  78. }
  79.  
  80. if (chartSeries.ContainsKey(t.Name))
  81. {
  82. chartSeries[t.Name][chartSeries[t.Name].Length - 1] = tagValues[t];
  83. Array.Copy(chartSeries[t.Name], 1, chartSeries[t.Name], 0, chartSeries[t.Name].Length - 1);
  84. }
  85. else
  86. {
  87. trendingChart.Series.Add(t.Name);
  88. trendingChart.Series[t.Name].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
  89. double[] myArray = new double[15];
  90. for (int i = 0; i < 15; i++) myArray[i] = tagValues[t];
  91. chartSeries.Add(t.Name, myArray);
  92. checkedListBox1.Items.Add(t.Name, true);
  93. }
  94. strings.Add(t.Name);
  95. }
  96.  
  97. UpdateSeries(strings);
  98. UpdateChart();
  99. }
  100.  
  101. private int getCBIndex(string t)
  102. {
  103.  
  104. int index = 0;
  105. foreach (string p in checkedListBox1.Items)
  106. {
  107. if (p == t) return index;
  108. else index++;
  109. }
  110. return -1;
  111.  
  112. }
  113.  
  114.  
  115. private void UpdateSeries(List<string> strings)
  116. {
  117. foreach (string s in chartSeries.Keys.ToList())
  118. {
  119. if (!(strings.Contains(s)))
  120. {
  121. trendingChart.Series[s].Points.Clear();
  122. trendingChart.Series.Remove(trendingChart.Series[s]);
  123. checkedListBox1.Items.Remove(s);
  124. chartSeries.Remove(s);
  125. }
  126. }
  127. }
  128.  
  129.  
  130.  
  131. private void UpdateChart()
  132. {
  133. int i;
  134. foreach (string s in chartSeries.Keys)
  135. {
  136.  
  137. trendingChart.Series[s].Points.Clear();
  138. for (i = 0; i < chartSeries[s].Length - 1; i++)
  139. {
  140. trendingChart.Series[s].Points.AddXY(i, chartSeries[s][i]);
  141. }
  142. }
  143. }
  144.  
  145. private void button1_Click(object sender, EventArgs e)
  146. {
  147. History(proxy.getValuesFromDatabaseGUI(comboBox1.Text), comboBox1.Text);
  148. }
  149.  
  150. private void StartDoing()
  151. {
  152. timer1.Start();
  153.  
  154. }
  155.  
  156. private void timer1_Tick(object sender, EventArgs e)
  157. {
  158. Update(proxy.getTagsForDisplay());
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement