// Deciding along which axis take coordinates private void comboBoxTemplateAxis_SelectedIndexChanged(object sender, EventArgs e) { if (listBoxTemplateCharts.Items.Count == 0) { return; } ChartDirection iSelected = (ChartDirection)comboBoxTemplateAxis.SelectedIndex; charts_.axis[listBoxTemplateCharts.SelectedItem.ToString()] = iSelected; }
// Processing all the data private void buttonProcess_Click(object sender = null, EventArgs e = null) { string templateName = textBoxNameExcel.Text; string templateDirectory = textBoxDirectoryExcel.Text; if (String.IsNullOrEmpty(templateName)) { MessageBox.Show("Filename to save is not specified", "Empty filename", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (String.IsNullOrEmpty(templateDirectory)) { MessageBox.Show("Directory to save is not specified", "Empty directory name", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ExcelObject excelResult = new ExcelObject(excelTemplate_, templateDirectory, templateName); // Checking the project, template and selected signals if (!project.isProjectOpened() || !excelTemplate_.isOpened()) { return; } // Resolving dependencies resolveDependencies(); // Retrieve selected frequencies int nSingleFrequency = singleFrequencyIndices_.Count; ChartPosition.lastRow = 0; // Error handling errorMessage_ = null; int iError = 0; // Create a map to convert directions into labels Dictionary <ChartDirection, string> mapDirections = new Dictionary <ChartDirection, string>(); mapDirections[ChartDirection.X] = "X"; mapDirections[ChartDirection.Y] = "Y"; mapDirections[ChartDirection.Z] = "Z"; // Creating series double signData = 1.0; string inversionInfo = "No"; if (checkBoxInverseResults.Checked) { signData = -1.0; inversionInfo = "Yes"; } // Creating the header excelResult.addHeaderInfo($"Date: {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")}"); excelResult.addHeaderInfo($"Inversion: {inversionInfo}"); excelResult.addHeaderInfo($"Template: {templateDirectory + "\\" + templateName + ".xlsx"}"); ChartPosition.lastRow += 1; foreach (string chart in listBoxTemplateCharts.Items) { // Nodes selection List <ISelection> selectedObjects = charts_.selection[chart]; // Type and direction ChartTypes type = charts_.type[chart]; ChartDirection direction = charts_.direction[chart]; SignalUnits units = charts_.units[chart]; // Error handling if (type == ChartTypes.UNKNOWN || selectedObjects.Count == 0) { continue; } if (direction == ChartDirection.UNKNOWN) { throwError(ref iError, "The direction for '" + chart + "' is not specified"); continue; } if (units == SignalUnits.UNKNOWN) { throwError(ref iError, "The units for '" + chart + "' are not specified"); continue; } // Norm double norm = charts_.normalization[chart]; ChartDirection axis = charts_.axis[chart]; int indX = 0, indY = 1; if (charts_.swapAxes[chart]) { indX = 1; indY = 0; } string infoUnits = comboBoxTemplateUnits.Items[(int)units].ToString(); // Frequency response function: real and imaginary parts if ((type == ChartTypes.REAL_FRF || type == ChartTypes.IMAG_FRF) && project.signals_.Count != 0) { List <string> chartNodes = new List <string>(); foreach (ISelection item in selectedObjects) { chartNodes.Add((string)item.retrieveSelection()); } int iType = (int)type - 1; foreach (string node in chartNodes) // Node { // Check if there is an appropriate signal if (!project.signals_.ContainsKey(node) || !project.signals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } Response response = project.signals_[node][direction][0]; // Slice data by the selected index if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } double[,] refFullData = response.data[units]; double[,] data = new double[nSingleFrequency, 2]; int iSelected; for (int i = 0; i != nSingleFrequency; ++i) { iSelected = singleFrequencyIndices_[i]; data[i, indX] = response.frequency[iSelected]; data[i, indY] = refFullData[iSelected, iType] * signData; } string ptrNode = "т. " + node.Split(selectionDelimiter_)[1]; string info = $" - {mapDirections[direction]} ({infoUnits})"; excelResult.addSeries(chart, data, ptrNode, info); } } // Modeset else if (type == ChartTypes.MODESET && project.signals_.Count != 0) { if (axis == ChartDirection.UNKNOWN) { throwError(ref iError, "The coordinate axis for '" + chart + "' is not specified"); continue; } if (indexSingleResonanceFrequency_ < 0) { throwError(ref iError, "The resonance frequency is not chosen"); continue; } // For each line foreach (ISelection item in selectedObjects) { Lines currentLine = (Lines)item; string nameLine = currentLine.lineName_; List <string> lineNodes = (List <string>)currentLine.retrieveSelection(); List <double> coordinates = new List <double>(); List <double> values = new List <double>(); double resonanceFrequency = -1.0; foreach (string node in lineNodes) { // Check if there is an appropriate signal if (!project.signals_.ContainsKey(node) || !project.signals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } // Retreiving the coordinate along the choosen axis string[] selectionInfo = node.Split(selectionDelimiter_); uint indNode = modelRenderer_.componentSet_.mapNodeNames[selectionInfo[0]][selectionInfo[1]]; double[,] componentCoordinates = (double[, ])modelRenderer_.componentSet_.nodeCoordinates[selectionInfo[0]]; int tInd = (int)axis - 1; coordinates.Add(componentCoordinates[indNode, tInd]); // Retrieving the function value Response response = project.signals_[node][direction][0]; if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } double[,] refFullData = response.data[units]; values.Add(refFullData[indexSingleResonanceFrequency_, 1]); // Imaginary part of the signal if (resonanceFrequency < 0) { resonanceFrequency = response.frequency[indexSingleResonanceFrequency_]; } } if (values.Count > 0) { int nNodes = coordinates.Count; double[,] data = new double[nNodes, 2]; for (int i = 0; i != nNodes; ++i) { data[i, indX] = coordinates[i] / norm; data[i, indY] = values[i] * signData; } string frequencyInfo = null; if (resonanceFrequency > 0) { frequencyInfo = $" - {resonanceFrequency.ToString("F3", CultureInfo.InvariantCulture)} Гц ({infoUnits})"; } excelResult.addSeries(chart, data, nameLine, frequencyInfo); } } } // Multi-FRF else if ((type == ChartTypes.MULTI_REAL_FRF || type == ChartTypes.MULTI_IMAG_FRF) && project.multiSignals_.Count != 0) { List <string> chartNodes = new List <string>(); int iType = 0; if (type == ChartTypes.MULTI_IMAG_FRF) { iType = 1; } foreach (ISelection item in selectedObjects) { chartNodes.Add((string)item.retrieveSelection()); } foreach (string node in chartNodes) // Node { // Check if there is an appropriate signal if (!project.multiSignals_.ContainsKey(node) || !project.multiSignals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } var dirNodeSignals = project.multiSignals_[node][direction]; foreach (Response response in dirNodeSignals) { // Slice data by the selected index if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } double[,] refFullData = response.data[units]; List <int> indices = multiFrequencyIndices_[mapResponses_[response.path]]; int nIndices = indices.Count; double[,] data = new double[nIndices, 2]; int iSelected; for (int i = 0; i != nIndices; ++i) { iSelected = indices[i]; data[i, indX] = response.frequency[iSelected]; data[i, indY] = refFullData[iSelected, iType] * signData; } // Retrieving force value if (data.GetLength(0) > 0) { string force = $"𝐹 = {getForceValue(response.path)} Н"; string infoChart = $" - {mapDirections[direction]} ({infoUnits})"; string infoNode = $"{node}"; excelResult.addSeries(chart, data, force, infoChart, infoNode); } } } } // Frequency function else if ((type == ChartTypes.REAL_FREQUENCY || type == ChartTypes.IMAG_FREQUENCY) && project.multiSignals_.Count != 0) { List <string> chartNodes = new List <string>(); foreach (ISelection item in selectedObjects) { chartNodes.Add((string)item.retrieveSelection()); } foreach (string node in chartNodes) // Node { // Check if there is an appropriate signal if (!project.multiSignals_.ContainsKey(node) || !project.multiSignals_[node].ContainsKey(direction)) { throwError(ref iError, "The chosen signals do not contain the node '" + node + "'"); continue; } var dirNodeSignals = project.multiSignals_[node][direction]; double[,] data = new double[dirNodeSignals.Count, 2]; int k = 0; foreach (Response response in dirNodeSignals) { if (!response.data.ContainsKey(units) || response.data[units] == null) { continue; } string label = mapResponses_[response.path]; int indexMultiResonance = multiResonanceFrequencyIndices_[label]; double resonanceFrequency = multiFrequency_[label][indexMultiResonance]; Tuple <double, double> pair = response.evaluateResonanceFrequency(type, units, resonanceFrequency); if (pair != null) { data[k, indX] = pair.Item1; data[k, indY] = pair.Item2; } else { double realPart = response.data[units][indexMultiResonance, 0]; double imagPart = response.data[units][indexMultiResonance, 1]; data[k, indX] = resonanceFrequency; data[k, indY] = Math.Sqrt(Math.Pow(realPart, 2.0) + Math.Pow(imagPart, 2.0)); } ++k; } if (data.GetLength(0) > 0) { string ptrNode = "т. " + node.Split(selectionDelimiter_)[1]; string infoChart = $" - {mapDirections[direction]} ({infoUnits})"; excelResult.addSeries(chart, data, ptrNode, infoChart); } } } } ChartPosition.lastRow = 0; // Saving and opening the results excelResult.save(); excelResult.open(); if (iError == 0) { setStatus("The results were successfully processed"); } else { showErrors(iError); } }
/// <summary> /// 将DataTable转换为EChart Option /// </summary> /// <param name="dtData">DataTable</param> /// <param name="chartType">Chart 类型:line,bar,pie</param> /// <param name="Title">Chart Title</param> /// <param name="subTitle">Chart Sub Title</param> /// <param name="columnAxis">DataTable中用于作为坐标轴的列名</param> /// <param name="columnSeries">DataTable中用于作为Series的列名</param> /// <param name="columnValue">DataTable中用于作为值的列名</param> /// <param name="stackName">Stack名</param> /// <param name="chartDirection">Chart 方向</param> /// <param name="xAxisName">X轴名</param> /// <param name="xAxisName">Y轴名</param> /// <returns></returns> public static ChartOption ToChartOption(this DataTable dtData, ChartType chartType, string Title, string subTitle, string columnAxis, string columnSeries, string columnValue, string stackName, ChartDirection chartDirection, string xAxisName = "", string yAxisName = "") { ChartOption option = new ChartOption(); if (string.IsNullOrWhiteSpace(Title) == false) { option.title = new Title() { text = Title, subtext = subTitle } } ; option.ChartDirection = chartDirection; option.tooltip = new ToolTip(); option.legend = new Legend(); if (chartType == ChartType.Pie) { option.tooltip.trigger = "item"; } else { option.tooltip.formatter = null; } option.toolbox = new ToolBox(); dtData.AddToChartOption(option, chartType, columnAxis, columnSeries, columnValue, stackName, xAxisName, yAxisName); return(option); }