1

I'm pretty new to MVC and have some problem with Linq, I think the solution would be pretty simple.

I have a table in a database with the following variables;

Name - string

Value - float

Time- Datetime

Now i want to make a specific name into two arrays, one containing the Values and the other one the Time so it will be easy to plot. I have made a controller where i have manage to access the database and get the specific name out with this code;

        var Names = db.Graphs
                        .Where(r => r.Name.Contains(Name) ||
                                    string.IsNullOrEmpty(Name))
                        .Take(10)

But im completely stuck on how to itterate through the database, get two arrays and send it to the View where i have my Highchart-plot.

Javascript;

  series: [{
            name: 'NameOn',
            data: [23.3, 18.3, 13.9, 9.6]   <--- Where i want to insert my data
        }]

Any idea on how to retrieve the data?

4

1 に答える 1

2

不規則なTimeChartデータ形式の場合、次のようになります

public static Series GetIrregularTimeChartData()
{
    List<Series> Series = new List<Series>();

    // get data from db and convert it to chart data (name, value, date)
    var chartSeries = db.Graphs.GroupBy(x => new { x.Name })
                      .Select(g => new
                      {
                          Name = g.Key,
                          Data = g.Select(x => x.Value).ToArray(),
                          Date = g.Select(x => x.Date).ToArray()
                      }).ToArray();

    // create 2D array => [value, date]
    foreach (var item in chartSeries)
    {
        int lenght = item.Data.Count();
        object[,] data = new object[lenght, 2];
        for (int i = 0; i < lenght; i++)
        {
            data[i, 0] = item.Date[i];
            data[i, 1] = item.Data[i];
        }
        Series localSeries = new Series { Name = item.Name, Data = new Data(data) };
        Series.Add(localSeries);
    }

    return Series;
}

このデータをチャート データ ソースに設定します。詳細については、このページの例を使用できます。

于 2013-02-11T13:52:47.510 に答える