/// /// Copyright (c) 2018 Sensus Slovensko a.s. /// using System; using System.Drawing; using System.IO; using System.Windows.Forms; using TBF.Resources; using TBF.UiBridge; using GraphLib; using log4net; namespace TBF.Screens { public partial class GraphsTabPageCtrl : UserControl { static readonly ILog log = LogManager.GetLogger(typeof(GraphsTabPageCtrl)); static readonly GraphInfo[] SupportedGraphs = new GraphInfo[] { new GraphInfo { FileName = "Flow", Caption = string.Format("Q [m3/h]", Strings.Flow) }, new GraphInfo { FileName = "Pressure up", Caption = string.Format("Pr up [bar]", Strings.Pressure) }, new GraphInfo { FileName = "Pressure down", Caption = string.Format("Pr dw [bar]", Strings.Pressure) }, new GraphInfo { FileName = "Temperature up", Caption = string.Format("T up [°C]", Strings.Temperature) }, new GraphInfo { FileName = "Temperature down", Caption = string.Format("T dw [°C]", Strings.Temperature) }, new GraphInfo { FileName = "Temperature div", Caption = string.Format("T di [°C]", Strings.Temperature) }, }; PlotterGraphPaneEx.LayoutMode layoutMode; CheckBox[] checkBoxes; string selectedGraphsPath; int selectedBatchNr; string selectedTestName; public GraphsTabPageCtrl() { InitializeComponent(); /// checkBoxes array must be initialized before other actions checkBoxes = new CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6 }; Localize(); UpdateCheckBoxesVisibility(); checkBox1.Checked = Program.LocalSettings.Graph1_On; checkBox2.Checked = Program.LocalSettings.Graph2_On; checkBox3.Checked = Program.LocalSettings.Graph3_On; checkBox4.Checked = Program.LocalSettings.Graph4_On; checkBox5.Checked = Program.LocalSettings.Graph5_On; checkBox6.Checked = Program.LocalSettings.Graph6_On; layoutMode = Program.LocalSettings.GraphsLayoutMode; layoutModeComboBox.Text = layoutMode.ToString(); for (PlotterGraphPaneEx.LayoutMode mode = 0; mode <= PlotterGraphPaneEx.LayoutMode.TILES_HOR; mode++) { layoutModeComboBox.Items.Add(mode.ToString()); } UpdateTestsHistory(testsHistoryTreeView); selectedGraphsPath = null; //Bridge.ProcessDataHandler += delegate(object sender, ProcessDataEventArgs args) //{ // if (InvokeRequired) // { // Invoke(new EventHandler(OnProcessData), sender, args); // } // else OnProcessData(sender, args); //}; } //void OnProcessData(object sender, ProcessDataEventArgs args) //{ // if (!paused && isRunning == true) // { // try // { // gPane.starting_idx += Math.Min(RichTextBoxFinds.BenchControl.StateMachine.; // UpdateScrollBar(); // gPane.Invalidate(); // } // catch { } // } //} void Localize() { refreshButton.Text = Strings.Refresh; for (int i = 0; i < checkBoxes.Length; i++) { if (i < SupportedGraphs.Length) { checkBoxes[i].Text = SupportedGraphs[i].Caption; } } } void UpdateCheckBoxesVisibility() { for (int i = 0; i < checkBoxes.Length; i++) { checkBoxes[i].Visible = (i < SupportedGraphs.Length); } } private void refreshButton_Click(object sender, EventArgs e) { UpdateTestsHistory(testsHistoryTreeView); } void UpdateTestsHistory(TreeView treeView) { try { string[] batches = Directory.GetDirectories(Program.GraphsDir); treeView.Nodes.Clear(); for (int i = batches.Length - 1; i >= Math.Max(0, batches.Length - 20); i--) { string b = batches[i]; string bstr = Path.GetFileName(b); if ((TBF.BenchControl.Sequences.ProcessData.BatchRslts != null) && bstr.Equals(TBF.BenchControl.Sequences.ProcessData.BatchRslts.Batch.BatchNr.ToString())) { bstr = Strings.current; } TreeNode node = new TreeNode(bstr); node.Tag = null; treeView.Nodes.Add(node); string[] tests = Directory.GetDirectories(b); foreach (var t in tests) { string testNameStr = Path.GetFileName(t); TreeNode subnode = new TreeNode(testNameStr); node.Nodes.Add(subnode); string[] repetitions = Directory.GetDirectories(t); if (repetitions.Length == 1) { subnode.Tag = string.Format("{0}~{1}~{2}", repetitions[0], bstr, testNameStr); } else if (repetitions.Length > 1) { subnode.Tag = null; foreach (var r in repetitions) { string repetStr = Path.GetFileName(r); TreeNode subsubnode = new TreeNode(repetStr); subsubnode.Tag = string.Format("{0}~{1}~{2}/{3}", r, bstr, testNameStr, repetStr); subnode.Nodes.Add(subsubnode); } } else { subnode.Tag = null; } } } } catch (Exception exc) { log.ErrorFormat("UpdateTestsHistory() failed: {0}", exc.Message); } } private void testsHistoryTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Node.Tag is string) { string[] fields = (e.Node.Tag as string).Split(new char[]{'~'}); selectedGraphsPath = fields[0]; selectedBatchNr = int.Parse(fields[1]); selectedTestName = fields[2]; ShowGraphs(plotterDisplayEx, selectedGraphsPath, selectedBatchNr, selectedTestName); } } void ShowGraphs(PlotterDisplayEx display, string path, int batchNr, string testName) { Color[] colors = { Color.DarkRed, Color.DarkSlateGray, Color.DarkCyan, Color.DarkGreen, Color.DarkBlue , Color.DarkMagenta, Color.DeepPink }; float[] gridY = { 0.0002f, 0.0005f, 0.001f, 0.002f, 0.005f, 0.01f, 0.02f, 0.05f, 0.1f, 0.2f, 0.5f, 1, 2, 5, 10, 20, 50, 100, 200, 500 }; this.SuspendLayout(); display.Smoothing = System.Drawing.Drawing2D.SmoothingMode.None; display.SetDisplayRangeX(0, 300); display.PanelLayout = layoutMode; display.BackgroundColorTop = Color.White; display.BackgroundColorBot = Color.White; display.SolidGridColor = Color.LightGray; display.DashedGridColor = Color.LightGray; display.DataSources.Clear(); bool first = true; for (int i = 0; i < checkBoxes.Length; i++) { if (checkBoxes[i].Checked && SupportedGraphs.Length > i) { DataSource dataSrc = new DataSource(); float ymin; float ymax; int samplesCount = dataSrc.LoadSamples(Path.Combine(path, SupportedGraphs[i].FileName), out ymin, out ymax); /// Following section of code makes sure graps 1, 2 and graphs 3, 4, 5 use the same scale (Y-axis) DataSource dummyDS = new DataSource(); float y1, y2, y3, y4; switch (i) { case 0: default: break; case 1: dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[2].FileName), out y1, out y2); if (y1 < ymin) ymin = y1; if (y2 > ymax) ymax = y2; break; case 2: dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[1].FileName), out y1, out y2); if (y1 < ymin) ymin = y1; if (y2 > ymax) ymax = y2; break; case 3: dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[4].FileName), out y1, out y2); dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[5].FileName), out y3, out y4); if (y1 < ymin) ymin = y1; if (y2 > ymax) ymax = y2; if (y3 < ymin) ymin = y3; if (y4 > ymax) ymax = y4; break; case 4: dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[3].FileName), out y1, out y2); dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[5].FileName), out y3, out y4); if (y1 < ymin) ymin = y1; if (y2 > ymax) ymax = y2; if (y3 < ymin) ymin = y3; if (y4 > ymax) ymax = y4; break; case 5: dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[3].FileName), out y1, out y2); dummyDS.LoadSamples(Path.Combine(path, SupportedGraphs[4].FileName), out y3, out y4); if (y1 < ymin) ymin = y1; if (y2 > ymax) ymax = y2; if (y3 < ymin) ymin = y3; if (y4 > ymax) ymax = y4; break; } if (first) { first = false; dataSrc.Name = string.Format("{0} {1} {2}", batchNr, testName, SupportedGraphs[i].Caption); } else { dataSrc.Name = SupportedGraphs[i].Caption; } dataSrc.OnRenderXAxisLabel += RenderXLabel; dataSrc.OnRenderYAxisLabel = RenderYLabel; dataSrc.AutoScaleY = false; dataSrc.SetDisplayRangeY(ymin * 0.9f, ymax * 1.1f); float d = ymax * 1.1f - ymin * 0.9f; int j = gridY.Length - 1; while ((2 * gridY[j] > d) && (j > 0)) j--; dataSrc.SetGridDistanceY(gridY[j]); dataSrc.GraphColor = colors[i % colors.Length]; display.DataSources.Add(dataSrc); } } this.ResumeLayout(); display.Refresh(); } private string RenderXLabel(DataSource s, int idx) { if (s.AutoScaleX) { //if (idx % 2 == 0) return ((int)(s.Samples[idx].X)).ToString(); //else // return string.Empty; } else { return ((int)(s.Samples[idx].X)).ToString(); } } private string RenderYLabel(DataSource s, float value) { return string.Format("{0:0.0}", value); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph1_On = checkBox1.Checked; AnyCBChanged(); } private void checkBox2_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph2_On = checkBox2.Checked; AnyCBChanged(); } private void checkBox3_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph3_On = checkBox3.Checked; AnyCBChanged(); } private void checkBox4_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph4_On = checkBox4.Checked; AnyCBChanged(); } private void checkBox5_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph5_On = checkBox5.Checked; AnyCBChanged(); } private void checkBox6_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph6_On = checkBox6.Checked; AnyCBChanged(); } void AnyCBChanged() { if (selectedGraphsPath != null) { ShowGraphs(plotterDisplayEx, selectedGraphsPath, selectedBatchNr, selectedTestName); } } private void layoutModeComboBox_SelectedIndexChanged(object sender, EventArgs e) { for (PlotterGraphPaneEx.LayoutMode mode = 0; mode <= PlotterGraphPaneEx.LayoutMode.TILES_HOR; mode++) { if (layoutModeComboBox.Text.Equals(mode.ToString()) && layoutMode != mode) { Program.LocalSettings.GraphsLayoutMode = layoutMode = mode; ShowGraphs(plotterDisplayEx, selectedGraphsPath, selectedBatchNr, selectedTestName); } } } } class GraphInfo { public string FileName; /// Name of a file written y the plotter public string Caption; /// Displayed graph lable } }