I'm using LiveCharts to display a barchart with stock levels. The unit names and stock levels are stored in an access database, and these values are retrieved and put into Lists.
The only thing that should really change is the value of stock so I wanted to track changes of the stock and update the database automatically, giving somewhat live data.
What I want to happen is for the chart to automatically update when the value (stock) changes in the database. I don't want to update the entire table, only the value that has changed so that the animations show on individual bars when they're changed.
I have the following code for loading my database values into the database:
string[] units = new string[30];
List<string> subs = new List<string>();
List<string> line = new List<string>();
List<double> min = new List<double>();
List<double> max = new List<double>();
List<ObservableValue> stock = new List<ObservableValue>();
bool initial = true;
private void LoadData()
{
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database\subDB.mdb";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
string strSQL = "SELECT * FROM Units";
OleDbCommand command = new OleDbCommand(strSQL, conn);
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (initial)
{
subs.Add(reader["Sub"].ToString());
line.Add(reader["Line"].ToString());
min.Add(Convert.ToDouble(reader["Minimum"]));
max.Add(Convert.ToDouble(reader["Maximum"]));
}
stock.Add(reader["Stock"]);
}
}
initial = false;
}
}
Chart setup:
public void chartSetup()
{
cartesianChart1.Series = new SeriesCollection
{
new ColumnSeries
{
Title = "Total",
Fill = System.Windows.Media.Brushes.Crimson,
Values = new ChartValues<ObservableValue>(stock)
}
};
cartesianChart1.AxisX.Add(new Axis
{
Title = "Unit",
FontSize = 15,
Separator = new Separator
{
Step = 1,
IsEnabled = false //disable it to make it invisible.
},
LabelsRotation = 45,
Labels = new ChartValues<string>(subs)
});
cartesianChart1.AxisY.Add(new Axis
{
Title = "Total",
FontSize = 15,
ShowLabels = true,
LabelFormatter = value => value.ToString("N")
});
}
My issue is that I'm unable to implement ObservableValue for stock. I get the message: "Cannot convert from 'object' to LiveCharts.Defaults.ObservableValue
A LiveCharts Observable value expects to be initialized with a double
so you need to convert the value of stock
to that, and add an ObservableValue to the list rather than a raw value::
stock.Add(new ObservableValue(Convert.ToDouble(reader["Stock"])));