使用Silverlight Toolkit绘制图表(下)--饼图,折线图,散点图2011-09-23 博客园 代震军在上一篇文章中,介绍了如何使用Silverlight Toolkit提供的Chart控件绘制柱状图(Column,Bar), 今天会继续使用上文中所创建的代码,我们只要做很少的修改就可以让柱状图显示变成饼图,折线图或散点图。好了,开始今天的正文。首先,我们要新创建一个饼图的"Silverlight 控件",并将其命名为:PieSample.xaml

然后我们拷贝相应的ColumnSample.xaml,ColumnSample.xaml.cs中的相应代码到: PieSample.xaml 和 Pie-Sample.xaml.cs文件中。接着我们修改PieSample.xaml.cs文件中的 dataServiceClient_GetEmployeeListCompleted方法,修改后的结果如下:
void dataServiceClient_GetEmployeeListCompleted(object sender, GetEmployeeListCompletedEventArgs e)
{
ObservableCollection<EmployeeInfo> employeeList = e.Result;
Action<Chart> chartModifier = (chart) =>
{
Axis dateAxis = new Axis { Orientation = AxisOrientation.Horizontal, Title = "雇员名称 ", FontStyle = FontStyles.Normal, FontSize = 12f, ShowGridLines = true};
EmployeeChart.Axes.Add(dateAxis);
Axis valueAxis = new Axis { Orientation = AxisOrientation.Vertical, Title = "薪水 ", Minimum = -1000, Maximum = 3000, ShowGridLines = true};
EmployeeChart.Axes.Add(valueAxis);
};
chartModifier(EmployeeChart);
PieSeries series = new PieSeries();
series.ItemsSource = employeeList;
series.IndependentValueBinding = new System.Windows.Data.Binding ("EmployeeName");
series.DependentValueBinding = new System.Windows.Data.Binding("Salary");
series.AnimationSequence = AnimationSequence.LastToFirst;
EmployeeChart.Series.Add(series);
}