/// /// Copyright (c) 2018 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; using TBF.Resources; using TBF.UiBridge; using GraphLib; using log4net; namespace TBF.UI.Graphs { 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]") }, new GraphInfo { FileName = "Pressure up", Caption = string.Format("Pr up [bar]") }, new GraphInfo { FileName = "Pressure down", Caption = string.Format("Pr dw [bar]") }, new GraphInfo { FileName = "Temperature up", Caption = string.Format("T up [°C]") }, new GraphInfo { FileName = "Temperature down", Caption = string.Format("T dw [°C]") }, new GraphInfo { FileName = "Temperature div", Caption = string.Format("T di [°C]") }, new GraphInfo { FileName = "Div start", Caption = string.Format("Div start") }, new GraphInfo { FileName = "Div end", Caption = string.Format("Div end") }, }; 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, checkBox7, checkBox8 }; Localize(); UpdateCheckBoxesVisibility(); layoutMode = GraphLib.PlotterGraphPaneEx.LayoutMode.NORMAL; /// Default value if (Program.LocalSettings != null) { 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; checkBox7.Checked = Program.LocalSettings.Graph7_On; checkBox8.Checked = Program.LocalSettings.Graph8_On; layoutMode = Program.LocalSettings.GraphsLayoutMode; /// Override the default value } 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); int[] batchNrs = new int[batches.Length]; int i = 0; foreach (var b in batches) { int nr; if (int.TryParse(Path.GetFileName(b), out nr)) { batchNrs[i++] = nr; } } Array.Sort(batchNrs); treeView.Nodes.Clear(); for (int j = batchNrs.Length - 1; j >= Math.Max(0, batchNrs.Length - 40); j--) { string batchNrSstr = batchNrs[j].ToString(); string b = Path.Combine(Program.GraphsDir, batchNrSstr); string rootNodeName = batchNrSstr; if ((TBF.BenchControl.Sequences.ProcessData.BatchRslts != null) && rootNodeName.Equals(TBF.BenchControl.Sequences.ProcessData.BatchRslts.Batch.BatchNr.ToString())) { rootNodeName = Strings.current; } TreeNode node = new TreeNode(rootNodeName); 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], batchNrSstr, 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, batchNrSstr, 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) { if (path == null) return; /// No batch selected yet Color[] colors = { Color.DarkRed, Color.DarkSlateGray, Color.DarkCyan, Color.DarkGreen, Color.DarkBlue , Color.DarkMagenta, Color.DeepPink, Color.DarkGreen }; 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 value.ToString(Config.Utils.SignificantDigitsToFmt(value, 2)); } 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(); } private void checkBox7_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph7_On = checkBox7.Checked; AnyCBChanged(); } private void checkBox8_CheckedChanged(object sender, EventArgs e) { Program.LocalSettings.Graph8_On = checkBox8.Checked; AnyCBChanged(); } void AnyCBChanged() { 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 } }