242 lines
11 KiB
Plaintext
242 lines
11 KiB
Plaintext
|
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text;
|
||
|
|
using System.Windows;
|
||
|
|
using System.Windows.Input;
|
||
|
|
using System.Windows.Media.Imaging;
|
||
|
|
using C1.WPF.C1Chart;
|
||
|
|
using C1.WPF.C1Chart.Extended;
|
||
|
|
using C1.WPF.Pdf;
|
||
|
|
using DTS.Common.Interface;
|
||
|
|
using DTS.Common.Utilities.Logging;
|
||
|
|
using DTS.Common.Utils;
|
||
|
|
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
|
||
|
|
using System.Globalization;
|
||
|
|
|
||
|
|
// ReSharper disable CheckNamespace
|
||
|
|
|
||
|
|
namespace DTS.Viewer.Graph
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Interaction logic for TestDataSeriesView.xaml
|
||
|
|
/// </summary>
|
||
|
|
public partial class TestDataSeriesView : ITestDataSeriesView
|
||
|
|
{
|
||
|
|
public TestDataSeriesView()
|
||
|
|
{
|
||
|
|
InitializeComponent();
|
||
|
|
|
||
|
|
}
|
||
|
|
private void obj_DataPointChanged(object sender, EventArgs e)
|
||
|
|
{
|
||
|
|
//// update label in code from marker
|
||
|
|
//if (DataContext == null) return;
|
||
|
|
|
||
|
|
//var obj = (ChartPanelObject)sender;
|
||
|
|
|
||
|
|
//if (obj == null) return;
|
||
|
|
|
||
|
|
//var pt = MainChart.View.PointFromData(obj.DataPoint);
|
||
|
|
|
||
|
|
//var txt = String.Empty;
|
||
|
|
|
||
|
|
//for (var i = 0; i < MainChart.Data.Children.Count; i++)
|
||
|
|
//{
|
||
|
|
// double distance = 0; //distance calculated, but not used
|
||
|
|
// var index = MainChart.View.DataIndexFromPoint(pt, i, MeasureOption.X, out distance);
|
||
|
|
// if (index == -1) continue;
|
||
|
|
// txt += Environment.NewLine + MainChart.Data.Children[i].Label + Environment.NewLine + "Y: " + MainChart.Data.Children[i].GetDataValue("Values", index) + Environment.NewLine + "X: " + MainChart.Data.Children[i].GetDataValue("XValues", index);
|
||
|
|
//}
|
||
|
|
//var viewModel = (ITestDataSeriesViewModel)DataContext;
|
||
|
|
//viewModel.CurrentCursorValues = txt;
|
||
|
|
}
|
||
|
|
private void MainChart_OnMouseWheel(object sender, MouseWheelEventArgs e)
|
||
|
|
{
|
||
|
|
//if (Keyboard.Modifiers == ModifierKeys.Control && e.Delta == -120)
|
||
|
|
//{
|
||
|
|
// MainChart.View.AxisX.Scale += 0.1;
|
||
|
|
// MainChart.View.AxisY.Scale += 0.1;
|
||
|
|
//}
|
||
|
|
//else if (Keyboard.Modifiers == ModifierKeys.Control && e.Delta == 120)
|
||
|
|
//{
|
||
|
|
// MainChart.View.AxisX.Scale -= 0.1;
|
||
|
|
// MainChart.View.AxisY.Scale -= 0.1;
|
||
|
|
//}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void MainChart_OnKeyUp(object sender, KeyEventArgs e)
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool SaveReportToPDF(string directory)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(directory)) throw new DirectoryNotFoundException();
|
||
|
|
|
||
|
|
var indent = 10;
|
||
|
|
var spacing = 2;
|
||
|
|
// FB9152: Migrated from ReviewFile.xaml.cs
|
||
|
|
// Generate Chart Image
|
||
|
|
var ms = new MemoryStream();
|
||
|
|
MainChart.SaveImage(ms, ImageFormat.Png);
|
||
|
|
var bi = new BitmapImage();
|
||
|
|
bi.BeginInit();
|
||
|
|
bi.StreamSource = ms;
|
||
|
|
bi.EndInit();
|
||
|
|
var wBmp = new WriteableBitmap(bi);
|
||
|
|
|
||
|
|
var dataSeries = ((TestDataSeriesViewModel)MainChart.DataContext).GraphDataSeries;
|
||
|
|
|
||
|
|
// create and save pdf document
|
||
|
|
var pdf = new C1PdfDocument
|
||
|
|
{
|
||
|
|
Landscape = false,
|
||
|
|
PaperKind = System.Globalization.RegionInfo.CurrentRegion.IsMetric ? PaperKind.A4 : PaperKind.Letter,
|
||
|
|
};
|
||
|
|
var font = new C1.WPF.Pdf.Font(MainChart.FontFamily.Source, MainChart.FontSize);
|
||
|
|
var pen = new Pen(System.Windows.Media.Colors.Black);
|
||
|
|
|
||
|
|
var drawHeight = indent;
|
||
|
|
// write test info
|
||
|
|
var testSetup = $"Test Setup: {dataSeries[0].TestSetupName}";
|
||
|
|
var testId = $"Test ID: {dataSeries[0].TestId}";
|
||
|
|
pdf.DrawString(testSetup, font, System.Windows.Media.Colors.Black, new Point(indent, drawHeight));
|
||
|
|
drawHeight += (int)pdf.MeasureString(testSetup, font).Height;
|
||
|
|
pdf.DrawString(testId, font, System.Windows.Media.Colors.Black, new Point(indent, drawHeight));
|
||
|
|
drawHeight += (int)pdf.MeasureString(testId, font).Height;
|
||
|
|
|
||
|
|
// draw chart
|
||
|
|
var chartWidth = pdf.PageSize.Width - indent - indent;
|
||
|
|
var chartHeight = (chartWidth / MainChart.ActualWidth) * MainChart.ActualHeight;
|
||
|
|
|
||
|
|
pdf.DrawImage(wBmp, new Rect(indent,
|
||
|
|
drawHeight,
|
||
|
|
chartWidth,
|
||
|
|
chartHeight));
|
||
|
|
drawHeight += (int)chartHeight;
|
||
|
|
|
||
|
|
// write gRMS table
|
||
|
|
var colStart = indent * 2;
|
||
|
|
var col1X = colStart + spacing;
|
||
|
|
var col2X = col1X + dataSeries.Max(ds => pdf.MeasureString(ds.ChannelName, font).Width) + spacing;
|
||
|
|
var col3X = col2X + Math.Max(pdf.MeasureString("Sample Rate", font).Width, dataSeries.Max(ds => pdf.MeasureString(ds.SampleRate, font).Width)) + spacing;
|
||
|
|
var colEnd = col3X + dataSeries.Max(ds => pdf.MeasureString(ds.GRMS.ToString(), font).Width) + spacing;
|
||
|
|
|
||
|
|
|
||
|
|
pdf.DrawLine(pen, col1X, drawHeight, colEnd, drawHeight);
|
||
|
|
pdf.DrawString("Name", font, System.Windows.Media.Colors.Black, new Point(col1X, drawHeight));
|
||
|
|
pdf.DrawString("Sample Rate", font, System.Windows.Media.Colors.Black, new Point(col2X, drawHeight));
|
||
|
|
pdf.DrawString("gRMS", font, System.Windows.Media.Colors.Black, new Point(col3X, drawHeight));
|
||
|
|
var titleHeight = (int)pdf.MeasureString("gRMS", font).Height;
|
||
|
|
pdf.DrawLine(pen, col1X, drawHeight, col1X, drawHeight + titleHeight);
|
||
|
|
pdf.DrawLine(pen, col2X, drawHeight, col2X, drawHeight + titleHeight);
|
||
|
|
pdf.DrawLine(pen, col3X, drawHeight, col3X, drawHeight + titleHeight);
|
||
|
|
pdf.DrawLine(pen, colEnd, drawHeight, colEnd, drawHeight + titleHeight);
|
||
|
|
drawHeight += titleHeight;
|
||
|
|
pdf.DrawLine(pen, col1X, drawHeight, colEnd, drawHeight);
|
||
|
|
|
||
|
|
foreach (var ds in dataSeries)
|
||
|
|
{
|
||
|
|
var rowHeight = (int)pdf.MeasureString(ds.ChannelName, font).Height;
|
||
|
|
pdf.FillRectangle((ds.GraphColor as System.Windows.Media.SolidColorBrush).Color, indent, drawHeight, indent, rowHeight);
|
||
|
|
pdf.DrawString(ds.ChannelName, font, System.Windows.Media.Colors.Black, new Point(col1X, drawHeight));
|
||
|
|
pdf.DrawString(ds.SampleRate, font, System.Windows.Media.Colors.Black, new Point(col2X, drawHeight));
|
||
|
|
pdf.DrawString(ds.GRMS.ToString(), font, System.Windows.Media.Colors.Black, new Point(col3X, drawHeight));
|
||
|
|
pdf.DrawLine(pen, col1X, drawHeight, col1X, drawHeight + rowHeight);
|
||
|
|
pdf.DrawLine(pen, col2X, drawHeight, col2X, drawHeight + rowHeight);
|
||
|
|
pdf.DrawLine(pen, col3X, drawHeight, col3X, drawHeight + rowHeight);
|
||
|
|
pdf.DrawLine(pen, colEnd, drawHeight, colEnd, drawHeight + rowHeight);
|
||
|
|
drawHeight += rowHeight;
|
||
|
|
pdf.DrawLine(pen, col1X, drawHeight, colEnd, drawHeight);
|
||
|
|
}
|
||
|
|
|
||
|
|
//pdf.ImageQuality = ImageQuality.High;
|
||
|
|
|
||
|
|
var pdfFileName = directory + "\\" +
|
||
|
|
DateTime.Now.ToString("M'/'d'/'yyyy", CultureInfo.InvariantCulture).Replace("/", "_") + "_" +
|
||
|
|
DateTime.Now.ToString("h':'mm' 'tt", CultureInfo.InvariantCulture).Replace(":", "_") + "_" + DateTime.Now.Second + "_" +
|
||
|
|
DateTime.Now.Millisecond + ".pdf";
|
||
|
|
if (!System.IO.Directory.Exists(directory)) System.IO.Directory.CreateDirectory(directory);
|
||
|
|
var pdfPath = Path.Combine(directory, pdfFileName);
|
||
|
|
pdf.Save(pdfPath);
|
||
|
|
// 35532 log save path after successful write
|
||
|
|
APILogger.Log($"Report saved to {pdfPath}");
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
// 35532
|
||
|
|
APILogger.Log("Error saving report to PDF");
|
||
|
|
APILogger.LogException(ex);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool SaveReportToCSV(string directory)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (!System.IO.Directory.Exists(directory)) System.IO.Directory.CreateDirectory(directory);
|
||
|
|
var csvFileName = directory + "\\" +
|
||
|
|
DateTime.Now.ToString("M'/'d'/'yyyy", CultureInfo.InvariantCulture).Replace("/", "_") + "_" +
|
||
|
|
DateTime.Now.ToString("h':'mm' 'tt", CultureInfo.InvariantCulture).Replace(":", "_") + "_" + DateTime.Now.Second + "_" +
|
||
|
|
DateTime.Now.Millisecond + ".PSD.csv";
|
||
|
|
APILogger.Log("opening ", csvFileName);
|
||
|
|
using (var fs = new FileStream(csvFileName, FileMode.Create))
|
||
|
|
{
|
||
|
|
|
||
|
|
Encoding encoder;
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
encoder = Common.Utils.FileUtils.GetEncoding(Encoding.UTF8.CodePage);
|
||
|
|
}
|
||
|
|
catch (System.Exception ex)
|
||
|
|
{
|
||
|
|
APILogger.Log("Problem getting encoder", ex);
|
||
|
|
encoder = Encoding.Default;
|
||
|
|
}
|
||
|
|
|
||
|
|
using (var fileWriter = new StreamWriter(fs, encoder, 1024 * 1000))
|
||
|
|
{
|
||
|
|
var dataSeries = ((TestDataSeriesViewModel)MainChart.DataContext).GraphDataSeries;
|
||
|
|
var header = "Frequency," + string.Join(",", dataSeries.Select(ds => ds.ChannelName).ToArray());
|
||
|
|
fileWriter.WriteLine(header);
|
||
|
|
var units = "Hz," + string.Join(",", dataSeries.Select(ds => ds.EngineeringUnits + "^2/Hz"));
|
||
|
|
fileWriter.WriteLine(units);
|
||
|
|
|
||
|
|
for (var i = 0; dataSeries.All(ds => i < ds.Xvalue.Length && i < ds.Yvalue.Length); i++)
|
||
|
|
{
|
||
|
|
var line = dataSeries[0].Xvalue[i].ToString() + "," + string.Join(",", dataSeries.Select(ds => ds.Yvalue[i].ToString()).ToArray());
|
||
|
|
fileWriter.WriteLine(line);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fs.Close();
|
||
|
|
|
||
|
|
// 35532 log save path after successful write
|
||
|
|
APILogger.Log($"Report saved to {csvFileName}");
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
// 35532
|
||
|
|
APILogger.Log("Error saving report to CSV");
|
||
|
|
APILogger.LogException(ex);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
public static class AxisExtension
|
||
|
|
{
|
||
|
|
public static double GetDispMin(this Axis axis) { return axis.ActualMin + axis.Value * (axis.ActualMax - axis.ActualMin) * (1 - axis.Scale); }
|
||
|
|
public static double GetDispMax(this Axis axis) { return axis.ActualMax - (1 - axis.Value) * (axis.ActualMax - axis.ActualMin) * (1 - axis.Scale); }
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|