好的圖表不只可以起到美化佈局的做用,並且對於數據的展現也會更加的形象直觀,可是有時候也會出現這種狀況,好比說數據的差別比較大的狀況,在這種狀況下呢,有的很是小的數據可能在圖表中就不可以展現出來。 網絡
最近恰好在看Visifire的圖表,發現裏面的文字標註欄的Legend點擊事件就能夠有效的避免這個問題。 函數
下面就以在平常生活中比較常見的電腦網絡發包量爲例來進行說明,這是我在網上看見的一個例子: 佈局
例如在統計一組用戶電腦的網絡發包量的時候,有一些用戶開啓電腦幾十個小時,有一些用戶開啓電腦幾秒鐘。很明顯用戶開機幾十個小時的發包量巨大,而開機幾秒鐘的發包量極小,若是放在一個Visifire的圖標中組成一個統計列的時候,發包量小的電腦幾乎看不見了。這種狀況下,咱們就能夠經過點擊文字標註欄的Legend文字來肯定某一個在圖表上看不見的用戶電腦的發包量。 對象
第一步:設置一個實體類,該類包含(ComputerName,NetWorkNum)兩個屬性,分別代碼電腦名和電腦網絡發包量: 事件
/// <summary>
/// 電腦信息
/// </summary>
publicclass ComputerInfomation
{
string_ComputerName;
string_NetWorkNum;
/// <summary>
/// 電腦名稱
/// </summary>
publicstringComputerName
{
get{return_ComputerName; }
set{ _ComputerName = value; }
}
/// <summary>
/// 網絡發送包
/// </summary>
publicstringNetWorkNum
{
get{return_NetWorkNum; }
set{ _NetWorkNum = value; }
}
}
第二步:實例化該類造成多個實體類對象集合,MainPage.xaml.cs的構造函數中敲入代碼以下:
ComputerList =newList<ComputerInfomation>()
{
newComputerInfomation(){ComputerName="張三的電腦", NetWorkNum="32143242223"},
newComputerInfomation(){ComputerName="李四的電腦", NetWorkNum="23432423"},
newComputerInfomation(){ComputerName="王五的電腦", NetWorkNum="12342342344"},
newComputerInfomation(){ComputerName="劉六的電腦", NetWorkNum="562342"},
newComputerInfomation(){ComputerName="林七的電腦", NetWorkNum="55353453445"},
newComputerInfomation(){ComputerName="馬林的電腦", NetWorkNum="2454555543"}
};
BindChart(ComputerList);
第三步:製做一個函數,此函數建立一個圖表而且設置相應的Legend文字標註欄的事件綁定
List<ComputerInfomation> ComputerList =newList<ComputerInfomation>();
/// <summary>
/// 綁定一個圖標
/// </summary>
/// <param name="computerList">用戶電腦類實體集合</param>
publicvoidBindChart( List<ComputerInfomation> computerList)
{
Chart chart =newChart();
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 =newSolidColorBrush(Colors.Gray);
chart.AnimatedUpdate =true;
chart.CornerRadius =newCornerRadius(7);
chart.ShadowEnabled =true;
chart.Padding =newThickness(4, 4, 4, 10);
#region 設置Title
Title title =newTitle();
title.Text ="電腦網絡發包統計";
chart.Titles.Add(title);
#endregion
#region 設置AxesX
Axis xAxis =newAxis();
xAxis.Title ="用戶電腦";
chart.AxesX.Add(xAxis);
#endregion
#region 設置AxesY
Axis yAxis =newAxis();
yAxis.Title ="用戶網卡發送包";
yAxis.Prefix ="發送:";
yAxis.Suffix ="包";
chart.AxesY.Add(yAxis);
#endregion
#region 設置PlotArea
PlotArea plot =newPlotArea();
plot.ShadowEnabled =false;
chart.PlotArea = plot;
#endregion
#region 設置Legends
Legend legend =newLegend();
//Legend文字標註欄綁定一個事件Legend_MouseLeftButtonDown
legend.MouseLeftButtonDown +=newEventHandler<LegendMouseButtonEventArgs>(Legend_MouseLeftButtonDown);
chart.Legends.Add(legend);
#endregion
#region
Visifire.Charts.ToolTip tip =newVisifire.Charts.ToolTip();
tip.VerticalAlignment = VerticalAlignment.Bottom;
chart.ToolTips.Add(tip);
#endregion
#region 建立數據序列和數據點
foreach(ComputerInfomation cominfoincomputerList)
{
DataSeries dseries =newDataSeries();
//設置一個數據序列的LengendText值爲ComputerName
dseries.LegendText = cominfo.ComputerName;
//設置圖表的類型爲RenderAs.StackedColumn
dseries.RenderAs = RenderAs.StackedColumn;
//設置一個數據點
DataPoint dpointUpload =newDataPoint();
//數據點的Y座標值
dpointUpload.YValue =double.Parse(cominfo.NetWorkNum);
//數據點的Tag值也爲電腦名稱,用於數據點被點擊後對比判斷當前點擊的點
dpointUpload.Tag = cominfo.ComputerName;
//設置數據點被點擊以後觸發事件Dpoint_MouseLeftButtonDown
dpointUpload.MouseLeftButtonDown +=newMouseButtonEventHandler(Dpoint_MouseLeftButtonDown);
dseries.DataPoints.Add(dpointUpload);
chart.Series.Add(dseries);
}
#endregion
#region 設置遮罩,將Visifire的LOGO遮擋住。
StackPanel sp =newStackPanel();
sp.Width = 145;
sp.Height = 15;
sp.Margin =newThickness(0, 3, 3, 0);
sp.VerticalAlignment = VerticalAlignment.Top;
sp.HorizontalAlignment = HorizontalAlignment.Right;
sp.Background =newSolidColorBrush(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>
voidDpoint_MouseLeftButtonDown(objectsender, MouseButtonEventArgs e)
{
//接收到當前被點擊的LengendText的值
DataPoint dpoint = senderasDataPoint;
stringstr = dpoint.Tag.ToString();
foreach(ComputerInfomation cominfoinComputerList)
{
if(str == cominfo.ComputerName)
{
MessageBox.Show(cominfo.ComputerName +"網絡發送:"+ cominfo.NetWorkNum +"數據包");
}
}
}
/// <summary>
/// Legend文字被點擊執行的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
privatevoidLegend_MouseLeftButtonDown(objectsender, LegendMouseButtonEventArgs e)
{
//接收到當前被點擊的LengendText的值
stringstr = e.DataSeries.LegendText.ToString();
foreach(ComputerInfomation cominfoinComputerList)
{
if(str == cominfo.ComputerName)
{
MessageBox.Show(cominfo.ComputerName +"網絡發送:"+ cominfo.NetWorkNum +"數據包");
}
}
}
經過以上的方法就很好的解決了數據差別的問題,效果圖以下所示:
ip