原創(chuàng)|其它|編輯:郝浩|2012-10-19 16:47:20.000|閱讀 509 次
概述:Visifire圖表控件對有大差異數(shù)據(jù)的表現(xiàn)技巧,本文將以在統(tǒng)計一組用戶電腦的網(wǎng)絡(luò)發(fā)包量的時候為例
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
好的圖表不僅能夠起到美化布局的作用,而且對于數(shù)據(jù)的展示也會更加的形象直觀,但是有時候也會出現(xiàn)這種情況,比如說數(shù)據(jù)的差異比較大的情況,在這種情況下呢,有的非常小的數(shù)據(jù)可能在圖表中就不能夠展示出來。
最近剛好在看Visifire的圖表,發(fā)現(xiàn)里面的文字標注欄的Legend點擊事件就可以有效的避免這個問題。
下面就以在日常生活中比較常見的電腦網(wǎng)絡(luò)發(fā)包量為例來進行說明,這是我在網(wǎng)上看見的一個例子:
例如在統(tǒng)計一組用戶電腦的網(wǎng)絡(luò)發(fā)包量的時候,有一些用戶開啟電腦幾十個小時,有一些用戶開啟電腦幾秒鐘。很明顯用戶開機幾十個小時的發(fā)包量巨大,而開機幾秒鐘的發(fā)包量極小,如果放在一個Visifire的圖標中組成一個統(tǒng)計列的時候,發(fā)包量小的電腦幾乎看不見了。這種情況下,我們就可以通過點擊文字標注欄的Legend文字來確定某一個在圖表上看不見的用戶電腦的發(fā)包量。
第一步:設(shè)置一個實體類,該類包含(ComputerName,NetWorkNum)兩個屬性,分別代碼電腦名和電腦網(wǎng)絡(luò)發(fā)包量:
/// <summary>
/// 電腦信息
/// </summary>
public class ComputerInfomation
{
string _ComputerName;
string _NetWorkNum;
/// <summary>
/// 電腦名稱
/// </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; }
}
}
第二步:實例化該類形成多個實體類對象集合,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);
第三步:制作一個函數(shù),此函數(shù)創(chuàng)建一個圖表并且設(shè)置相應(yīng)的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";//設(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)計";
chart.Titles.Add(title);
#endregion
#region 設(shè)置AxesX
Axis xAxis = new Axis();
xAxis.Title = "用戶電腦";
chart.AxesX.Add(xAxis);
#endregion
#region 設(shè)置AxesY
Axis yAxis = new Axis();
yAxis.Title = "用戶網(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文字標注欄綁定一個事件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ù)點
foreach (ComputerInfomation cominfo in computerList)
{
DataSeries dseries = new DataSeries();
//設(shè)置一個數(shù)據(jù)序列的LengendText值為ComputerName
dseries.LegendText = cominfo.ComputerName;
//設(shè)置圖表的類型為RenderAs.StackedColumn
dseries.RenderAs = RenderAs.StackedColumn;
//設(shè)置一個數(shù)據(jù)點
DataPoint dpointUpload = new DataPoint();
//數(shù)據(jù)點的Y坐標值
dpointUpload.YValue =double.Parse(cominfo.NetWorkNum);
//數(shù)據(jù)點的Tag值也為電腦名稱,用于數(shù)據(jù)點被點擊后對比判斷當前點擊的點
dpointUpload.Tag = cominfo.ComputerName;
//設(shè)置數(shù)據(jù)點被點擊之后觸發(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);
}
第四步:Lengend事件的設(shè)置,那么下面我們貼出Lengend被點擊事件和DataPoint被點擊事件的處理函數(shù)
/// <summary>
/// DataPoint被點擊執(zhí)行事件
/// </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 + "網(wǎng)絡(luò)發(fā)送:" + cominfo.NetWorkNum + "數(shù)據(jù)包");
}
}
}
/// <summary>
/// Legend文字被點擊執(zhí)行的事件
/// </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 + "網(wǎng)絡(luò)發(fā)送:" + cominfo.NetWorkNum + "數(shù)據(jù)包");
}
}
}
通過以上的方法就很好的解決了數(shù)據(jù)差異的問題,效果圖如下所示:

本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都科技