将csv文件导入DataTable并使用Chart绑定列 [英] import csv file to DataTable and bind columns with Chart
问题描述
我有一个已导入DataTable的csv文件。
我想绘制数据表中的两列
我希望x轴的编号为0 1 2 3 4 5 6 7 8 9 10
和Y轴从-5到5
如果我错过了一些我需要设置的设置,请告诉我。
正如我在我的图表中我得到的我的x中的CpK_refrence列值asis
http:/ /imgur.com/3mcF9My [ ^ ]
CSV文件: https://drive.google.com / file / d / 0BxsoIsUEjoORYmo0Q3dWWG14SkU / edit?usp = sharing [ ^ ]
I have a csv file which I have imported to DataTable.
I want to plot the two columns present in the data table
I want the x axis to be number as 0 1 2 3 4 5 6 7 8 9 10
and Y axis from -5 to 5
Please tell me if I am missing some settings that I need to set.
As my in my graph I am getting CpK_refrence column values in my x asis
http://imgur.com/3mcF9My[^]
CSV file : https://drive.google.com/file/d/0BxsoIsUEjoORYmo0Q3dWWG14SkU/edit?usp=sharing[^]
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
private DataTable csvData;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
// To list only csv files, we need to add this filter
openFileDialog.Filter = "|*.csv";
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
tbCSVPath.Text = openFileDialog.FileName;
}
csvData = GetDataTableFromCSVFile(tbCSVPath.Text);
}
private static DataTable GetDataTableFromCSVFile(string csvfilePath)
{
DataTable csvData = new DataTable();
using (TextFieldParser csvReader = new TextFieldParser(csvfilePath))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
//Read columns from CSV file, remove this line if columns not exits
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
return csvData;
}
private void button2_Click(object sender, EventArgs e)
{
chart1.DataSource = csvData;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisX.Minimum = -5;
chart1.ChartAreas[0].AxisX.Maximum = 5;
chart1.Series["Series2"].YValueMembers = "Delta_Sigma";
chart1.Series["Series2"].XValueMember = "CpK_Refrence";
chart1.DataBind();
}
}
}
推荐答案
问题是,当您从csv读取数据时,它将所有数据添加为字符串,然后图表将值读取为字符串,这就是为什么没有Chart1.ChartAreas(0 ).AxisX.Maximum = 10改变轴。
列类型必须改变所以
The issue is that when you are reading the data from the csv it is adding all the data as string and the chart is then reading the values as string thats why none of the Chart1.ChartAreas(0).AxisX.Maximum = 10 changes the axis.
The column type have to be changed so
private void button2_Click(object sender, EventArgs e)
{
DataTable dtCloned = csvData.Clone();
dtCloned.Columns["Delta_Sigma"].DataType = typeof(double);
dtCloned.Columns["CpK_Refrence"].DataType = typeof(double);
//dtCloned.Columns[3].DataType = typeof(double);
foreach (DataRow row in csvData.Rows)
{
dtCloned.ImportRow(row);
}
chart1.DataSource = dtCloned;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisX.Minimum = -5;
chart1.ChartAreas[0].AxisX.Maximum = 5;
chart1.Series["Series2"].YValueMembers = "Delta_Sigma";
chart1.Series["Series2"].XValueMember = "CpK_Refrence";
chart1.DataBind();
}
这篇关于将csv文件导入DataTable并使用Chart绑定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!