使用Silverlight Toolkit绘制图表(上)--柱状图
2009-02-10 11:57:18 来源:WEB开发网大家看到,这里使用了Random来模拟一个动态数据源信息,其生成的随机数介于500-3000之间,那么接下来,我
们再Silverlight的XAML上创建这样一个Chart对象,用以显示该动态数据源信息,如下:
<charting:ChartTitle="动态员工薪水"x:Name="DynamicEmployeeChart"/>
接着就是相应的CS代码了,这里为了方便起见,这里直接使用DispatcherTimer来定时(3秒)获取相应的数据源信
息,如下:
voidLoadDynamicData()
{
System.Windows.Threading.DispatcherTimerdispatcherTimer=newSystem.Windows.Threading.DispatcherTimer();
dispatcherTimer.Interval=TimeSpan.FromSeconds(3);
dispatcherTimer.Tick+=delegate
{
dataServiceClient.GetEmployeeDynamicListCompleted+=newEventHandler<GetEmployeeDynamicListCompletedEventArgs>(dataServiceClient_GetEmployeeDynamicListCompleted);
dataServiceClient.GetEmployeeDynamicListAsync();
};
dispatcherTimer.Start();
}
接着就是初始化相应的图形控件并绑定相应的数据源了,代码与上面的CS代码相似,如下:
voiddataServiceClient_GetEmployeeDynamicListCompleted(objectsender,GetEmployeeDynamicListCompletedEventArgse)
{
ObservableCollection<EmployeeInfo>employeeList=e.Result;
DynamicEmployeeChart.Axes.Clear();
DynamicEmployeeChart.Series.Clear();
Action<Chart>chartModifier=(chart)=>
{
AxisdateAxis=newAxis{Orientation=AxisOrientation.Horizontal,Title="雇员名称",FontStyle=FontStyles.Normal,FontSize=12f,ShowGridLines=true};
DynamicEmployeeChart.Axes.Add(dateAxis);
AxisvalueAxis=newAxis{Orientation=AxisOrientation.Vertical,Title="薪水",Minimum=0,Maximum=3000,ShowGridLines=true};
DynamicEmployeeChart.Axes.Add(valueAxis);
};
chartModifier(DynamicEmployeeChart);
ColumnSeriesseries=newColumnSeries();
series.ItemsSource=employeeList;
series.IndependentValueBinding=newSystem.Windows.Data.Binding("EmployeeName");
series.DependentValueBinding=newSystem.Windows.Data.Binding("Salary");
series.Title="薪水";
DynamicEmployeeChart.Series.Add(series);
}
Tags:使用 Silverlight Toolkit
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接