圖表應用于表現資料量,進行直覺的對比,但是在某一些領域中如果資料之間大小差異過大,那麼會出現某一些資料因為過小,而無法讓使用者看見的情況。例如在統計一組使用者電腦的網絡發包量的時候,有一些使用者開啟電腦幾十個小時,有一些使用者開啟電腦幾秒鐘。很明顯使用者開機幾十個小時的發包量巨大,而開機幾秒鐘的發包量極小,如果放在一個Visifire的圖示中組成一個統計列的時候,發包量小的電腦幾乎看不見了。這種情況下,我們就可以通過點選文字标注欄的Legend文字來确定某一個在圖表上看不見的使用者電腦的發包量。
首先我們設定一個實體類,該類包含(ComputerName,NetWorkNum)兩個屬性,分别代碼電腦名和電腦網絡發包量:

/// <summary>
/// 電腦資訊
/// </summary>
public class ComputerInfomation
{
string _ComputerName;
string _NetWorkNum;
/// 電腦名稱
public string ComputerName
get { return _ComputerName; }
set { _ComputerName = value; }
}
/// 網絡發送包
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>();
/// 綁定一個圖示
/// <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);
#region 設定AxesY
Axis yAxis = new Axis();
yAxis.Title = "使用者網卡發送包";
yAxis.Prefix = "發送:";
yAxis.Suffix = "包";
chart.AxesY.Add(yAxis);
#region 設定PlotArea
PlotArea plot = new PlotArea();
plot.ShadowEnabled = false;
chart.PlotArea = plot;
#region 設定Legends
Legend legend = new Legend();
//Legend文字标注欄綁定一個事件Legend_MouseLeftButtonDown
legend.MouseLeftButtonDown += new EventHandler<LegendMouseButtonEventArgs>(Legend_MouseLeftButtonDown);
chart.Legends.Add(legend);
#region
Visifire.Charts.ToolTip tip = new Visifire.Charts.ToolTip();
tip.VerticalAlignment = VerticalAlignment.Bottom;
chart.ToolTips.Add(tip);
#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);
#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);
LayoutRoot.Children.Add(chart);
LayoutRoot.Children.Add(sp);

關鍵在于Lengend事件的設定,那麼下面我們貼出Lengend被點選事件和DataPoint被點選事件的處理函數:

/// DataPoint被點選執行事件
/// <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 + "資料包");
/// Legend文字被點選執行的事件
private void Legend_MouseLeftButtonDown(object sender, LegendMouseButtonEventArgs e)
string str = e.DataSeries.LegendText.ToString();

本文轉自程興亮部落格園部落格,原文連結:http://www.cnblogs.com/chengxingliang/archive/2011/02/28/1967127.html,如需轉載請自行聯系原作者