Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
How do I choose the selected values from MongoDB?

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},
    },
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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:

enter image description here

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)});
over 4 years ago · Santiago Trujillo Report

0

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>
},
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!