簡體   English   中英

如何在WPF圖表中顯示以下結果

[英]How to display the following result in WPF chart

在我的WPF應用程序中,我有一個以下查詢,以將選定的日期范圍顯示為“日期”,並將每天要處理的服務器總數顯示為“金額”。

  var query_1 = 

  (this.db.Servers
  .Where(a => (a.Date >= fromDP.SelectedDate.Value && a.Date <= toDP.SelectedDate.Value)
   && ((a.ServerID == "ServerID1" || a.ServerID == "ServerID2") && a.Type == "Complete"))
  .GroupBy(a => EntityFunctions.TruncateTime(a.Date))
  .OrderBy(a => a.Key)
  .Select(g => new { Date = g.Key, Amount = g.Count() }))
  .ToList();

綁定到ListBox的ItemsSource時返回以下輸出(僅用於檢查查詢是否正常工作)

結果

現在如何將這個結果, 日期金額綁定到WPF圖表的X和Y軸。 (日期的X軸,金額的Y軸)。

圖表可以是“氣泡系列” /“線系列”或“柱系列”。

該博客文章可能滿足您的需求。

http://www.itdevspace.com/2010/09/wpf-toolkit-datagrid-chart-example.html

基本上,您將從LINQ查詢中檢索到的信息放入列表中,就像您擁有的列表一樣(上面的博客文章中的示例使用ObservableCollection並可能用於Notification),然后將Chart ItemSource屬性設置為Collection,將IndependentValue設置為日期和DependentValue到金額。

<charting:LineSeries
    ItemsSource="{Binding Path=Data}"
    IndependentValueBinding="{Binding Date}"
    DependentValueBinding="{Binding Amount}"/>

我沒有在匿名類型上嘗試過此操作,因此您可能需要更改查詢邏輯以使其更加明確。 上面的xaml代碼期望DataContext具有名稱為Data的Collection,其中包含具有屬性Date和Amount的項目。

我在msdn論壇中找到了解決方案。 對於那些將來希望參考該解決方案的人。

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2c4fc325-83c1-4b07-b77a-bc7070c558f3/?prof=required

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM