Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();
           
        }
        


        
    }
}
Posted

1 solution

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();

  }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900