原創|其它|編輯:郝浩|2012-10-19 16:47:20.000|閱讀 494 次
概述:Visifire圖表控件對有大差異數據的表現技巧,本文將以在統計一組用戶電腦的網絡發包量的時候為例
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
好的圖表不僅能夠起到美化布局的作用,而且對于數據的展示也會更加的形象直觀,但是有時候也會出現這種情況,比如說數據的差異比較大的情況,在這種情況下呢,有的非常小的數據可能在圖表中就不能夠展示出來。
最近剛好在看Visifire的圖表,發現里面的文字標注欄的Legend點擊事件就可以有效的避免這個問題。
下面就以在日常生活中比較常見的電腦網絡發包量為例來進行說明,這是我在網上看見的一個例子:
例如在統計一組用戶電腦的網絡發包量的時候,有一些用戶開啟電腦幾十個小時,有一些用戶開啟電腦幾秒鐘。很明顯用戶開機幾十個小時的發包量巨大,而開機幾秒鐘的發包量極小,如果放在一個Visifire的圖標中組成一個統計列的時候,發包量小的電腦幾乎看不見了。這種情況下,我們就可以通過點擊文字標注欄的Legend文字來確定某一個在圖表上看不見的用戶電腦的發包量。
第一步:設置一個實體類,該類包含(ComputerName,NetWorkNum)兩個屬性,分別代碼電腦名和電腦網絡發包量:
/// <summary> /// 電腦信息 /// </summary> public class ComputerInfomation { string _ComputerName; string _NetWorkNum; /// <summary> /// 電腦名稱 /// </summary> public string ComputerName { get { return _ComputerName; } set { _ComputerName = value; } } /// <summary> /// 網絡發送包 /// </summary> public string NetWorkNum { get { return _NetWorkNum; } set { _NetWorkNum = value; } } }
第二步:實例化該類形成多個實體類對象集合,MainPage.xaml.cs的構造函數中敲入代碼如下:
ComputerList = new List<ComputerInfomation>() { new ComputerInfomation(){ComputerName="張三的電腦", NetWorkNum="32143242223"}, new ComputerInfomation(){ComputerName="李四的電腦", NetWorkNum="23432423"}, new ComputerInfomation(){ComputerName="王五的電腦", NetWorkNum="12342342344"}, new ComputerInfomation(){ComputerName="劉六的電腦", NetWorkNum="562342"}, new ComputerInfomation(){ComputerName="林七的電腦", NetWorkNum="55353453445"}, new ComputerInfomation(){ComputerName="馬林的電腦", NetWorkNum="2454555543"} }; BindChart(ComputerList);
第三步:制作一個函數,此函數創建一個圖表并且設置相應的Legend文字標注欄的事件綁定
List<ComputerInfomation> ComputerList = new List<ComputerInfomation>(); /// <summary> /// 綁定一個圖標 /// </summary> /// <param name="computerList">用戶電腦類實體集合</param> public void BindChart( List<ComputerInfomation> computerList) { Chart chart = new Chart(); chart.Width = 400; chart.Height = 550; chart.Name = "Chart"; chart.SetValue(Canvas.LeftProperty, 30.0); chart.SetValue(Canvas.TopProperty, 30.0); chart.Theme = "Theme1";//設置皮膚 chart.BorderBrush = new SolidColorBrush(Colors.Gray); chart.AnimatedUpdate = true; chart.CornerRadius = new CornerRadius(7); chart.ShadowEnabled = true; chart.Padding = new Thickness(4, 4, 4, 10); #region 設置Title Title title = new Title(); title.Text = "電腦網絡發包統計"; chart.Titles.Add(title); #endregion #region 設置AxesX Axis xAxis = new Axis(); xAxis.Title = "用戶電腦"; chart.AxesX.Add(xAxis); #endregion #region 設置AxesY Axis yAxis = new Axis(); yAxis.Title = "用戶網卡發送包"; yAxis.Prefix = "發送:"; yAxis.Suffix = "包"; chart.AxesY.Add(yAxis); #endregion #region 設置PlotArea PlotArea plot = new PlotArea(); plot.ShadowEnabled = false; chart.PlotArea = plot; #endregion #region 設置Legends Legend legend = new Legend(); //Legend文字標注欄綁定一個事件Legend_MouseLeftButtonDown legend.MouseLeftButtonDown += new EventHandler<LegendMouseButtonEventArgs>(Legend_MouseLeftButtonDown); chart.Legends.Add(legend); #endregion #region Visifire.Charts.ToolTip tip = new Visifire.Charts.ToolTip(); tip.VerticalAlignment = VerticalAlignment.Bottom; chart.ToolTips.Add(tip); #endregion #region 創建數據序列和數據點 foreach (ComputerInfomation cominfo in computerList) { DataSeries dseries = new DataSeries(); //設置一個數據序列的LengendText值為ComputerName dseries.LegendText = cominfo.ComputerName; //設置圖表的類型為RenderAs.StackedColumn dseries.RenderAs = RenderAs.StackedColumn; //設置一個數據點 DataPoint dpointUpload = new DataPoint(); //數據點的Y坐標值 dpointUpload.YValue =double.Parse(cominfo.NetWorkNum); //數據點的Tag值也為電腦名稱,用于數據點被點擊后對比判斷當前點擊的點 dpointUpload.Tag = cominfo.ComputerName; //設置數據點被點擊之后觸發事件Dpoint_MouseLeftButtonDown dpointUpload.MouseLeftButtonDown += new MouseButtonEventHandler(Dpoint_MouseLeftButtonDown); dseries.DataPoints.Add(dpointUpload); chart.Series.Add(dseries); } #endregion #region 設置遮罩,將Visifire的LOGO遮擋住。 StackPanel sp = new StackPanel(); sp.Width = 145; sp.Height = 15; sp.Margin = new Thickness(0, 3, 3, 0); sp.VerticalAlignment = VerticalAlignment.Top; sp.HorizontalAlignment = HorizontalAlignment.Right; sp.Background = new SolidColorBrush(Colors.White); #endregion LayoutRoot.Children.Add(chart); LayoutRoot.Children.Add(sp); }
第四步:Lengend事件的設置,那么下面我們貼出Lengend被點擊事件和DataPoint被點擊事件的處理函數
/// <summary> /// DataPoint被點擊執行事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Dpoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //接收到當前被點擊的LengendText的值 DataPoint dpoint = sender as DataPoint; string str = dpoint.Tag.ToString(); foreach (ComputerInfomation cominfo in ComputerList) { if (str == cominfo.ComputerName) { MessageBox.Show(cominfo.ComputerName + "網絡發送:" + cominfo.NetWorkNum + "數據包"); } } } /// <summary> /// Legend文字被點擊執行的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Legend_MouseLeftButtonDown(object sender, LegendMouseButtonEventArgs e) { //接收到當前被點擊的LengendText的值 string str = e.DataSeries.LegendText.ToString(); foreach (ComputerInfomation cominfo in ComputerList) { if (str == cominfo.ComputerName) { MessageBox.Show(cominfo.ComputerName + "網絡發送:" + cominfo.NetWorkNum + "數據包"); } } }
通過以上的方法就很好的解決了數據差異的問題,效果圖如下所示:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都科技