15.Visifire圖表控件的使用二(DataPoint點擊事件和Legend文字標註欄的點擊事件)

        圖表應用於表現數據量,進行直觀的對比,可是在某一些領域中若是數據之間大小差別過大,那麼會出現某一些數據由於太小,而沒法讓用戶看見的狀況。例如在統 計一組用戶電腦的網絡發包量的時候,有一些用戶開啓電腦幾十個小時,有一些用戶開啓電腦幾秒鐘。很明顯用戶開機幾十個小時的發包量巨大,而開機幾秒鐘的發 包量極小,若是放在一個Visifire的圖標中組成一個統計列的時候,發包量小的電腦幾乎看不見了。這種狀況下,咱們就能夠經過點擊文字標註欄的 Legend文字來肯定某一個在圖表上看不見的用戶電腦的發包量。sql

        首先咱們設置一個實體類,該類包含(ComputerName,NetWorkNum)兩個屬性,分別代碼電腦名和電腦網絡發包量:網絡

 

  
  
           
  
  
  1. /// <summary> 
  2.   /// 電腦信息 
  3.   /// </summary> 
  4.   public class  ComputerInfomation 
  5.   { 
  6.       string _ComputerName; 
  7.       string _NetWorkNum; 
  8.       /// <summary> 
  9.       /// 電腦名稱 
  10.       /// </summary> 
  11.       public string ComputerName 
  12.       { 
  13.           get { return _ComputerName; } 
  14.           set { _ComputerName = value; } 
  15.       } 
  16.       /// <summary> 
  17.       /// 網絡發送包 
  18.       /// </summary> 
  19.       public string NetWorkNum 
  20.       { 
  21.           get { return _NetWorkNum; } 
  22.           set { _NetWorkNum = value; } 
  23.       } 
  24.   } 

        再實例化該類造成多個實體類對象集合,MainPage.xaml.cs的構造函數中敲入代碼以下:ide

 

  
  
           
  
  
  1. ComputerList = new List<ComputerInfomation>()  
  2.           { 
  3.           new ComputerInfomation(){ComputerName="張三的電腦", NetWorkNum="32143242223"}, 
  4.           new ComputerInfomation(){ComputerName="李四的電腦", NetWorkNum="23432423"}, 
  5.           new ComputerInfomation(){ComputerName="王五的電腦", NetWorkNum="12342342344"}, 
  6.           new ComputerInfomation(){ComputerName="劉六的電腦", NetWorkNum="562342"}, 
  7.           new ComputerInfomation(){ComputerName="林七的電腦", NetWorkNum="55353453445"}, 
  8.           new ComputerInfomation(){ComputerName="馬林的電腦", NetWorkNum="2454555543"
  9.           }; 
  10.           BindChart(ComputerList); 

        如今用戶電腦數據已經準備好了,咱們開始製做一個函數,此函數建立一個圖表而且設置相應的Legend文字標註欄的事件綁定:函數

 

  
  
           
  
  
  1. List<ComputerInfomation> ComputerList = new List<ComputerInfomation>(); 
  2. /// <summary> 
  3. /// 綁定一個圖標 
  4. /// </summary> 
  5. /// <param name="computerList">用戶電腦類實體集合</param> 
  6. public void BindChart( List<ComputerInfomation> computerList) 
  7. Chart chart = new Chart(); 
  8. chart.Width = 400; 
  9. chart.Height = 550; 
  10. chart.Name = "Chart"
  11. chart.SetValue(Canvas.LeftProperty, 30.0); 
  12. chart.SetValue(Canvas.TopProperty, 30.0); 
  13. chart.Theme = "Theme1";//設置皮膚 
  14. chart.BorderBrush = new SolidColorBrush(Colors.Gray); 
  15. chart.AnimatedUpdate = true
  16. chart.CornerRadius = new CornerRadius(7); 
  17. chart.ShadowEnabled = true
  18. chart.Padding = new Thickness(4, 4, 4, 10); 
  19.  
  20. #region 設置Title 
  21. Title title = new Title(); 
  22. title.Text = "電腦網絡發包統計"
  23. chart.Titles.Add(title); 
  24. #endregion 
  25.  
  26. #region 設置AxesX 
  27. Axis xAxis = new Axis(); 
  28. xAxis.Title = "用戶電腦"
  29. chart.AxesX.Add(xAxis); 
  30. #endregion 
  31.  
  32. #region 設置AxesY 
  33. Axis yAxis = new Axis(); 
  34. yAxis.Title = "用戶網卡發送包"
  35. yAxis.Prefix = "發送:"
  36. yAxis.Suffix = "包"
  37. chart.AxesY.Add(yAxis); 
  38. #endregion 
  39.  
  40. #region 設置PlotArea 
  41. PlotArea plot = new PlotArea(); 
  42. plot.ShadowEnabled = false
  43. chart.PlotArea = plot; 
  44. #endregion 
  45.  
  46. #region 設置Legends 
  47. Legend legend = new Legend(); 
  48. //Legend文字標註欄綁定一個事件Legend_MouseLeftButtonDown 
  49. legend.MouseLeftButtonDown += new EventHandler<LegendMouseButtonEventArgs>(Legend_MouseLeftButtonDown); 
  50. chart.Legends.Add(legend); 
  51. #endregion 
  52. #region 
  53. Visifire.Charts.ToolTip tip = new Visifire.Charts.ToolTip(); 
  54. tip.VerticalAlignment = VerticalAlignment.Bottom; 
  55. chart.ToolTips.Add(tip); 
  56. #endregion 
  57. #region 建立數據序列和數據點 
  58. foreach (ComputerInfomation cominfo in computerList) 
  59. DataSeries dseries = new DataSeries(); 
  60. //設置一個數據序列的LengendText值爲ComputerName 
  61. dseries.LegendText = cominfo.ComputerName; 
  62. //設置圖表的類型爲RenderAs.StackedColumn 
  63. dseries.RenderAs = RenderAs.StackedColumn; 
  64. //設置一個數據點 
  65. DataPoint dpointUpload = new DataPoint(); 
  66. //數據點的Y座標值 
  67. dpointUpload.YValue =double.Parse(cominfo.NetWorkNum); 
  68. //數據點的Tag值也爲電腦名稱,用於數據點被點擊後對比判斷當前點擊的點 
  69. dpointUpload.Tag = cominfo.ComputerName; 
  70. //設置數據點被點擊以後觸發事件Dpoint_MouseLeftButtonDown 
  71. dpointUpload.MouseLeftButtonDown += new MouseButtonEventHandler(Dpoint_MouseLeftButtonDown); 
  72. dseries.DataPoints.Add(dpointUpload); 
  73. chart.Series.Add(dseries); 
  74. #endregion 
  75. #region 設置遮罩,將Visifire的LOGO遮擋住。 
  76. StackPanel sp = new StackPanel(); 
  77. sp.Width = 145; 
  78. sp.Height = 15; 
  79. sp.Margin = new Thickness(0, 3, 3, 0); 
  80. sp.VerticalAlignment = VerticalAlignment.Top
  81. sp.HorizontalAlignment = HorizontalAlignment.Right
  82. sp.Background = new SolidColorBrush(Colors.White); 
  83. #endregion 
  84. LayoutRoot.Children.Add(chart); 
  85. LayoutRoot.Children.Add(sp); 

        關鍵在於Lengend事件的設置,那麼下面咱們貼出Lengend被點擊事件和DataPoint被點擊事件的處理函數:spa

 

  
  
           
  
  
  1. /// <summary> 
  2.   /// DataPoint被點擊執行事件 
  3.   /// </summary> 
  4.   /// <param name="sender"></param> 
  5.   /// <param name="e"></param> 
  6.   void Dpoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
  7.   { 
  8.       //接收到當前被點擊的LengendText的值 
  9.       DataPoint dpoint = sender as DataPoint; 
  10.       string str = dpoint.Tag.ToString(); 
  11.       foreach (ComputerInfomation cominfo in ComputerList) 
  12.       { 
  13.           if (str == cominfo.ComputerName) 
  14.           { 
  15.               MessageBox.Show(cominfo.ComputerName + "網絡發送:" + cominfo.NetWorkNum + "數據包"); 
  16.           } 
  17.       } 
  18.   } 
  19.   /// <summary> 
  20.   /// Legend文字被點擊執行的事件 
  21.   /// </summary> 
  22.   /// <param name="sender"></param> 
  23.   /// <param name="e"></param> 
  24.   private void Legend_MouseLeftButtonDown(object sender, LegendMouseButtonEventArgs e) 
  25.   { 
  26.       //接收到當前被點擊的LengendText的值 
  27.       string str = e.DataSeries.LegendText.ToString(); 
  28.       foreach (ComputerInfomation cominfo in ComputerList) 
  29.       { 
  30.           if (str == cominfo.ComputerName) 
  31.           { 
  32.               MessageBox.Show(cominfo.ComputerName + "網絡發送:" + cominfo.NetWorkNum + "數據包"); 
  33.           } 
  34.       } 
  35.   } 

        如此就解決了本文開頭所述的圖標繪製的問題。本實例採用VS2010+Silverlight 4.0編寫,點擊 SLTOVisiFire.rar 下載源碼。運行效果以下:對象

 

相關文章
相關標籤/搜索