Given that my data structure in MongoDB is like that the above . How do I use them for the below codes where I would like to use NodeId as my title and WT's value as my chart values. Do ignore the inputs I have input into title and Values for the ChartValues.
new LineSeries
{
Title = "Screws",
Values = new ChartValues<double> {4.5, 4.5, 4.45, 4.4, 4.4, 4.35, 4.35},
},
I will assume, that you have a model class for your data, if not, i suggest to create one. My model for your json looks like:
public class Value
{
public ObjectId _id { get; set; }
public Event @event { get; set;}
}
public class Event
{
public string NodeId {get;set;}
public string FirmwareVER {get;set;}
public double SignalSTR {get;set;}
public double battery {get;set;}
public double CEL {get;set;}
public double WT {get;set;}
public string Onlinestat {get;set;}
public double timeStamp {get;set;}
}
In that case you could aggregate your data from collection:
var collection = db.GetCollection<Value>("chart");
var res = collection.Aggregate()
.Group(x => x.@event.NodeId,
x => new {Name = x.Key, WTs = x.Select(r => r.@event.WT).ToList()})
.ToList();
That would be result for some sample data i have used:
After that you could get the data to your charts, something like:
res.Select(r => new LineSeries{Title = r.Name, Values = new ChartValues<double>(r.WTs)});
One of the best features of LiveCharts is that you can plot any type you want, and you are type-safe, considering you want to Plot the WT property with a given IEnumerable of Value type.
public class Value
{
public ObjectId _id { get; set; }
public Event @event { get; set;}
}
public class Event
{
public string NodeId {get;set;}
public double WT {get;set;}
}
You can teach the library to plot any type you want, in this case lets teach it to plot the WT property from the Value type:
var mapper = Mappers.XY<Value>()
.X((index,value) => index)
.Y((i,v) => (double) v.@event.WT);
Charting.For<Value>(mapper);
So in other words this means, every time the library finds a ChartValues<Value> instance, it will use the mapper we set. The mapper is really intuitive, it means we will use a zero based index as X coordinate, and the WT property as Y.
This line normally needs to be added when you application starts, there are many ways to teach the library how to plot a custom type, for more information pelase see https://lvcharts.net/App/examples/v1/wpf/Types%20and%20Configuration
And that is all, now you can directly plot Value type
IEnumerable<Value> dataSource = // source from data base...
new LineSeries
{
Title = "Screws",
Values = dataSource.AsChartValues(), //converts the source to
//an instance of ChartValues<Value>
},