2017-08-23 53 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和使用GROUP BY和使用循環得到的結果中創建圖表。但我在這裏很困難獲得所需的輸出。

這是我到目前爲止嘗試過的。

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設備中,沒有國際象棋遊戲和Cyber​​ Gunner遊戲 – user3127109

+0

您是否看過http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/bar-stacked/mate? – user3127109

+0

是的,當您完成後,您可以序列化圖表對象並將其提供給Highcharts。只需創建一個測試項目來驗證確切的語法。應該在幾分鐘內完成。 –