0

I'm working on making a chart and looking up the DB with C# WPF. I succeeded in making the DB search and chart.

Is it possible to make a graph of this shape on a live chart? Can I read the value and make it print out in the middle according to the time?

enter image description here

Please enter a description of the picture. I'm doing this right now. It's not the shape I want because it's coming out like the bottom. ChatGPT is possible, but I don't think it's possible because the line automatically starts at the left or right end. Is it possible?

enter image description here

Even if I read the data later, I don't know how to put in the values after time. I need 5 lines.

    public List<SensingDataPoint> GetSensingData(int _iCount)
    {
      List<SensingDataPoint> dataPoints = new List<SensingDataPoint>();
      var query = $"SELECT {sSensorId}, {sSensorTime}, {sSensorValue} FROM {sSensorTable} ORDER BY {sSensorTime} DESC LIMIT {_iCount}";

      using (var connection = new MySqlConnection(sConnectionString))
      {
        connection.Open();
        using (var command = new MySqlCommand(query, connection))
        {
          using (var reader = command.ExecuteReader())
          {
            while (reader.Read())
            {
              dataPoints.Add(new SensingDataPoint
              {
                ID = reader.GetInt32(0),  // ID 컬럼은 인덱스 0
                Time = reader.GetDateTime(1),  // Time 컬럼은 인덱스 1
                Value = reader.GetDouble(2)  // Value 컬럼은 인덱스 2
              });
            }
          }
        }
      }
      return dataPoints;
    }

  public class SensingDataPoint
  {
    public int ID { get; set; }
    public DateTime Time { get; set; }
    public double Value { get; set; }
  }
    // 실시간 데이터 업데이트 메서드
    private void vUpdateChartData()
    {
      // X축 레이블 설정
      if (SelectedData == MainViewModel.DataSelection.Value)
      {
        // 각 ID의 센서 데이터를 최신 데이터로 가져오기
        var SensorDataPoints = dbHelper.GetSensingData(iReadCount);

        // UI 스레드에서 차트 업데이트
        Application.Current.Dispatcher.Invoke(() =>
        {
          ID1SensorValues.Clear();
          ID2SensorValues.Clear();
          ID3SensorValues.Clear();
          ID4SensorValues.Clear();
          ID5SensorValues.Clear();
          vUpdateSeries(SensorDataPoints);

        });

      }
    }
    private void vUpdateSeries(List<SensingDataPoint> dataPoints)
    {
      // 기존 값 초기화
      foreach (var point in dataPoints)
      {
        if (selectedData == DataSelection.Value)
        {
          TimeLabels.Add(point.Time.ToString("HH:mm:ss"));
          switch (point.ID)
          {
            case 1:
              ID1SensorValues.Add(point.Value);
              ID2SensorValues.Add(0);
              ID3SensorValues.Add(0);
              ID4SensorValues.Add(0);
              ID5SensorValues.Add(0);
              break;
            case 2:
              ID1SensorValues.Add(0);
              ID2SensorValues.Add(point.Value);
              ID3SensorValues.Add(0);
              ID4SensorValues.Add(0);
              ID5SensorValues.Add(0);
              break;
            case 3:
              ID1SensorValues.Add(0);
              ID2SensorValues.Add(0);
              ID3SensorValues.Add(point.Value);
              ID4SensorValues.Add(0);
              ID5SensorValues.Add(0);
              break;
            case 4:
              ID1SensorValues.Add(0);
              ID2SensorValues.Add(0);
              ID3SensorValues.Add(0);
              ID4SensorValues.Add(point.Value);
              ID5SensorValues.Add(0);
              break;
            case 5:
              ID1SensorValues.Add(0);
              ID2SensorValues.Add(0);
              ID3SensorValues.Add(0);
              ID4SensorValues.Add(0);
              ID5SensorValues.Add(point.Value);
              break;
            default:
              break;
          }
        }
      }
    }
3
  • Can you add the link/repo of the chart library you are using Commented Jan 3 at 9:31
  • You're capturing data for five (5) "data series" (time and value). If you want to "read it later", you need to save those series. You could have one "data set" that has 5 "values" for each datetime; but that's not "natural" in terms of managing if they're actually different sensors. Commented Jan 3 at 16:39
  • Could you describe what you mean by "Please enter a description of the picture"? I wonder if this is an image alt text, and it is not part of your question. Commented Jan 4 at 16:03

1 Answer 1

0

because the line automatically starts at the left or right end

That's because you fill in the data for every timestamp, i.e.:

          TimeLabels.Add(point.Time.ToString("HH:mm:ss"));
          switch (point.ID)
          {
            case 1:
              ID1SensorValues.Add(point.Value);
              ID2SensorValues.Add(0); // this adds value 0 at timestamp point.Time, even if there's no data for ID 2
              ID3SensorValues.Add(0); // this also adds value 0 to for ID 3
              ID4SensorValues.Add(0); // etc.
              ID5SensorValues.Add(0);
              break;

That's why your lines go from the very left to the very right.

If you only want to keep those parts of the plot that has actual value, but remove the rest (in the image below I marked the different areas for yellow plot in your two images), you cannot simply insert 0 as a value, because it will be interpreted as a normal value of the series.

desired difference

Instead, insert a special value, which the library understands as "empty", the values are different depending on version you are using (also terminology is slightly different):

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.