2017-08-23 22 views
0

このjson形式に従ってデータを取得する必要があります。LINQクエリを使用して必要な出力を取得する問題

series: [{ 
    name: 'Marriage', 
    data: [1, 2, 3] // Sample Data 
}, { 
    name: 'Chess', 
    data: [2, 2, 3] 
}, { 
    name: 'Ludo', 
    data: [3, 4, 4] 
}] 

は、私はここにhttp://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/bar-stacked/

にデバイスIDからして、グループを使用して、結果を得るために、forループを使用している私は何をしようとしたグラフを作成する必要があります。しかし、私はここで必要な出力を得るにはかなり詰まっています。

これまで私がこれまでに試したことは次のとおりです。

void Main() 
{ 
DateTime currentDate = DateTime.UtcNow.Date.AddDays(-30); 
var currentMonthData = Device_GameDevices.Where(x => x.CreatedDate >= currentDate).ToList(); 
// Get total game list 
var gamesList = currentMonthData.Select(x => x.GameName).Distinct(); 

// Group the data as per the device id 
var groupedData = from gameData in currentMonthData 
        group gameData by gameData.DeviceID 
        into egroup 
        select new { 
         Game = egroup.Key, 
         Data = from bug in egroup 
           group bug by bug.GameName into g2 
           select new { Name = g2.Key, HoursPlayed = g2.Sum(x => (x.EndTime - x.StartTime).TotalMinutes/60) } 
        }; 

Console.Write(groupedData); 

List<DashboardVM.ChartData> chartDatas = new List<DashboardVM.ChartData>(); 
List<double> hourResultList = new List<double>(); 

foreach(var item in groupedData) 
{ 
    var chart = new DashboardVM.ChartData(); 
    foreach(var gameItem in gamesList) 
    { 
     chart.GameNameResult = gameItem; 
     foreach(var groupedDataItem in item.Data) 
     {    
      if(gameItem == groupedDataItem.Name) 
      { 
       hourResultList.Add(groupedDataItem.HoursPlayed); 
      } 
      else 
      { 
       hourResultList.Add(0.0); 
      } 
     } 

     chart.HoursPlayed = hourResultList; 
    } 

    chartDatas.Add(chart); 
} 

Console.Write(chartDatas); 
} 

public class DashboardVM{ 
public class ChartData{ 
    public string GameNameResult{get;set;} 
    public List<double> HoursPlayed{get;set;} 
} 
} 
+0

はどのようにあなたが提供したサンプルデータは、あなたが示されてきたテーブル内のデータに対応していますか? – StriplingWarrior

+0

サンプルデータについては、X軸、Y軸、およびスタッキングについてはどう思いますか?私はX軸が再生される時間を想定し、Y軸はGameNameになり、積み重ねはDeviceIdになりますか? –

+0

はい@RobertMcKee – user3127109

答えて

1
public class Chart 
{ 
    public string type { get; set; } 
} 

public class Title 
{ 
    public string text { get; set; } 
} 

public class XAxis 
{ 
    public List<string> categories { get; set; } 
} 

public class Title2 
{ 
    public string text { get; set; } 
} 

public class YAxis 
{ 
    public int min { get; set; } 
    public Title2 title { get; set; } 
} 

public class Legend 
{ 
    public bool reversed { get; set; } 
} 

public class Series 
{ 
    public string stacking { get; set; } 
} 

public class PlotOptions 
{ 
    public Series series { get; set; } 
} 

public class Series2 
{ 
    public string name { get; set; } 
    public List<double> data { get; set; } 
} 

public class RootObject 
{ 
    public Chart chart { get; set; } 
    public Title title { get; set; } 
    public XAxis xAxis { get; set; } 
    public YAxis yAxis { get; set; } 
    public Legend legend { get; set; } 
    public PlotOptions plotOptions { get; set; } 
    public List<Series2> series { get; set; } 
} 

void Main() 
{ 
    var Device_GameDevices = new[] { 
    new {ID=1,CreatedDate=DateTime.Parse("8/23/2017 06:07:30"),DeviceID="Desktop12",EndTime=DateTime.Parse("8/23/2017 06:06:30"),GameName="CyberGunner",StartTime=DateTime.Parse("8/23/2017 06:03:45")}, 
    new {ID=2,CreatedDate=DateTime.Parse("8/23/2017 07:14:01"),DeviceID="A12"  ,EndTime=DateTime.Parse("8/23/2017 11:14:01"),GameName="Marriage" ,StartTime=DateTime.Parse("8/23/2017 07:14:01")}, 
    new {ID=3,CreatedDate=DateTime.Parse("8/23/2017 07:14:02"),DeviceID="A12"  ,EndTime=DateTime.Parse("8/23/2017 08:14:01"),GameName="Marriage" ,StartTime=DateTime.Parse("8/23/2017 07:14:02")}, 
    new {ID=4,CreatedDate=DateTime.Parse("8/23/2017 09:14:01"),DeviceID="A12"  ,EndTime=DateTime.Parse("8/23/2017 09:14:01"),GameName="Chess"  ,StartTime=DateTime.Parse("8/23/2017 07:14:03")}, 
    new {ID=5,CreatedDate=DateTime.Parse("8/23/2017 07:14:03"),DeviceID="A12"  ,EndTime=DateTime.Parse("8/23/2017 10:14:01"),GameName="Marriage" ,StartTime=DateTime.Parse("8/23/2017 07:14:03")}, 
    new {ID=6,CreatedDate=DateTime.Parse("8/23/2017 09:57:28"),DeviceID="B12"  ,EndTime=DateTime.Parse("8/23/2017 10:57:28"),GameName="Marriage" ,StartTime=DateTime.Parse("8/23/2017 09:57:28")}, 
    }; 
    DateTime currentDate=DateTime.UtcNow.Date.AddDays(-30); 
    var currentMonthData=Device_GameDevices 
    .Where(x=>x.CreatedDate>=currentDate) 
    .ToList(); 

    // Get total game list 
    var gamesList=currentMonthData 
    .Select(x=>x.GameName) 
    .Distinct() 
    .ToList(); 

    var chart=new RootObject 
    { 
    chart=new Chart{ type="bar"}, 
    title=new Title{ text="My title" }, 
    xAxis=new XAxis { categories=gamesList }, 
    yAxis=new YAxis { min=0, title=new Title2 {text="Total Game Time"}}, 
    legend=new Legend {reversed=true}, 
    plotOptions=new PlotOptions { series=new Series {stacking="normal"}}, 
    series=currentMonthData 
     .GroupBy(d=>new {d.DeviceID,d.GameName}) 
     .Select(d=>new { 
     DeviceID=d.Key.DeviceID, 
     GameName=d.Key.GameName, 
     HoursPlayed=d.Sum(x=>(x.EndTime - x.StartTime).TotalMinutes)/60 
     }) 
     .GroupBy(d=>d.DeviceID) 
     .Select(d=>new Series2 { 
     name=d.Key, 
     data=gamesList 
      .GroupJoin(d,a=>a,b=>b.GameName,(a,b)=>new {GameName=a,HoursPlayed=b.Sum(z=>z.HoursPlayed)}) 
      .OrderBy(x=>gamesList.IndexOf(x.GameName)) 
      .Select(x=>x.HoursPlayed) 
      .ToList() 
     }).ToList() 
    }; 
    chart.Dump(); 
} 

これはシリーズがどのように見えるかです:

enter image description here

+0

LINQクエリを使ってゲームがプレイされていないデバイスのゲーム値0をどこかで得ることができたらと思っていました。たとえば、B12デバイスでは、チェスゲームやサイバーガナーゲームはありません – user3127109

+0

http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demoを見ましたか?/bar-stacked/mate? – user3127109

+0

はい、完了したら、グラフオブジェクトをシリアル化してHighchartsに送ることができます。正確な構文を検証するためのテストプロジェクトを作成するだけです。数分で完了する必要があります。 –

関連する問題