2014-07-18 5 views
0

하나의 차트에 여러 Y 값을 플로팅하는 데 문제가 있습니다. 다음 코드는 단일 y 값에 대해 작동하지만 동일한 csv 데이터 파일에서 여러 y 값을 그릴 수 없습니다.하나의 CSV 파일에서 여러 y 값 계산

CSV 파일 형식은 : I 차트 CSV 파일의 모든 열 (Y) 데이터 포인트 대 날짜 스탬프를 표시 얻을 수 있지만 표시되지 3

날짜 스탬프, 값 1, 값 2, 값 다른 y 값.

예, 분명히 OOP 및 winforms에 새로운 것이지만 나는 노력하고 있습니다. 내가 프로그래밍 할 당시 PDP11의 Fortran이었습니다.

어쨌든, 차트하고 버튼되는 코드의이 조각은 여기 어딘가에서하지만 나는

양식을 찾고 답을 찾을 수 없어.

귀하의 도움에 감사드립니다.

7/27
이 코드는 작동하지만 여러 Y 값을로드하는 더 좋은 방법이 있습니까?

또한 코드는 개정판에서 분리되었습니다.

데이터 테이블에로드하는 경우 데이터 테이블을 차트에 바인딩하십시오. 이것이 메모리 공간을 차지하지 않을까요?

  private void button1_Click(object sender, EventArgs e) 
    { 
     // Full path to the data source file 
     string file = "testlog2.csv"; 
     string path = @"g:\"; 
     // Create a connection string. 
     string filepPathConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
      path + ";Extended Properties=\"Text;HDR=No;FMT=CSVDelimited\""; 
     // Create Connection Object 
     OleDbConnection myConnection = new OleDbConnection(filepPathConnectStr); 

     // Create a database command on the connection using query 
     string mySelectQuery = "Select * from " + file; 
     OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection); 

     // Open the connection and create the reader 
     myCommand.Connection.Open(); 
     OleDbDataReader myReader = myCommand.ExecuteReader(); 

     // Column 1 is a time value, column 2 is a double 
     // databind the reader to the chart using the DataBindXY method 
     Chart1.Series["Series1"].Points.DataBindXY(myReader, "0", myReader, "1"); 
     //Chart1.Series["Series2"].Points.DataBindY(myReader, "3"); 


     myReader.Close(); 

myConnection.Close();

} 

    private void checkBox1_CheckedChanged(object sender, EventArgs e) 
    { 
     // Full path to the data source file 
     string file = "testlog2.csv"; 
     string path = @"g:\"; 
     // Create a connection string. 
     string filepPathConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + 
      path + ";Extended Properties=\"Text;HDR=No;FMT=CSVDelimited\""; 
     // Create Connection Object 
     OleDbConnection myConnection = new OleDbConnection(filepPathConnectStr); 

     // Create a database command on the connection using query 
     string mySelectQuery = "Select * from " + file; 
     OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection); 

     // Open the connection and create the reader 
     myCommand.Connection.Open(); 
     OleDbDataReader myReader = myCommand.ExecuteReader(); 

     // Column 1 is a time value, column 2 is a double 
     // databind the reader to the chart using the DataBindY method 

     Chart1.Series["Series2"].Points.DataBindY(myReader, "3"); 

     myReader.Close(); 
     myConnection.Close(); 

    } 
    } 
} 

답변

0

데이터 바인딩 포인트는 1 열입니다. 여러 개의 y 열을 표시하려면 다음 두 가지 중 하나를 수행하십시오.

  1. 데이터 바인딩 대신 수동으로 포인트를 추가하십시오. 이것은 당신의 CSV의 모든 행을 반복하고 각 행에 대해 2 (또는 그 이상) 포인트를 추가 수반 :

    series.Points.AddXY(csvLine[0], csvLine[3]); 
    series.Points.AddXY(csvLine[0], csvLine[4]); 
    

당신은 구별하기 위해 다른 열에있는 점의 색상을 변경할 수 있지만 전설은 다른 색깔을 반영하지 않을 것이다.

  1. Series 개체를 여러 개 만들고 각 개체를 다른 y 열로 바인딩합니다.

    Series series2 = chart1.Series.Add("Series2"); 
    series2.Points.DataBindXY(myReader, "0", myReader, "4"); 
    

같은 뭔가 그런 당신을 위해 물건을 많이 처리로이 선호하는 방법 등, 다른 색을 가지고 series2의 속성을 설정할 수 있습니다.

+0

안녕하세요. 시간 내 주셔서 감사합니다. 이것은 당신이 염두에두고있는 것입니다. –

관련 문제