银光亮技巧指南系列:实战教程15 - Visifire图表组件应用(DataPoint点击交互与 Legend文本标签栏点击事件)+ 附赠源代码示例
最编程
2024-07-19 14:31:57
...
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);
}
/// <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);
}
上一篇: 专题探讨:非微软 Silverlight 第三方插件集锦
下一篇: 图表的一些资源