This is a follow-up question for CSV Data Plotting Program in C#. I am trying to use CsvHelper
to build an application for plotting CSV Data in this post. In the plotting part, I use ZedGraph Library to perform plotting operation.
The experimental implementation
using CsvHelper.Configuration;
using CsvHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;
namespace CSV_PlottingApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_load_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// Set the title and filter for the dialog
openFileDialog.Title = "Open File";
openFileDialog.Filter = "CSV files (*.csv)|*.csv|Text files (*.txt)|*.txt|All files (*.*)|*.*";
// Display the dialog and check if the user clicked OK
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Get the selected file name
string fileName = openFileDialog.FileName;
CSV_Processing(fileName);
}
else
{
Console.WriteLine("No file selected.");
}
}
private void CSV_Processing(in string filename)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
};
PointPairList data_list1 = new PointPairList();
PointPairList data_list2 = new PointPairList();
using (var reader = new StreamReader(filename))
using (var csv = new CsvReader(reader, config))
{
var records = csv.GetRecords<Channels>();
uint index = 0;
try
{
foreach (var record in records)
{
data_list1.Add(index, record.Channel1);
data_list2.Add(index, record.Channel2);
index++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
CreateSignalGraph(zedGraphControl1, data_list1, "Signal", "X", "Y");
}
}
private void CreateSignalGraph(
ZedGraphControl zgc,
PointPairList pointPairs,
string title = "Signal",
string x_axis_title = "X",
string y_axis_title = "Y",
bool legend_visibility = true,
SymbolType symbolType = SymbolType.None,
bool line_visibility = true)
{
GraphPane myPane = zgc.GraphPane;
myPane.Legend.IsVisible = legend_visibility;
myPane.CurveList.Clear();
// Set the titles and axis labels
myPane.Title.Text = title;
myPane.XAxis.Title.Text = x_axis_title;
myPane.YAxis.Title.Text = y_axis_title;
// Create a curve
LineItem myCurve = myPane.AddCurve("Signal", pointPairs, Color.Blue, symbolType);
myCurve.Line.IsVisible = line_visibility;
// Show the x axis grid
myPane.XAxis.MajorGrid.IsVisible = true;
// Make the Y axis scale red
myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
// Fill the axis background with a gradient
myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
// Fill the pane background with a gradient
myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);
// Add a zoom event handler
zgc.ZoomEvent += new ZedGraphControl.ZoomEventHandler(zgc_ZoomEvent);
zgc.AxisChange();
zgc.Refresh();
}
private void zgc_ZoomEvent(ZedGraphControl sender, ZoomState oldState, ZoomState newState)
{
// Optionally handle the zoom event here
}
private void Form1_Load(object sender, EventArgs e)
{
// Automatic arrange element size on window
window_width = this.Width;
window_height = this.Height;
setTag(this);
}
#region -- Automatic arrange element size on window --
private float window_width;
private float window_height;
private void setTag(Control cons)
{
foreach (Control con in cons.Controls)
{
con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
if (con.Controls.Count > 0) setTag(con);
}
}
private void setControls(float new_window_width_ratio, float new_window_height_ratio, Control cons)
{
foreach (Control con in cons.Controls)
{
if (con.Tag != null)
{
string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * new_window_width_ratio); // width
con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * new_window_height_ratio); // height
con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * new_window_width_ratio); // left
con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * new_window_height_ratio); // top
System.Single new_font_size = System.Convert.ToSingle(mytag[4]) * new_window_height_ratio; // font size
con.Font = new Font(con.Font.Name, new_font_size, con.Font.Style, con.Font.Unit);
if (con.Controls.Count > 0) setControls(new_window_width_ratio, new_window_height_ratio, con);
}
}
}
private void Form_Resize(object sender, EventArgs e)
{
setControls((this.Width) / window_width, (this.Height) / window_height, this);
}
#endregion
}
}
Channels
classusing CsvHelper.Configuration.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSV_PlottingApp { public class Channels { [Index(0)] public double Channel1 { get; set; } [Index(1)] public double Channel2 { get; set; } [Index(2)] public double Channel3 { get; set; } [Index(3)] public double Channel4 { get; set; } [Index(4)] public double Channel5 { get; set; } public override string ToString() { return Channel1.ToString() + '\t' + Channel2.ToString() + '\t' + Channel3.ToString() + '\t' + Channel4.ToString() + '\t' + Channel5.ToString(); } } }
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
What changes has been made in the code since last question?
I am trying to use
CsvHelper
to build an application for plotting CSV Data in this post.Why a new review is being asked for?
Please review all the revised code.
PointPairList
(which shouldn't have the word "List" in its name)? \$\endgroup\$PointPairList
is a class provided by ZedGraph library. \$\endgroup\$