/// <summary> /// Make score plot series /// </summary> /// <param name="index">Data index</param> /// <param name="preSeriesName">The last series name</param> /// <param name="data">Plot data</param> /// <param name="seriesName">Series name</param> /// <returns>T=Existing, F=New creation</returns> private bool MakeScorePlotSeries(int index, string preSeriesName, SimcaData data, out string seriesName) { bool seriesExist = false; string group = data.GetGroupName(index); if (group != string.Empty) { seriesName = group; } else { seriesName = "(none)"; } try { if (!preSeriesName.Equals(seriesName)) { preSeriesName = seriesName; _chart.Series.Add(seriesName); } } catch (Exception) { seriesExist = true; } return(seriesExist); }
/// <summary> /// Set data /// </summary> /// <param name="grid">DataGridView</param> /// <param name="plotData">SimcaData</param> private void SetData(DataGridView grid, SimcaData plotData) { if (plotData.GetDataCount() > ENABLE_VIRTUAL_MODE_LINE_COUNT) { grid.VirtualMode = true; int index = (int)plotData.DataType; for (int row = 0; row < grid.RowCount; row++) { List <string> itemList = new List <string>(); itemList.Clear(); for (int col = 0; col < grid.ColumnCount; col++) { itemList.Add(plotData.GetStrValue(col, row + 1)); } _dataList[index].Add(itemList); } } else { grid.VirtualMode = false; for (int row = 0; row < grid.RowCount; row++) { for (int col = 0; col < grid.ColumnCount; col++) { grid[col, row].Value = plotData.GetStrValue(col, row + 1); } } } }
/// <summary> /// Initializes a new instance of the FormSimcaGrid class /// </summary> /// <param name="scoreData">Score plot data</param> /// <param name="loadingData">Loading plot data</param> /// <param name="splotData">S plot data</param> /// <param name="selIndex">select index</param> public FormSimcaGrid(SimcaData scoreData, SimcaData loadingData, SimcaData splotData, int selIndex) { InitializeComponent(); Update(scoreData); Update(loadingData); Update(splotData); _tabControlGrid.SelectedIndex = selIndex; }
/// <summary> /// Graph ChangeMousePos event handler /// </summary> /// <param name="sender">sender</param> /// <param name="e">event args</param> private void Graph_SelectAxisBtnClick(object sender, EventArgs e) { SimcaData data = null; UserControlSimcaGraph graph = null; switch ((TAB_ID)_tabControlPlot.SelectedIndex) { case TAB_ID.SCORE_PLOT: data = _scoreData; graph = _graphScore; break; case TAB_ID.LOADING_PLOT: data = _loadingData; graph = _graphLoading; break; case TAB_ID.S_PLOT: data = _sPlotData; graph = _graphSPlot; break; default: break; } if (data == null) { return; } FormSimcaSelectAxis dlg = new FormSimcaSelectAxis(data); if (dlg.ShowDialog(this) != DialogResult.OK) { dlg.Dispose(); return; } if ((dlg.SelectAxisIndexX != data.AxisXDataIndex) || (dlg.SelectAxisIndexY != data.AxisYDataIndex)) { data.AxisXDataIndex = dlg.SelectAxisIndexX; data.AxisYDataIndex = dlg.SelectAxisIndexY; graph.SetData(data); } }
/// <summary> /// Clear data /// </summary> /// <param name="grid">grid object</param> /// <param name="plotData">plot data</param> private void ClearData(DataGridView grid, SimcaData plotData) { grid.Rows.Clear(); grid.Columns.Clear(); int index = (int)plotData.DataType; if (_dataList[index] == null) { _dataList[index] = new List <List <string> >(); } foreach (List <string> item in _dataList[index]) { item.Clear(); } _dataList[index].Clear(); _pasteValue[index] = string.Empty; _enableApply[index] = false; _enableSave[index] = false; UpdataButton(); }
/// <summary> /// Read file /// </summary> /// <param name="data">The target SIMCA data</param> /// <param name="graph">The target graph</param> private void ReadFile(SimcaData data, UserControlSimcaGraph graph) { string formatDlgTitle = "Select {0} data file"; string formatErrMsg = string.Empty; // type string typeName = "score"; Label pathLabel = _labelScorePath; if (data == _scoreData) { typeName = "score"; pathLabel = _labelScorePath; } if (data == _loadingData) { typeName = "loding"; pathLabel = _labelLoadingPath; } if (data == _sPlotData) { typeName = "S"; pathLabel = _labelSPlotPath; } // Show file open dialog OpenFileDialog ofd = new OpenFileDialog(); ofd.FileName = string.Empty; ofd.InitialDirectory = string.Empty; ofd.Filter = "csv file(*.csv)|*.csv"; ofd.FilterIndex = 0; ofd.Title = string.Format(formatDlgTitle, typeName); ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { // Tab select _tabControlPlot.SelectedIndex = (int)data.DataType; // Grid tab select if (_formGridInstance != null) { _formGridInstance.TabSelect(data.DataType); } // Read file data SimcaData.SIMCA_READ_ERR_CODE errorCode = SimcaData.SIMCA_READ_ERR_CODE.OK; data.Clear(); try { using (TextFieldParser parser = new TextFieldParser( ofd.FileName, System.Text.Encoding.GetEncoding("Shift_JIS"))) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); while (parser.EndOfData == false) { errorCode = data.Add(parser.ReadFields(), _dbData); if (errorCode != SimcaData.SIMCA_READ_ERR_CODE.OK) { break; } } } } catch (Exception ex) { formatErrMsg = ex.Message; errorCode = SimcaData.SIMCA_READ_ERR_CODE.NG_READ; } if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.OK) { errorCode = data.CheckData(_dbData.GetSelectedMatrixIndex()); } if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_CLASS) { string workMsg = string.Format(WARN_MSG_FMT_MSG_CLASS, Path.GetFileName(ofd.FileName)); MessageBox.Show( workMsg, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); errorCode = SimcaData.SIMCA_READ_ERR_CODE.OK; } if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.OK) { graph.SetData(data); _filePath[(int)data.DataType] = ofd.FileName; LabelSetText(pathLabel, ofd.FileName); if (_formGridInstance != null) { _formGridInstance.Update(data); } return; } if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_DATA) { formatErrMsg = ERR_MSG_FMT_FILE_READ_DATA; } else if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_READ) { // formatErrMsg = ERR_MSG_FMT_READ_FILE; } else { formatErrMsg = ERR_MSG_FMT_FILE_READ_FMT; } string work = string.Format(formatErrMsg, typeName, Path.GetFileName(ofd.FileName)); work = char.ToUpper(work[0]) + work.Substring(1); MessageBox.Show( work, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); graph.SetDefaultData(); data.Clear(); if (_formGridInstance != null) { _formGridInstance.Update(data); } pathLabel.Text = string.Empty; _filePath[(int)data.DataType] = string.Empty; } }
/// <summary> /// FormSimcaGrid ApplyClick evevt handler /// </summary> /// <param name="sender">object</param> /// <param name="e">ApplyClickEventArgs</param> private void FormSimcaGrid_ApplyClick(object sender, FormSimcaGrid.ApplyClickEventArgs e) { SimcaData.SIMCA_READ_ERR_CODE errorCode = SimcaData.SIMCA_READ_ERR_CODE.OK; string typeName = "score"; Label pathLabel = _labelScorePath; UserControlSimcaGraph graph = _graphScore; SimcaData data = _scoreData; switch (e._dataType) { case SimcaData.SIMCA_DATA_TYPE.SCORE_PLOT: typeName = "score"; pathLabel = _labelScorePath; graph = _graphScore; data = _scoreData; break; case SimcaData.SIMCA_DATA_TYPE.LOADING_PLOT: typeName = "loading"; pathLabel = _labelLoadingPath; graph = _graphLoading; data = _loadingData; break; case SimcaData.SIMCA_DATA_TYPE.S_PLOT: typeName = "S"; pathLabel = _labelSPlotPath; graph = _graphSPlot; data = _sPlotData; break; default: break; } // Tab select _tabControlPlot.SelectedIndex = (int)e._dataType; data.Clear(); errorCode = data.SetPasteData(e._applyData, _dbData); pathLabel.Text = string.Empty; _filePath[(int)e._dataType] = string.Empty; string workMsg; if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_CLASS) { workMsg = string.Format(WARN_MSG_FMT_MSG_CLASS, "paste data"); MessageBox.Show( workMsg, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); errorCode = SimcaData.SIMCA_READ_ERR_CODE.OK; } if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.OK) { graph.SetData(data); //if (_formGridInstance != null) //{ // _formGridInstance.Update(data); //} return; } string formatErrMsg; if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_DATA) { formatErrMsg = ERR_MSG_FMT_FILE_READ_DATA; } else if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_READ) { formatErrMsg = ERR_MSG_FMT_READ_FILE; } else { formatErrMsg = ERR_MSG_FMT_FILE_READ_FMT; } workMsg = string.Format(formatErrMsg, typeName, "paste data"); workMsg = char.ToUpper(workMsg[0]) + workMsg.Substring(1); MessageBox.Show( workMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); graph.SetDefaultData(); data.Clear(); if (_formGridInstance != null) { _formGridInstance.Update(data); } }
/// <summary> /// Paste of clipboard data /// </summary> /// <param name="dataType">Data type</param> /// <param name="typeName">Data type name</param> private void PasteClipboardData(SimcaData.SIMCA_DATA_TYPE dataType, string typeName) { // Set wait Cursor Cursor preCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; int index = _tabControlGrid.SelectedIndex; // Data will be set if data is in a clipboard. if (Clipboard.ContainsText()) { string clipboardText = Clipboard.GetText(); SimcaData.SIMCA_READ_ERR_CODE errorCode = SimcaData.SIMCA_READ_ERR_CODE.OK; // Convert TSV to CSV string workPaste = ConvTsvToCsv(clipboardText); if (workPaste == null) { errorCode = SimcaData.SIMCA_READ_ERR_CODE.NG_FORMAT; } SimcaData tempData = new SimcaData(dataType); if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.OK) { errorCode = tempData.SetPasteData(workPaste, null); } string formatErrMsg; if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_DATA) { // NG_DATA formatErrMsg = ERR_MSG_FMT_FILE_READ_DATA; } else if (errorCode == SimcaData.SIMCA_READ_ERR_CODE.NG_FORMAT) { // NG_FORMAT formatErrMsg = ERR_MSG_FMT_FILE_READ_FMT; } else { // OK Update(tempData); _pasteValue[index] = workPaste; _enableApply[index] = true; _enableSave[index] = true; UpdataButton(); Cursor.Current = preCursor; return; } string workMsg = string.Format(formatErrMsg, typeName, "paste data"); workMsg = char.ToUpper(workMsg[0]) + workMsg.Substring(1); MessageBox.Show( workMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); SimcaData clrData = new SimcaData(dataType); Update(clrData); _pasteValue[index] = string.Empty; _enableApply[index] = false; _enableSave[index] = false; UpdataButton(); Cursor.Current = preCursor; return; } }
/// <summary> /// Update /// </summary> /// <param name="plotData">plot data</param> public void Update(SimcaData plotData) { DataGridView grid = null; switch (plotData.DataType) { case SimcaData.SIMCA_DATA_TYPE.SCORE_PLOT: grid = _dataGridViewScore; break; case SimcaData.SIMCA_DATA_TYPE.LOADING_PLOT: grid = _dataGridViewLoading; break; case SimcaData.SIMCA_DATA_TYPE.S_PLOT: grid = _dataGridViewSPlot; break; } if (grid == null) { return; } // Virtual mode OFF grid.VirtualMode = false; // Data clear ClearData(grid, plotData); if (plotData.GetDataCount() <= 0) { return; } SuspendLayout(); // Set double buffer // typeof(DataGridView).GetProperty("DoubleBuffered", // System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic). // SetValue(grid, true, null); // Set grid size grid.ColumnCount = plotData.ColumnCount; grid.RowCount = plotData.GetDataCount(); // Get style data Color[] backColor = plotData.GetGridBkColor(); FontStyle[] fontStyle = plotData.GetGridFontStyle(); // Set title/style grid.ColumnHeadersDefaultCellStyle.Font = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Bold); for (int col = 0; col < plotData.ColumnCount; col++) { grid.Columns[col].SortMode = DataGridViewColumnSortMode.NotSortable; grid.Columns[col].HeaderText = plotData.GetStrValue(col, 0); grid.Columns[col].DefaultCellStyle.Font = new Font(this.Font.FontFamily, this.Font.Size, fontStyle[col]); grid.Columns[col].DefaultCellStyle.BackColor = backColor[col]; } // Set data SetData(grid, plotData); ResumeLayout(true); }
/// <summary> /// Initializes a new instance of the FormSimcaSelectAxis class /// </summary> /// <param name="data">SIMCA data</param> public FormSimcaSelectAxis(SimcaData data) { InitializeComponent(); Size formSize = this.Size; string[] axisNameArray = data.GetAxisNameArray(); if (axisNameArray == null) { return; } _dataType = data.DataType; this.SuspendLayout(); // Create option button (1st(X) Axis) _radioButtonArray1st = new RadioButton[axisNameArray.Count()]; formSize.Height = FORM_HEIGHT_MIN; for (int index = 0; index < axisNameArray.Count(); index++) { formSize.Height += FORM_HEIGHT_ADD; // Create instance RadioButton btn = _radioButtonArray1st[index] = new RadioButton(); // Set properties btn.AutoSize = RADIO_BTN_AUTO_SIZE; btn.Name = RADIO_BTN_NAME_1ST + index.ToString(); btn.Text = axisNameArray[index]; btn.Size = new Size(RADIO_BTN_WIDTH, RADIO_BTN_HEIGHT); btn.Location = new Point( RADIO_BTN_LOC_LEFT, (index * RADIO_BTN_LOC_ADD) + RADIO_BTN_LOC_TOP); // Set event btn.CheckedChanged += this.RadioButtonArray1st_CheckedChanged; if ((index + 1) >= MAX_SELECT_NO) { break; } } // Control is added to a group box _groupBox1st.Controls.AddRange(_radioButtonArray1st); // Create option button (2nd(Y) Axis) if (_dataType == SimcaData.SIMCA_DATA_TYPE.S_PLOT) { _groupBox2nd.Visible = false; formSize.Width = FORM_WIDTH_MIN; } else { _radioButtonArray2nd = new RadioButton[axisNameArray.Count()]; for (int index = 0; index < axisNameArray.Count(); index++) { // Create instance RadioButton btn = _radioButtonArray2nd[index] = new RadioButton(); // Set properties btn.AutoSize = RADIO_BTN_AUTO_SIZE; btn.Name = RADIO_BTN_NAME_2ND + index.ToString(); btn.Text = axisNameArray[index]; btn.Size = new Size(RADIO_BTN_WIDTH, RADIO_BTN_HEIGHT); btn.Location = new Point( RADIO_BTN_LOC_LEFT, (index * RADIO_BTN_LOC_ADD) + RADIO_BTN_LOC_TOP); // Set event btn.CheckedChanged += this.RadioButtonArray2nd_CheckedChanged; if ((index + 1) >= MAX_SELECT_NO) { break; } } // Set select index _radioButtonArray2nd[data.AxisYDataIndex].Checked = true; this.SelectAxisIndexY = data.AxisYDataIndex; // Control is added to a group box _groupBox2nd.Controls.AddRange(_radioButtonArray2nd); } // Set select index _radioButtonArray1st[data.AxisXDataIndex].Checked = true; this.SelectAxisIndexX = data.AxisXDataIndex; this.ResumeLayout(false); this.Size = formSize; }
/// <summary> /// Set data /// </summary> /// <param name="data">Plot data</param> public void SetData(SimcaData data) { // Clear _chart.Series.Clear(); // Clear Series _chart.Legends.Clear(); // Clear Legends // Set Series / Legend string seriesName = string.Empty; switch (this.DataType) { case SimcaData.SIMCA_DATA_TYPE.SCORE_PLOT: seriesName = "(none)"; _chart.Legends.Add("Score"); _chart.Legends["Score"].BorderColor = Color.Black; break; case SimcaData.SIMCA_DATA_TYPE.LOADING_PLOT: seriesName = "Loading"; _chart.Series.Add(seriesName); break; case SimcaData.SIMCA_DATA_TYPE.S_PLOT: seriesName = "SPlot"; _chart.Series.Add(seriesName); break; default: break; } // It cannot be. (Just to make sure) if (seriesName == string.Empty) { return; } // Gets and check data count _dataCount = data.GetDataCount(); if (_dataCount <= 0) { // There is no data. SetDefaultData(); return; } // The maximum and the minimum are held for display range determination. double minX = double.MaxValue; // Minimum value (axis X) double minY = double.MaxValue; // Minimum value (axis Y) double maxX = double.MinValue; // Maximum value (axis X) double maxY = double.MinValue; // Maximum value (axis Y) // Series relations string preSeriesName = string.Empty; // The last series name bool seriesExist = false; // Existence of series // Add point for (int index = 1; index < _dataCount + 1; index++) { // Make series if (this.DataType == SimcaData.SIMCA_DATA_TYPE.SCORE_PLOT) { seriesExist = MakeScorePlotSeries(index, preSeriesName, data, out seriesName); preSeriesName = seriesName; } // Set series property Series workSeries = _chart.Series[seriesName]; if (!seriesExist) { workSeries.ChartType = CHART_TYPE; workSeries.MarkerStyle = MARKER_STYLE; workSeries.MarkerSize = MARKER_SIZE; workSeries.MarkerColor = data.GetGroupColor(index); workSeries.MarkerBorderColor = data.GetGroupColor(index); } // Set max/min value double valueX; double valueY; data.TryGetXData(index, out valueX); data.TryGetYData(index, out valueY); minX = Math.Min(minX, valueX); minY = Math.Min(minY, valueY); maxX = Math.Max(maxX, valueX); maxY = Math.Max(maxY, valueY); // Add point workSeries.Points.AddXY(valueX, valueY); // Set tool tip string work = string.Empty; if (this.DataType == SimcaData.SIMCA_DATA_TYPE.SCORE_PLOT) { work = seriesName; } work += string.Format("({0:G3},{1:G3})", valueX, valueY); workSeries.Points.Last().ToolTip = work; // Set custom properties workSeries.Points.Last().CustomProperties = INIT_CUSTOM_PROPERTY; workSeries.Points.Last().SetCustomProperty( PROPERTY_NAME_NUMBER, index.ToString()); workSeries.Points.Last().SetCustomProperty( PROPERTY_NAME_PEAK_ID, data.GetPeakId(index).ToString()); } // In one point, in order that there may be no range, // a range setup does not function well. // ±5% is set up as a range. if (_dataCount == 1) { minX = minX - Math.Abs(minX * 0.05); maxX = maxX + Math.Abs(maxX * 0.05); minY = minY - Math.Abs(minY * 0.05); maxY = maxY + Math.Abs(maxY * 0.05); } // Update graph range UpdateGraphGridInterval(minX, minY, maxX, maxY, true); _chart.ChartAreas[0].AxisX.Title = data.GetAxisXName(); _chart.ChartAreas[0].AxisY.Title = data.GetAxisYName(); // Select axis button disable _buttonSelAxis.Enabled = true; // Change of a selective points is notified. ChangeSelect(false); }