Livecharts C# Winforms Getting Data from SQL

Viewed 104

There's no proper documentation for LiveCharts and I really don't know how to start with this. I have a SQL query to return a result like this:

|   Date   | TotalReservation |
|----------|------------------|
|2021-06-05|         3        |
|2021-06-06|         2        |
|2021-06-07|         3        |
|2021-06-08|         4        |
|2021-06-09|         0        |

I intend to create a Line Series in Winforms using the data above. I am really new to C#, and reading this documentation https://lvcharts.net/App/examples/v1/WinForms/Line does not really provide me with any guide to achieve the result. YouTube tutorials are mostly for Column Series and limited tutorial on Line Series.

Any help would be greatly appreciated. Thank you.

1 Answers

I've managed to do it after some experimenting some how...Hope this helps anyone who visits this thread! :)

  var data = db.GetPastReservation();
  LineSeries line = new LineSeries() { DataLabels = true, Values = new ChartValues<int>(), LabelPoint
  = point => point.Y.ToString() };
  Axis ax = new Axis();
  ax.Labels = new List<string>();
  foreach (var x in data)
  {
      line.Values.Add(x.TotalReservation.Value);
      string dates = x.Date.ToString().Replace("12:00:00 AM", "");
      ax.Labels.Add(dates);
  }
  resChart.Series.Add(line);
  resChart.AxisX.Add(ax);
  resChart.AxisY.Add(
      new Axis
      {
          LabelFormatter = value => value.ToString()
      });

I saved the SQL query as a StoredProcedure and call it in the Form_Load event. I had to replace do a .Replace() as the Date returns Date and the start time of the day. For each row in the Result, it adds the values to the Line Series.

Related