1.引用:WPFToolkit.dll, System.Windows.Controls.DataVisualization.Toolkit.dll
2. 页面代码参考如下:
<UserControl x:Class="GDNXFD.Alert.IFIXControls.StationPieChart" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit" Height="200"> <Border BorderThickness="0,0,0,1" BorderBrush="#FFFFFFFF" Padding="10,0,10,10"> <StackPanel> <charting:Chart> <charting:Chart.Template> <ControlTemplate TargetType="{x:Type charting:Chart}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="90" /> </Grid.ColumnDefinitions> <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" /> <chartingprimitives:EdgePanel Name="ChartArea" Height="160" Style="{TemplateBinding ChartAreaStyle}" Grid.Column="0" Margin="5 5 0 5"> <Grid Panel.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" /> <Border Panel.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" /> </chartingprimitives:EdgePanel> <datavis:Legend x:Name="Legend" Height="160" Grid.Column="1" Title="{TemplateBinding LegendTitle}" Style="{TemplateBinding LegendStyle}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0 5 5 5" /> </Grid> </Border> </ControlTemplate> </charting:Chart.Template> <charting:PieSeries Name="pieChart" DependentValuePath="Total" IndependentValuePath="ObjectName" Background="Navy" IsSelectionEnabled="True" > </charting:PieSeries> </charting:Chart> </StackPanel> </Border> </UserControl>
3. 后台代码参考:
IList<AlertSnap> lst = ClientCache.Instance.AlertSnaps; if (lst != null) { var q = from p in lst group p by p.StationName into g select new RankingModel { ObjectName = GetSimplifyStationName( g.Key), Total = g.Count() }; var x = q.OrderByDescending(a => a.Total).Take(9).ToArray(); this.pieChart.ItemsSource = x; }