轉(zhuǎn)帖|其它|編輯:郝浩|2011-03-01 13:18:29.000|閱讀 1497 次
概述:圖表應(yīng)用于表現(xiàn)數(shù)據(jù)量,進(jìn)行直觀的對(duì)比,但是在某一些領(lǐng)域中如果數(shù)據(jù)之間大小差異過(guò)大,那么會(huì)出現(xiàn)某一些數(shù)據(jù)因?yàn)檫^(guò)小,而無(wú)法讓用戶(hù)看見(jiàn)的情況。例如在統(tǒng)計(jì)一組用戶(hù)電腦的網(wǎng)絡(luò)發(fā)包量的時(shí)候,有一些用戶(hù)開(kāi)啟電腦幾十個(gè)小時(shí),有一些用戶(hù)開(kāi)啟電腦幾秒鐘。很明顯用戶(hù)開(kāi)機(jī)幾十個(gè)小時(shí)的發(fā)包量巨大,而開(kāi)機(jī)幾秒鐘的發(fā)包量極小,如果放在一個(gè)Visifire的圖標(biāo)中組成一個(gè)統(tǒng)計(jì)列的時(shí)候,發(fā)包量小的電腦幾乎看不見(jiàn)了。這種情況下,我們就可以通過(guò)點(diǎn)擊文字標(biāo)注欄的 Legend文字來(lái)確定某一個(gè)在圖表上看不見(jiàn)的用戶(hù)電腦的發(fā)包量。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
圖表應(yīng)用于表現(xiàn)數(shù)據(jù)量,進(jìn)行直觀的對(duì)比,但是在某一些領(lǐng)域中如果數(shù)據(jù)之間大小差異過(guò)大,那么會(huì)出現(xiàn)某一些數(shù)據(jù)因?yàn)檫^(guò)小,而無(wú)法讓用戶(hù)看見(jiàn)的情況。例如在統(tǒng)計(jì)一組用戶(hù)電腦的網(wǎng)絡(luò)發(fā)包量的時(shí)候,有一些用戶(hù)開(kāi)啟電腦幾十個(gè)小時(shí),有一些用戶(hù)開(kāi)啟電腦幾秒鐘。很明顯用戶(hù)開(kāi)機(jī)幾十個(gè)小時(shí)的發(fā)包量巨大,而開(kāi)機(jī)幾秒鐘的發(fā)包量極小,如果放在一個(gè)Visifire的圖標(biāo)中組成一個(gè)統(tǒng)計(jì)列的時(shí)候,發(fā)包量小的電腦幾乎看不見(jiàn)了。這種情況下,我們就可以通過(guò)點(diǎn)擊文字標(biāo)注欄的Legend文字來(lái)確定某一個(gè)在圖表上看不見(jiàn)的用戶(hù)電腦的發(fā)包量。
首先我們?cè)O(shè)置一個(gè)實(shí)體類(lèi),該類(lèi)包含(ComputerName,NetWorkNum)兩個(gè)屬性,分別代碼電腦名和電腦網(wǎng)絡(luò)發(fā)包量:
/// <summary>
/// 電腦信息
/// </summary>
public class ComputerInfomation
{
string _ComputerName;
string _NetWorkNum;
/// <summary>
/// 電腦名稱(chēng)
/// </summary>
public string ComputerName
{
get { return _ComputerName; }
set { _ComputerName = value; }
}
/// <summary>
/// 網(wǎng)絡(luò)發(fā)送包
/// </summary>
public string NetWorkNum
{
get { return _NetWorkNum; }
set { _NetWorkNum = value; }
}
}
再實(shí)例化該類(lèi)形成多個(gè)實(shí)體類(lèi)對(duì)象集合,MainPage.xaml.cs的構(gòu)造函數(shù)中敲入代碼如下:
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);
現(xiàn)在用戶(hù)電腦數(shù)據(jù)已經(jīng)準(zhǔn)備好了,我們開(kāi)始制作一個(gè)函數(shù),此函數(shù)創(chuàng)建一個(gè)圖表并且設(shè)置相應(yīng)的Legend文字標(biāo)注欄的事件綁定:
List<ComputerInfomation> ComputerList = new List<ComputerInfomation>();
/// <summary>
/// 綁定一個(gè)圖標(biāo)
/// </summary>
/// <param name="computerList">用戶(hù)電腦類(lèi)實(shí)體集合</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";//設(shè)置皮膚
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 設(shè)置Title
Title title = new Title();
title.Text = "電腦網(wǎng)絡(luò)發(fā)包統(tǒng)計(jì)";
chart.Titles.Add(title);
#endregion
#region 設(shè)置AxesX
Axis xAxis = new Axis();
xAxis.Title = "用戶(hù)電腦";
chart.AxesX.Add(xAxis);
#endregion
#region 設(shè)置AxesY
Axis yAxis = new Axis();
yAxis.Title = "用戶(hù)網(wǎng)卡發(fā)送包";
yAxis.Prefix = "發(fā)送:";
yAxis.Suffix = "包";
chart.AxesY.Add(yAxis);
#endregion
#region 設(shè)置PlotArea
PlotArea plot = new PlotArea();
plot.ShadowEnabled = false;
chart.PlotArea = plot;
#endregion
#region 設(shè)置Legends
Legend legend = new Legend();
//Legend文字標(biāo)注欄綁定一個(gè)事件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 創(chuàng)建數(shù)據(jù)序列和數(shù)據(jù)點(diǎn)
foreach (ComputerInfomation cominfo in computerList)
{
DataSeries dseries = new DataSeries();
//設(shè)置一個(gè)數(shù)據(jù)序列的LengendText值為ComputerName
dseries.LegendText = cominfo.ComputerName;
//設(shè)置圖表的類(lèi)型為RenderAs.StackedColumn
dseries.RenderAs = RenderAs.StackedColumn;
//設(shè)置一個(gè)數(shù)據(jù)點(diǎn)
DataPoint dpointUpload = new DataPoint();
//數(shù)據(jù)點(diǎn)的Y坐標(biāo)值
dpointUpload.YValue =double.Parse(cominfo.NetWorkNum);
//數(shù)據(jù)點(diǎn)的Tag值也為電腦名稱(chēng),用于數(shù)據(jù)點(diǎn)被點(diǎn)擊后對(duì)比判斷當(dāng)前點(diǎn)擊的點(diǎn)
dpointUpload.Tag = cominfo.ComputerName;
//設(shè)置數(shù)據(jù)點(diǎn)被點(diǎn)擊之后觸發(fā)事件Dpoint_MouseLeftButtonDown
dpointUpload.MouseLeftButtonDown += new MouseButtonEventHandler(Dpoint_MouseLeftButtonDown);
dseries.DataPoints.Add(dpointUpload);
chart.Series.Add(dseries);
}
#endregion
#region 設(shè)置遮罩,將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);
}
關(guān)鍵在于Lengend事件的設(shè)置,那么下面我們貼出Lengend被點(diǎn)擊事件和DataPoint被點(diǎn)擊事件的處理函數(shù):
/// <summary>
/// DataPoint被點(diǎn)擊執(zhí)行事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Dpoint_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//接收到當(dāng)前被點(diǎn)擊的LengendText的值
DataPoint dpoint = sender as DataPoint;
string str = dpoint.Tag.ToString();
foreach (ComputerInfomation cominfo in ComputerList)
{
if (str == cominfo.ComputerName)
{
MessageBox.Show(cominfo.ComputerName + "網(wǎng)絡(luò)發(fā)送:" + cominfo.NetWorkNum + "數(shù)據(jù)包");
}
}
}
/// <summary>
/// Legend文字被點(diǎn)擊執(zhí)行的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Legend_MouseLeftButtonDown(object sender, LegendMouseButtonEventArgs e)
{
//接收到當(dāng)前被點(diǎn)擊的LengendText的值
string str = e.DataSeries.LegendText.ToString();
foreach (ComputerInfomation cominfo in ComputerList)
{
if (str == cominfo.ComputerName)
{
MessageBox.Show(cominfo.ComputerName + "網(wǎng)絡(luò)發(fā)送:" + cominfo.NetWorkNum + "數(shù)據(jù)包");
}
}
}
如此就解決了本文開(kāi)頭所述的圖標(biāo)繪制的問(wèn)題。本實(shí)例采用VS2010+Silverlight 4.0編寫(xiě),點(diǎn)擊 SLTOVisiFire.rar 下載源碼。運(yùn)行效果如下:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載