I have a table named tblWeeklyAudit which has more than one rows data. I want to read it and show it on the live chart. code is shown below using.
using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
using static MAS.clsPUB;
namespace MAS.Windows
{
/// <summary>
/// Interaction logic for Dash.xaml
/// </summary>
public partial class Dash : Window
{
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
//public Func<double, string> YFormatter { get; set; }
public Dash()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
double test =0;
if (CON.State == ConnectionState.Open)
{
CON.Close();
}
CON.ConnectionString = ConfigurationManager.ConnectionStrings["conDB"].ConnectionString;
CON.Open();
CMD = new SqlCommand("select * from tblWeeklyAudit", CON);
RDR = CMD.ExecuteReader();
if (RDR.Read())
{
test = Convert.ToDouble(RDR["Defects"]);
}
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Values = new ChartValues<double> { test }
},
};
Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May" };
DataContext = this;
}
}
}
Chart Values are given by this line and I have called the table tblWeeklyAudit and from that defect table. It has several float value rows
Values = new ChartValues<double> { test }
Add the double values to a List<double>
that you then pass to the constructor of the ChartValues<double>
class:
private void LoadData()
{
List<double> allValues = new List<double>();
if (CON.State == ConnectionState.Open)
{
CON.Close();
}
CON.ConnectionString = ConfigurationManager.ConnectionStrings["conDB"].ConnectionString;
CON.Open();
CMD = new SqlCommand("select * from tblWeeklyAudit", CON);
RDR = CMD.ExecuteReader();
while (RDR.Read())
{
allValues.Add(Convert.ToDouble(RDR["Defects"]));
}
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Values = new ChartValues<double>(allValues)
}
};
Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May" };
DataContext = this;
}