Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda