圖表應用於表現數據量,進行直觀的對比,可是在某一些領域中若是數據之間大小差別過大,那麼會出現某一些數據由於太小,而沒法讓用戶看見的狀況。例如在統 計一組用戶電腦的網絡發包量的時候,有一些用戶開啓電腦幾十個小時,有一些用戶開啓電腦幾秒鐘。很明顯用戶開機幾十個小時的發包量巨大,而開機幾秒鐘的發 包量極小,若是放在一個Visifire的圖標中組成一個統計列的時候,發包量小的電腦幾乎看不見了。這種狀況下,咱們就能夠經過點擊文字標註欄的 Legend文字來肯定某一個在圖表上看不見的用戶電腦的發包量。sql
首先咱們設置一個實體類,該類包含(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的構造函數中敲入代碼以下:ide
- 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被點擊事件的處理函數:spa
- /// <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 + "數據包");
- }
- }
- }
如此就解決了本文開頭所述的圖標繪製的問題。本實例採用VS2010+Silverlight 4.0編寫,點擊 SLTOVisiFire.rar 下載源碼。運行效果以下:對象