Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS will be completely phased out by mid-2025. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit 464f8870 authored by Markus Quaritsch's avatar Markus Quaritsch
Browse files

new method for plotting graphs

parent d1e46e07
No related branches found
No related tags found
No related merge requests found
......@@ -55,62 +55,93 @@ namespace TUGraz.VectoCore.Tests.Utils
ModalResultField.Tq_eng, ModalResultField.FCMap
};
var titleHeight = 36;
var titleHeight = (36 * 100.0f) / (diagramSize.Height * yfields.Count());
//var images = new List<Stream>();
//try {
foreach (var xfield in xfields) {
//var x = modDataV3.Rows.Cast<DataRow>().Select(v => v.Field<double>(xfield.GetName())).ToArray();
var fileName = string.Format("{0}_{1}.png", Path.GetFileNameWithoutExtension(fileNameV3), xfield.GetName());
var output = new Bitmap(diagramSize.Width, diagramSize.Height * yfields.Count() + titleHeight);
var g = Graphics.FromImage(output);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(fileName, new Font("Arial", 16), Brushes.Black, output.Width / 2, 10,
new StringFormat { Alignment = StringAlignment.Center });
var x = modDataV3.Rows.Cast<DataRow>().Select(v => v.Field<string>(xfield.GetName()).ToDouble()).ToArray();
var x2 = modDataV22.Rows.Cast<DataRow>().Select(v => v.Field<string>(xfield.GetName()).ToDouble()).ToArray();
var plotSize = new Size(diagramSize.Width, diagramSize.Height * yfields.Count());
var maxX = (int)(Math.Ceiling(Math.Max(x.Max(), x2.Max()) / 10.0) * 10.0);
var minX = (int)(Math.Floor(Math.Max(x.Min(), x2.Min()) / 10.0) * 10.0);
var chart = new Chart { Size = plotSize };
for (var i = 0; i < yfields.Length; i++) {
var yfield = yfields[i];
var y = modDataV3.Rows.Cast<DataRow>().Select(v => v.Field<string>(yfield.GetName()).ToDouble()).ToArray();
var y2 = modDataV22.Rows.Cast<DataRow>().Select(v => v.Field<string>(yfield.GetName()).ToDouble()).ToArray();
var y =
modDataV3.Rows.Cast<DataRow>()
.Select(
v => v.Field<string>(yfield.GetName()).Length == 0 ? Double.NaN : v.Field<string>(yfield.GetName()).ToDouble())
.ToArray();
var y2 =
modDataV22.Rows.Cast<DataRow>()
.Select(
v =>
v.Field<string>(yfield.GetName()).Length == 0 ? Double.NaN : v.Field<string>(yfield.GetName()).ToDouble())
.ToArray();
var chartArea = new ChartArea { Name = yfield.ToString() };
chartArea.AxisX.MajorGrid.LineColor = Color.DarkGray;
chartArea.AxisY.MajorGrid.LineColor = Color.DarkGray;
chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 10);
chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 10);
chartArea.AxisX.Interval = maxX / 20.0;
chartArea.AxisX.Maximum = maxX;
chartArea.AxisX.Minimum = minX;
chartArea.AxisX.MinorGrid.Enabled = true;
chartArea.AxisX.MinorGrid.Interval = maxX / 100.0;
chartArea.AxisX.MinorGrid.LineColor = Color.LightGray;
chartArea.AxisX.Title = xfield.GetCaption();
chartArea.AxisX.TitleFont = new Font("Verdana", 12);
chartArea.AxisX.RoundAxisValues();
chartArea.AxisX.MajorTickMark.Size = 2 * 100.0f / diagramSize.Height;
var chart = new Chart { Size = diagramSize };
chartArea.AxisY.Title = yfield.GetCaption();
chartArea.AxisY.TitleFont = new Font("Verdana", 12);
chartArea.AxisY.RoundAxisValues();
if (yfield == ModalResultField.Gear) {
chartArea.AxisY.MajorGrid.Interval = 1;
chartArea.AxisY.MinorGrid.Enabled = false;
} else {
chartArea.AxisY.MinorGrid.Enabled = true;
}
chartArea.AxisY.MinorGrid.LineColor = Color.LightGray;
chartArea.AxisY.MajorTickMark.Size = 5 * 100.0f / diagramSize.Width;
var chartArea = new ChartArea();
chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chart.ChartAreas.Add(chartArea);
if (yfield == ModalResultField.v_act) {
var y3 = modDataV3.Rows.Cast<DataRow>()
.Select(v => v.Field<string>(ModalResultField.v_targ.GetName()).ToDouble())
.Select(
v =>
v.Field<string>(ModalResultField.v_targ.GetName()).Length == 0
? Double.NaN
: v.Field<string>(ModalResultField.v_targ.GetName()).ToDouble())
.ToArray();
var series3 = new Series {
Name = "v_target",
ChartType = SeriesChartType.FastLine,
Color = Color.Green,
BorderWidth = 2
BorderWidth = 3
};
chart.Series.Add(series3);
chart.Series[series3.Name].Points.DataBindXY(x, y3);
series3.ChartArea = chartArea.Name;
}
var series1 = new Series {
Name = String.Format("Vecto 3 - {0}", yfield),
ChartType = SeriesChartType.FastLine,
Color = Color.Blue,
BorderWidth = 3
BorderWidth = 2
};
series1.ChartArea = chartArea.Name;
chart.Series.Add(series1);
chart.Series[series1.Name].Points.DataBindXY(x, y);
......@@ -118,29 +149,36 @@ namespace TUGraz.VectoCore.Tests.Utils
Name = String.Format("Vecto 2.2 - {0}", yfield),
ChartType = SeriesChartType.FastLine,
Color = Color.Red,
BorderWidth = 3
BorderWidth = 2
};
series2.ChartArea = chartArea.Name;
chart.Series.Add(series2);
chart.Series[series2.Name].Points.DataBindXY(x2, y2);
//chart.Titles.Add(yfield.ToString());
var maxX = (int)Math.Ceiling(Math.Max(x.Max(), x2.Max()));
chartArea.Position.Auto = false;
chartArea.Position.Width = 97;
chartArea.Position.Height = (100.0f - titleHeight) / yfields.Count();
chartArea.Position.X = 0;
chartArea.Position.Y = (i * (100.0f - titleHeight)) / yfields.Count() + titleHeight;
chartArea.AxisX.Interval = maxX / 10;
chartArea.AxisX.Title = xfield.GetCaption();
chartArea.AxisX.RoundAxisValues();
chartArea.AxisY.Title = yfield.GetCaption();
chartArea.AxisY.RoundAxisValues();
chart.Invalidate();
//chart.SaveImage(fileName, ChartImageFormat.Png);
chart.DrawToBitmap(output, new Rectangle(new Point(0, titleHeight + i * diagramSize.Height), diagramSize));
if (i > 0) {
chart.ChartAreas[yfield.ToString()].AlignWithChartArea = yfields[0].ToString();
chart.ChartAreas[yfield.ToString()].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
chart.ChartAreas[yfield.ToString()].AlignmentStyle = AreaAlignmentStyles.All;
}
}
output.Save(fileName);
var title = new Title();
title.Text = Path.GetFileNameWithoutExtension(fileName);
title.DockedToChartArea = yfields[0].ToString();
title.IsDockedInsideChartArea = false;
title.Font = new Font("Verdana", 14, FontStyle.Bold);
chart.Titles.Add(title);
chart.Invalidate();
chart.SaveImage(fileName, ChartImageFormat.Png);
}
}
......@@ -195,135 +233,5 @@ namespace TUGraz.VectoCore.Tests.Utils
// images.ForEach(x => x.Close());
// }
//}
//public static void Write(string fileNameV3, string fileNameV22)
//{
// if (!_enabled) {
// return;
// }
// var modDataV3 = VectoCSVFile.Read(fileNameV3);
// if (!File.Exists(fileNameV22)) {
// LogManager.GetCurrentClassLogger().Error("Modfile V2.2 not found: " + fileNameV22);
// Write(fileNameV3);
// return;
// }
// var modDataV22 = VectoCSVFile.Read(fileNameV22);
// var xfields = new[] { ModalResultField.time, ModalResultField.dist };
// var yfields = new[] {
// ModalResultField.v_act, ModalResultField.acc, ModalResultField.n, ModalResultField.Gear, ModalResultField.Pe_eng,
// ModalResultField.Tq_eng,
// ModalResultField.FCMap
// };
// var images = new List<Stream>();
// try {
// foreach (var xfield in xfields) {
// var x = modDataV3.Rows.Cast<DataRow>().Select(v => v.Field<string>(xfield.GetName())).ToArray();
// var x2 = modDataV22.Rows.Cast<DataRow>().Select(v => v.Field<string>(xfield.GetName())).ToArray();
// for (var i = 1; i <= yfields.Length; i++) {
// var yfield = yfields[i - 1];
// var y =
// modDataV3.Rows.Cast<DataRow>()
// .Select(v => v.Field<string>(yfield.GetName()))
// .Select(v => v == "-1" ? "-1.00001" : v)
// .ToArray();
// //y = y.ToArray();
// var y2 =
// modDataV22.Rows.Cast<DataRow>()
// .Select(v => v.Field<string>(yfield.GetName()))
// .Select(v => v == "-1" ? "-1.00001" : v)
// .ToArray();
// var values = string.Format("{0}|{1}|{2}|{3}", string.Join(",", x), string.Join(",", y), string.Join(",", x2),
// string.Join(",", y2));
// if (yfield == ModalResultField.v_act) {
// var y3 =
// modDataV3.Rows.Cast<DataRow>()
// .Select(v => v.Field<string>(ModalResultField.v_targ.GetName()))
// .Select(v => string.IsNullOrWhiteSpace(v) ? "0" : v);
// values += string.Format("|{0}|{1}", string.Join(",", x), string.Join(",", y3));
// }
// values = values.Replace("NaN", "_");
// //values = values.Replace("-1", "-1.00001");
// if (values.Length > 14000) {
// // remove all decimal places to reduce request size
// values = Regex.Replace(values, @"(\.[0-9]).*?,", "$1,");
// }
// var maxX = (int)Math.Ceiling(Math.Max(x.ToDouble().Max(), x2.ToDouble().Max()));
// images.Add(CreateGraphStream(xfield.GetCaption(), yfield.GetCaption(), maxX, values));
// }
// var fileName = string.Format("{0}_{1}.png", Path.GetFileNameWithoutExtension(fileNameV3), xfield.GetName());
// SaveImages(fileName, images.ToArray());
// images.Clear();
// }
// } finally {
// images.ForEach(x => x.Close());
// }
//}
//private static Stream CreateGraphStream(string xLabel, string yLabel, int xAxisRange, string values)
//{
// // see https://developers.google.com/chart/image/?hl=en for details
// using (var client = new WebClient()) {
// var response = client.UploadValues("https://chart.googleapis.com/chart", new NameValueCollection {
// { "cht", "lxy" },
// { "chd", "t:" + values },
// { "chs", "1000x230" },
// { "chxt", "x,x,y,y" },
// { "chds", "a" },
// { "chxr", string.Format("0,0,{0},{1}", xAxisRange, xAxisRange / 10) },
// { "chco", "0000FF,FF0000,00FF00" },
// { "chg", "5,10" },
// { "chxl", string.Format("1:|{0}|3:|{1}", xLabel, yLabel) },
// { "chxp", "1,0|3,0" },
// { "chdl", "V3|V2.2" },
// });
// return new MemoryStream(response);
// }
//}
//public static void SaveImages(string outputFile, params Stream[] inputFiles)
//{
// var titleHeight = 36;
// var images = new List<Image>();
// Bitmap output = null;
// Graphics g = null;
// try {
// images = inputFiles.Select(Image.FromStream).ToList();
// output = new Bitmap(images.Max(x => x.Width), images.Sum(x => x.Height) + titleHeight);
// g = Graphics.FromImage(output);
// g.TextRenderingHint = TextRenderingHint.AntiAlias;
// g.DrawString(outputFile, new Font("Arial", 16), Brushes.Black, output.Width / 2, 10,
// new StringFormat { Alignment = StringAlignment.Center });
// var ypos = titleHeight;
// foreach (var image in images) {
// g.DrawImage(image, new System.Drawing.Point(0, ypos));
// ypos += image.Height;
// image.Dispose();
// }
// output.Save(outputFile);
// } finally {
// images.ForEach(x => x.Dispose());
// if (output != null) {
// output.Dispose();
// }
// if (g != null) {
// g.Dispose();
// }
// }
//}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment